配置阶段后运行commandLine会导致gradle的未来版本出现问题?

时间:2017-12-10 18:03:00

标签: gradle groovy

我正在使用gradle脚本,我正在获取hg存储库的修订版,然后使用该修订版用以下任务标记存储库,这些工作正常。我担心的是我在配置阶段后触发命令行,这可能会创建未来版本的gradle中的一个问题。还有其他方法可以完成这些任务吗?

task hgRev(type: Exec, dependsOn: UpdateRepo) {
    commandLine 'hg', 'id', '-i', "${project.rootDir}"
    standardOutput = new ByteArrayOutputStream()
    ext.hash = {
        return standardOutput.toString()
    }
}

task tagHg(type:Exec, dependsOn: hgRev) {
    doLast {
        if (execResult.exitValue == 0) {
            project.logger.info("Succesfully Created the tag \"Build $cbversion\"")
        } else {
            project.logger.info("It failed.Please check the Bamboo logs for the reason")
        }
    }
    doFirst {
        def hash = tasks.hgRev.hash()
        commandLine 'hg', 'tag', '-r', "$hash", "Build $cbversion"

    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用exec块和execute()方法代替Exec任务,这将使许多事情变得更容易:

// simple method, not a task
def hgRev() {
    def hashStdOut = new ByteArrayOutputStream()
    exec {
        commandLine 'hg', 'id', '-i', "${project.rootDir}"
        standardOutput = hashStdOut
    }
    return hashStdOut.toString().replaceAll('\\+', '').trim()
} 

task tagHg(dependsOn: UpdateRepo) {
    doLast {
        def hash = hgRev()

        def cmd = ["hg", "tag", "-r", hash, "Build $cbversion"]
        def sout = new StringBuilder(), serr = new StringBuilder()

        Process proc = cmd.execute()
        proc.consumeProcessOutput(sout, serr)
        proc.waitForOrKill(1000)
        println "out> $sout err> $serr"

        if (proc.exitValue() == 0) {
            project.logger.info("Succesfully Created the tag ")
        } else {
            project.logger.info("It failed.Please check the Bamboo logs for the reason")
        }
    }
}