【问题标题】:gradle: exclude a dependency in making fatJargradle:在制作 fatJar 时排除依赖项
【发布时间】:2020-04-25 18:53:11
【问题描述】:

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

maven 示例: How can I tell which signed jar is causing maven-shade-plugin to fail?

gradle 示例: Removing Jar Signatures in Gradle Build

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

我们如何制作一个包含特定依赖库(而不是该库中的单个文件)的 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 文件的内容仍会复制到生成的 fat jar 中。

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

【问题讨论】:

    标签: gradle bouncycastle fatjar


    【解决方案1】:

    我不知道 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
    }
    

    【讨论】:

    • 非常感谢您周到的回答!我很惊讶没有直接的方法来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多