Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an uberjar with Gradle

I want to build an uberjar (AKA fatjar) that includes all the transitive dependencies of the project. What lines do I need to add to 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     } } 
like image 320
missingfaktor Avatar asked Jun 11 '12 19:06

missingfaktor


People also ask

Can Gradle build Maven project?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

What is shadow jar in Gradle?

Shadow is a Gradle plugin for combining a project's dependency classes and resources into a single output Jar. The combined Jar is often referred to a fat-jar or uber-jar.


2 Answers

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.

like image 119
missingfaktor Avatar answered Oct 04 '22 02:10

missingfaktor


Have you tried the fatjar example in the gradle cookbook?

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

like image 20
tim_yates Avatar answered Oct 04 '22 02:10

tim_yates