Is there a simple way to write to file the mercurial version (or similar external command) in a gradle task:
I'm not yet groovy/gradle conversant, but my current effort looks like this:
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()
}
}
In Gradle, zip files can be created by using 'type: zip' in the task. In the below example, from is the path of source folder which we want to zip. destinationDir is the path of the destination where the zip file is to be created. archiveName is the name of the zip file.
Gradle supports two types of task. One such type is the simple task, where you define the task with an action closure. We have seen these in Build Script Basics. For this type of task, the action closure determines the behaviour of the task. This type of task is good for implementing one-off tasks in your build script.
In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies. Tools used : Gradle 2.0.
Here I have a little bit different approach, which uses javahg to get revision. And add task "writeRevisionToFile"
I wrote brief post on my blog 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()
}
There are two issues with this build script:
hg id -i -b t
instead of hg
with arguments id
, -i
, -b
and t
ByteOutputStream
to be read laterTry this:
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With