简体   繁体   English

用 Gradle 构建一个 uberjar

[英]Building an uberjar with Gradle

I want to build an uberjar (AKA fatjar) that includes all the transitive dependencies of the project.我想构建一个包含项目所有传递依赖项的 uberjar(又名 fatjar)。 What lines do I need to add to build.gradle ?我需要在build.gradle添加哪些行?

This is what I currently have:这是我目前拥有的:

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)

    manifest {
        attributes 'Implementation-Title': 'Foobar',
                'Implementation-Version': version,
                'Built-By': System.getProperty('user.name'),
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                'Main-Class': mainClassName
    }
}

I replaced the task uberjar(.. with the following:我将task uberjar(..替换为以下内容:

jar {
    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }

    manifest {
        attributes 'Implementation-Title': 'Foobar',
                'Implementation-Version': version,
                'Built-By': System.getProperty('user.name'),
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                'Main-Class': mainClassName
    }
}

The exclusions are needed because in their absence you will hit this issue.需要排除项,因为如果没有它们,您会遇到问题。

Have you tried the fatjar example in the gradle cookbook ?您是否尝试过gradle 食谱中的 fatjar 示例?

What you're looking for is the shadow plugin for gradle您正在寻找的是 gradle的影子插件

Simply add this to your java module's build.gradle.只需将其添加到您的 java 模块的 build.gradle 中即可。

mainClassName = "my.main.Class" mainClassName = "my.main.Class"

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

This will result in [module_name]/build/libs/[module_name].jar file.这将产生 [module_name]/build/libs/[module_name].jar 文件。

I found this project very useful.我发现这个项目非常有用。 Using it as a reference, my Gradle uberjar task would be使用它作为参考,我的 Gradle uberjar 任务将是

task uberjar(type: Jar, dependsOn: [':compileJava', ':processResources']) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }

    manifest {
        attributes 'Main-Class': 'SomeClass'
    }
}

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

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