Here is part of build.gradle file, which get mercurial revision and define task "revision", which print it to output.
It uses javahg to get revision number and that's why it needs Mercurial installation. We could use hg4j to remove this requirement. Anyway this script can be improved a lot, but for me it did it's work.
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.aragost.javahg:javahg:0.4' | |
} | |
} | |
task revision << { | |
println "Build revision: $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() | |
} |