Dynamic version creation for jar

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

}

}

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
 doFirst{
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é

Is it possible to set the global version number?

edit: To affect the jar name I mean?

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?

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

version = "1.0"

Well, from my script…

I would need a construct like:

version = runMyTask()

Is it possible?

And the magic:

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

Ack!

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?

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

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