gradle - 在配置阶段之后运行commandLine会在gradle的 future 版本中引起问题吗?

标签 gradle groovy

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

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"

    }

}

最佳答案

您可以使用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")
        }
    }
}

关于gradle - 在配置阶段之后运行commandLine会在gradle的 future 版本中引起问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741819/

相关文章:

android - 在 Android Gradle 任务中获取编译后的 .class 输出目录

gradle - 在 Spring 启动应用的Spock单元测试中未找到给定的测试

java - 从java上传本地文件到谷歌驱动器

android - 发现多个文件具有独立于操作系统的路径 'lib/armeabi-v7a/libopencv_java3.so'

android - 为什么 androidx.preference 库会阻止我的自定义复选框正常显示?

android - 使用 Gradle 在 Android Studio 中运行普通 Java 项目的单元测试

java - 在 grails 单元测试中获取域实体的结果

groovy - 应用gradle插件有什么区别

mysql - 如何在不使用字段名称的情况下从 Groovy 中的 MySQL 表行获取所有字段值?

java - 如何将字符串列表从 JMeter 传递到 Java 代码