Building an Uberjar with Gradle

Building an uberjar with Gradle

Have you tried the fatjar example in the gradle cookbook?

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

How do I create an executable fat JAR with Gradle with implementation dependencies?

You can use the following code.

jar {
manifest {
attributes(
'Main-Class': 'com.package.YourClass'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

Be sure to replace com.package.YourClass with the fully qualified class name containing static void main( String args[] ).

This will pack the runtime dependencies. Check the docs if you need more info.

Using Gradle to build a JAR with dependencies

I posted a solution in JIRA against Gradle:

// Include dependent libraries in archive.
mainClassName = "com.company.application.Main"

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

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

Note that mainClassName must appear BEFORE jar {.

How to build uberJar with Quarkus-Gradle-Plugin

The name of that option is "uber-jar".
To set this property you have to start the build like that from command line:

>gradle quarkusBuild --uber-jar

I had some bugs during the build, like that one

Caused by: java.nio.file.NoSuchFileException: /Users/sven/Idea/getting-started/build/getting-started.jar

but in the end the build was successful

Gradle 7 create a fat jar

Use configurations.runtimeClasspath instead of configurations.compileClasspath. compileClasspath contains all libraries required for compilation, thus fastutil is included. runtimeClasspath on the other hand excludes libraries from configuration compileOnly.

Creating an executable fat jar with dependencies (gradle or maven)

You can add a manifest to your task since it is type Jar. Specifying an entrypoint with the Main-Class attribute should make your Jar executable.

task uberJar(type: Jar) {
manifest {
attributes 'Main-Class': 'your.main.class.goes.here'
}
archiveClassifier = 'uber'

from sourceSets.main.output

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


Related Topics



Leave a reply



Submit