Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanielChuDC/4db68016f4c3211c5cfe55a70a5ceaf2 to your computer and use it in GitHub Desktop.
Save DanielChuDC/4db68016f4c3211c5cfe55a70a5ceaf2 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

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