这里我有一点不同的方法,它使用 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()
}