Building a uberjar with Gradle

丶灬走出姿态 提交于 2019-11-26 07:34:12

问题


I am a Gradle novice. I want to build a uberjar (AKA fatjar) that includes all the transitive dependencies of the project. What lines do I need to add to my \"build.gradle\"?

This is what I currently have: (I copied it from somewhere a few days back, but don\'t recollect from where.)

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
    }
}

回答1:


Have you tried the fatjar example in the gradle cookbook?

What you're looking for is the shadow plugin for gradle




回答2:


I replaced the task uberjar(.. with the following:

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.




回答3:


Simply add this to your java module's build.gradle.

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.




回答4:


I found this project very useful. Using it as a reference, my Gradle uberjar task would be

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'
    }
}


来源:https://stackoverflow.com/questions/10986244/building-a-uberjar-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!