Gradle任务将hg修订版写入文件

cmh*_*cmh 11 version-control mercurial groovy gradle

是否有一种简单的方法可以在gradle任务中写入mercurial版本(或类似的外部命令):

我还不熟悉/熟悉,但我目前的努力看起来像这样:

task versionInfo(type:Exec){
    commandLine 'hg id -i -b -t'
    ext.versionfile = new File('bin/$baseName-buildinfo.properties')

    doLast {
        versionfile.text = 'build.revision=' + standardOutput.toString()
    }
}
Run Code Online (Sandbox Code Playgroud)

ata*_*lor 14

此构建脚本存在两个问题:

  1. 命令行需要拆分; gradle这个公司试图执行一个名为二进制hg id -i -b t的,而不是hg用参数id,-i,-bt
  2. 需要捕获标准输出; 你可以把它变成ByteOutputStream以后阅读

试试这个:

task versionInfo(type:Exec){
    commandLine 'hg id -i -b -t'.split()
    ext.versionfile = new File('bin/$baseName-buildinfo.properties')
    standardOutput = new ByteArrayOutputStream()

    doLast {
        versionfile.text = 'build.revision=' + standardOutput.toString()
    }
}
Run Code Online (Sandbox Code Playgroud)