简体   繁体   English

Gradle:将多项目依赖项压缩到应用程序JAR文件中?

[英]Gradle: Zipping Multi-project dependencies into application JAR file?

In a build.gradle script, I would perform the following task to include dependencies into a runnable application JAR file. build.gradle脚本中,我将执行以下任务以将依赖项包含到可运行的应用程序JAR文件中。

jar {
manifest {
    attributes(
            'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
            'Main-Class': 'org.somepackage.MyMainClass'
    )
}
from configurations.compile.collect { entry -> zipTree(entry) }
}

However, in multi-project setups I was missing some dependencies in the JAR file. 但是,在多项目设置中,我缺少JAR文件中的某些依赖项。 I ultimately discovered any dependencies specified in other project build.gradle scripts were not being included, unless the parent project had those dependencies too. 最终,我发现其他项目build.gradle脚本中指定的任何依赖项均未包括在内,除非父项目也具有这些依赖项。

For example, Project A depends on Project B. Project B uses Google Guava. 例如,项目A取决于项目B。项目B使用Google Guava。 When I deployed Project A to a runnable JAR file, Google Guava would not be included and there would be runtime errors. 当我将Project A部署到可运行的JAR文件时,将不会包含Google Guava,并且会出现运行时错误。 If Project A redundantly specified Google Guava in its build.gradle script, then it runs just fine. 如果Project A在其build.gradle脚本中冗余地指定了Google Guava,则它运行良好。

How can I modify my jar task above to include dependencies in multi-project setups? 如何修改上面的jar任务以在多项目设置中包括依赖项?

I got some ideas from this post that seemed to work. 我从这篇文章中得到了一些可行的想法。

jar {
    manifest {
        attributes(
                'Main-Class': 'com.swa.rm.pricing.reporting.ux.SaleMxReportingUI'
        )
    }
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

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

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