Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdavidzapatab/1c974b5422aa5665c59cc6a6a7a91cc9 to your computer and use it in GitHub Desktop.
Save jdavidzapatab/1c974b5422aa5665c59cc6a6a7a91cc9 to your computer and use it in GitHub Desktop.
Generate Jar with dependencies (fatJar) using Gradle

Generate Jar with dependencies (fatJar) using Gradle

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:

Instructions

Create your Gradle java project as usual. Then:

  • Adjust your build.gradle file to have the java and application plugins.
  • Mark your application dependencies as implementation (see the dependencies 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 normal jar 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
}

Credits

Source: Gradle: Part 4, Creating a fat jar using Gradle

@ssi-anik
Copy link

If someone reaches here too, having trouble building fat jar for jfx with gradle, i'd request you to visit this SO link: https://stackoverflow.com/a/57452893/2190689

@GrantWesson
Copy link

GrantWesson commented Nov 26, 2022

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

@DanielChuDC
Copy link

Thanks for the sharing!

I tested this is working

// remember to add your main class => mainClassName = ' org.example.Main'
task fatJar(type: Jar) {

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

    archiveBaseName = "${rootProject.name}"
    tasks.withType(Jar){
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
    }
    from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    //from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } setDuplicatesStrategy(DuplicatesStrategy.INCLUDE) }
    with jar
}

@Zap4ick
Copy link

Zap4ick commented Dec 5, 2023

Found this guide :) Is there a way to pack testImplementation dependencies too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment