missingfaktor
missingfaktor

Reputation: 92056

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

Upvotes: 43

Views: 51113

Answers (5)

Bao Le
Bao Le

Reputation: 17507

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.

Upvotes: 8

ibai
ibai

Reputation: 1318

The official documentation provides a native way to achieve that.

Using Gradle 8 + adding a few extra things.

tasks.register('uberJar', Jar) {
    // For the newest gradle versions
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    // Manifest details
    manifest {
        attributes 'Main-Class': 'com.example.Foo'
    }

    // Suffix for the generated jar
    archiveClassifier = 'uber'

    // Include source files + resources (e.g. logback.xml)
    from sourceSets.main.output

    // Include dependencies
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
}

Upvotes: 1

boechat107
boechat107

Reputation: 1724

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

Upvotes: 5

tim_yates
tim_yates

Reputation: 171114

Have you tried the fatjar example in the gradle cookbook?

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

Upvotes: 38

missingfaktor
missingfaktor

Reputation: 92056

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.

Upvotes: 42

Related Questions