11

gradleタスクでmercurialバージョン(または同様の外部コマンド)をファイルに書き込む簡単な方法はありますか:

私はまだ groovy/gradle に精通していませんが、現在の取り組みは次のようになります。

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()
    }
}
4

2 に答える 2

14

このビルド スクリプトには 2 つの問題があります。

  1. コマンドラインを分割する必要があります。gradleは、引数、、およびhg id -i -b tの代わりにという名前のバイナリを実行しようとしていますhgid-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()
    }
}
于 2012-12-17T20:29:13.547 に答える
3

ここでは、javahg を使用してリビジョンを取得する、少し異なるアプローチがあります。そして、タスク「writeRevisionToFile」を追加します

私は自分のブログGradle - Get Hg Mercurial Revisionに短い記事を書きました。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.aragost.javahg:javahg:0.4'
    }
}

task writeRevisionToFile << {
    new File(projectDir, "file-with-revision.txt").text = scmRevision
}


import com.aragost.javahg.Changeset
import com.aragost.javahg.Repository
import com.aragost.javahg.commands.ParentsCommand

String getHgRevision() {
    def repo = Repository.open(projectDir)
    def parentsCommand = new ParentsCommand(repo)
    List<Changeset> changesets = parentsCommand.execute()
    if (changesets == null || changesets.size() != 1) {
        def message = "Exactly one was parent expected. " + changesets
        throw new Exception(message)
    }
    return changesets[0].node
}

ext {
    scmRevision = getHgRevision()
}
于 2015-10-19T11:53:50.090 に答える