There are multiple posts (old and new) with instructions on how to generate a fat jar, this is, a jar file for your application
containing also your application's dependencies. Most solutions I have tried did not work for me, even in a simple Hello World
java application, but I have found one that seems to work as expected.
Here it is:
Create your Gradle java project as usual. Then:
- Adjust your
build.gradle
file to have thejava
andapplication
plugins. - Mark your application dependencies as
implementation
(see thedependencies
section). - Be sure to have a defined
mainClassName
. - Define the
fatJar
as described below. - When running your Gradle tasks, make sure to call the
fatJar
task instead of the normaljar
task.
Here is an example build.gradle
content with all the mentioned tips:
plugins {
id 'java'
id 'application'
}
group 'dev.test'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
implementation 'your.test.dependency.libs:here:1.0.1'
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
mainClassName = 'dev.test.your.App'
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': "${mainClassName}"
}
archiveBaseName = "${rootProject.name}"
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
For the record this does not support multiple dependencies apparently you will receive the "> Entry META-INF/INDEX.LIST is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.4/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details." error if you attempt this, You can fix this as follows.
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } setDuplicatesStrategy(DuplicatesStrategy.INCLUDE) }