Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task to write hg revision to file

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()
    }
}
like image 441
cmh Avatar asked Dec 17 '12 19:12

cmh


People also ask

How do I create a zip file in Gradle?

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.

What is task type in Gradle?

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.

Does Gradle build create jar?

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.


2 Answers

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()
}
like image 147
bugs_ Avatar answered Nov 12 '22 11:11

bugs_


There are two issues with this build script:

  1. the command line needs to be split; gradle's trying to execute a binary named hg id -i -b t instead of hg with arguments id, -i, -b and t
  2. The standard output needs to be captured; you can make it a ByteOutputStream to be read later

Try 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()
    }
}
like image 40
ataylor Avatar answered Nov 12 '22 10:11

ataylor