How can I create a manifest for a jar that has dynamic attributes computed by a task?


(Jim Cox) #1

I would like to add attributes to a jar’s manifest file that are determined by the output of an executed task.

apply plugin: 'java'
  task svninfo
{
          ext.svnURL = 'placeholder url'
        ext.svnLastChangedRev = 'placeholder rev'
          doLast {
                svnLastChangedRev = 'real rev'
                svnURL = 'real url'
                  println "Latest Changed Revision #:
$svnLastChangedRev"
                println "URL: $svnURL"
        }
}
  jar.dependsOn svninfo
  jar {
      manifest {
                attributes(
                'Implementation-Title' : "Jar Test",
                'Implementation-Version' : "1",
                'Compile-Time'
        : new java.util.Date().toString(),
                'Compiled-By'
         : System.getProperty("user.name"),
                'SVN-Revision'
        : svninfo.svnLastChangedRev,
                'SVN-URL'
             : svninfo.svnURL
        )}
}

When I run this build, I can clearly see that the svninfo task is run before the jar task and that the properties are updatetd, but the Manifest entries for the svn props are still set to

SVN-Revision: placeholder rev
SVN-URL: placeholder url

What am I missing here?


(Benjamin Muschko) #2

The problem you see here is that the manifest attributes are configured even before your svninfo task action is executed. You might want to resolve your SVN information during Gradle’s configuration phase and not during the execution phase. Is there a reason why you don’t do that by default? I don’t think querying this information is horribly expensive.


(Jim Cox) #3

Thanks Benjamin. Your response pointed me to “Chapter 55. The Build Lifecycle.”

Pulling out the property setting from the doLast produced the desired result.