Gradle 6.5 Create Java Executable Jar with Deps

Hi, I am new to Gradle and have not been able to find a “best-practice” method for building an executable jar with dependencies in the latest version of Gradle. Older methods that I have come across yield compiler warnings or just don’t work. Can someone clarify how to build a single executable jar with at least one external dependency using the latest version of Gradle?

You mean a Uber Jar or Fat Jar? The moment one of your dependencies is signed you get into trouble, you can pack all dependencies in one ZIP archive and have a single (executable) JAR in the root of the archive.

tasks.jar {
    manifest {
        attributes(            
            "Implementation-Title" to project.name,
            "Implementation-Version" to project.version,
            "Class-Path" to configurations.runtimeClasspath.files.map({ "${it.name}" }).joinToString(prefix = "lib/", separator = " lib/"),
            "Main-Class" to "com.foobar.Main"
        )
    }
}

tasks.register<Zip>("dist") {
    dependsOn(tasks["jar"])
    from(tasks.jar.get().archivePath)
    from(configurations.runtimeClasspath) {
        into("lib")
    }
}
1 Like

Thanks for the response. This is definitely what I am looking for. Some inline comments would make this solution even more helpful, but I will take what I can get. Hopefully this will help others facing the same issue. In maven, (at least for me), it is much easier. As far as your question goes, I thought an Uber Jar and Fat Jar were the same thing. Perhaps you can clarify the difference. And as background, creating an executable jar is a nice clean way for me to bundle my Java application into a Docker image.