简体   繁体   English

使用Gradle将属性添加到jar文件的清单中

[英]Adding attributes to a jar file's manifest using Gradle

I'd like to bundle some jar files built with Gradle 1.6 as part of a Java webstart application. 我想将一些使用Gradle 1.6构建的jar文件捆绑为Java webstart应用程序的一部分。

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,并使用标准jar任务指定生成的工件的codebasepermissions属性,如下所示:

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. 这是因为Oracle JDK / JRE中的最新Java webstart版本使这些属性成为必需属性,否则它会向用户抱怨应用程序的安全性。

Preventing RIAs from Being Repurposed 防止RIA被重新利用

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. 但是,我的工件罐子有第三方依赖(例如,slf4j),我还没有找到一种直观的方式将这些属性包含在这样的第三方罐子里。 With this in mind, my final webstart application structure looks sort of like this: 考虑到这一点,我的最终webstart应用程序结构看起来像这样:

./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: 结果是,在运行时,webstart不会抱怨我的工件,但会对第三方罐子这样做,因为他们显然在他们的清单文件中没有属性:

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. 由于我不直接控制第三方jar的清单生成,因此我必须以某种方式修改build/webstart/lib目录中的清单文件,以明确包含这些属性,以使webstart满意。

Is there a way to add attributes to a jar file's manifest with Gradle? 有没有办法使用Gradle向jar文件的清单添加属性? In case anyone's interested, this is my Gradle build script: 如果有人感兴趣,这是我的Gradle构建脚本:

build.gradle 的build.gradle

Thanks for your time and help! 感谢您的时间和帮助!

UPDATE 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! 谢谢!

To set these attributes, you'll have to unpack the Jars, edit the manifests (using Groovy), and repack the Jars. 要设置这些属性,您必须解压缩Jars,编辑清单(使用Groovy),然后重新打包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 ). 或者,您可以尝试使用ant.jar(update = true)覆盖清单,但是似乎不支持覆盖文件(不添加副本)(请参阅Ant文档中的duplicate属性)。 Merging the Jars (in one way or another) might be another option. 合并罐子(以某种方式)可能是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM