Dynamic version creation for jar

(Juan Valencia) #1

I have a task that calculates a version number via an exec type. I use it to insert a version file into the output jar.

I would like to inject the version number into a jar manifest. However, the manifest is created during configuration. How do I delay the manifest creation? Alternatively, how can I get the version file during config?

Taking the file << out of doLast means the file is empty. Putting the mainfest in doLast doesn’t work.

task Version(type:Exec) {

outputs.upToDateWhen { false }

delete versionFileName

outputs.file versionFile

commandLine ‘version’

standardOutput = new ByteArrayOutputStream()

doLast {

versionFile << standardOutput.toString()

println “Version:” + versionFile.text

} }

jar {

dependsOn Version

from Version.outputs.files

manifest {

attributes ‘Implementation-Title’: ‘Project’,

‘Implementation-Version’: versionFile ? “0.0.0” : versionFile.text,

‘Main-Class’: mainClassName



(René Groeschke) #2

at the moment, the only way to defer the configuration of the manifest, is to put it into a doFirst block

jar {
 dependsOn Version
 from Version.outputs.files
manifest {
 attributes 'Implementation-Title': 'Project',
 'Implementation-Version': versionFile ? "0.0.0" : versionFile.text,
 'Main-Class': mainClassName

do you really need the version file within the jar or do you just want to use the information in it? cheers, René

(Juan Valencia) #3

Is it possible to set the global version number?

edit: To affect the jar name I mean?

(Juan Valencia) #4

Hmmm… the doLast still runs after the doFirst.

Perhaps let me rephrase the question. How do I get the version number from my script before the jar manifest is created?

(René Groeschke) #5

well you can just set the version number of your project e.g.

version = "1.0"

(Juan Valencia) #6

Well, from my script…

I would need a construct like:

version = runMyTask()

Is it possible?

(Juan Valencia) #7

And the magic:

def getVersion() {
    def command = """myScript"""// Create the String
    def proc = command.execute()
               // Call *execute* on the string
    return proc.in.text
  version = getVersion()

(Juan Valencia) #8


So it all worked. Jar was good with format myJar-X.X.X.jar , and code committed. Then Jenkins builds the jar with a different version? workspace-jenkins-myJar-buildNumber.jar

Any idea what is going on?

(Juan Valencia) #9

Jenkins uses the dir name to figure out the jar name: eg “workspace” The version number I set from git describe, but jenkins adds annotated tags to git - hence the jenkins-myJar-buildNumber

(Juan Valencia) #10

can use 'git describe --match [0-9].* to filter them out.