gradle:排除制作fatJar的依赖性



关于如何从fatJar中排除单个文件,有很多重复的答案。通常,排除的文件位于 META-INF 中,并且由于文件名冲突或因为它是从依赖项 libarar Jar 文件复制的签名而排除,该文件对新创建的 Jar 文件无效。

专家示例: 我如何判断哪个签名的 jar 导致 maven-shade-plugin 失败?

格拉德尔示例: 在 Gradle 构建中删除 jar 签名

但是,这些解决方案仅单独删除了有问题的文件。

我们如何使具有特定依赖项库(而不是该库中的单个文件(的 fatJar 被排除

在外?例如,在问题36226033中,很容易排除从 BouncyCastle 复制的签名,但是有没有办法完全排除依赖库bcprov-jdk15on-*.jar,以便用户必须拥有可用的库才能执行生成的胖 Jar?

事实证明这不起作用:

task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
exclude('**/bcprov-jdk15on-1.62.jar')
with jar
}

使用exclude('**/bcprov-jdk15on-1.62.jar'),该jar文件的内容仍然被复制到生成的胖jar中。

谢谢。动机是将我的Java应用程序发布到提供自己的安全库BouncyCastle(例如Debian Linux(的系统,而不是嵌入该安全库的未签名副本。

我不知道excludesincludes是如何工作的,但我希望这些配置在类文件级别工作,因为这是 jar 正在处理的内容。

它不适用于罐子。

我会选择这个解决方案:

task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
configurations.compile.findAll { file ->
// file is of type java.io.File
// when true, jar file is unziped and added 
file.name != "bcprov-jdk15on-1.62.jar"
}.sort { it.name }
.collect { file ->
logger.info("Including file: ${file.name}")
file.isDirectory() ? file : zipTree(file)
}
with jar
}

最新更新