Adding attributes to a jar file's manifest using Gradle
Asked Answered
R

1

9

I'd like to bundle some jar files built with Gradle 1.6 as part of a Java webstart application.

I can currently sign the jars correctly with a certificate and specify the codebase and permissions attributes for the produced artifacts by using the standard jar task like so:

jar {
    manifest.attributes provider: 'tribe7.net'
    manifest.attributes permissions: 'all-permissions'
    manifest.attributes codebase: '*'
}

This is because the latest Java webstart version in Oracle's JDK/JRE makes these attributes mandatory or else it complains to the user about the application's security.

Preventing RIAs from Being Repurposed

However, my artifact jars have third party dependencies (for example, slf4j) and I have yet to find an intuitive way to include these atributes in such third party jars. With this in mind, my final webstart application structure looks sort of like this:

./build/webstart/my.jnlp
./build/webstart/lib/myartifactA-1.00.jar
./build/webstart/lib/myartifactB-1.00.jar
./build/webstart/lib/myartifactC-1.00.jar
./build/webstart/lib/slf4j-api-1.7.5.jar

The result is that at runtime, webstart doesn't complain about my artifacts but does so for the third party jars because they obviously don't have the attributes in their manifest file:

Missing Codebase manifest attribute for:    file:/C:/build/webstart/lib/slf4j-api-1.7.5.jar
Missing Permissions manifest attribute for: file:/C:/build/webstart/lib/slf4j-api-1.7.5.jar
Missing Codebase manifest attribute for:    file:/C:/build/webstart/lib/slf4j-simple-1.7.5.jar
Missing Permissions manifest attribute for: file:/C:/build/webstart/lib/slf4j-simple-1.7.5.jar

Since I don't directly control the manifest generation for the third party jars I have to somehow modify the manifest files present inside the build/webstart/lib directory to explicitly include those attributes in order to make webstart happy.

Is there a way to add attributes to a jar file's manifest with Gradle? In case anyone's interested, this is my Gradle build script:

build.gradle

Thanks for your time and help!

UPDATE

Peter's answer worked! This is the updated code:

ant.jar(destfile: it, update: true) {
  delegate.manifest {
    attribute(name: 'permissions', value: 'all-permissions')
    attribute(name: 'codebase', value: '*')
  }
}

ant.signjar(
  destDir: webstartSignedLibPath,
  alias: project.getProperty('jarsign.keystore.alias'),
  jar: it,
  keystore: project.getProperty('jarsign.keystore.path'),
  storepass: project.getProperty('jarsign.keystore.password'),
  preservelastmodified: 'true'
)

Thanks!

Radical answered 25/7, 2013 at 1:6 Comment(1)
After a long search, this was the first example I could find where there's actually shown how to set attributes in a manifest element in an ant jar task call inside gradle. On the gradle website somebody asked that question and they instead showed an example of a gradle manifest element, not one for ant, which was completely irrelevant. Finally I found an answer here, thank you.Youth
P
4

To set these attributes, you'll have to unpack the Jars, edit the manifests (using Groovy), and repack the Jars. Alternatively, you could try to overwrite the manifests with ant.jar(update = true), although overwriting a file (without adding a duplicate) doesn't seem to be supported (see duplicate attribute in the Ant docs). Merging the Jars (in one way or another) might be another option.

Physicochemical answered 25/7, 2013 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.