#development #gradle #java #kotlin

When you build a jar file using the ./gradlew jar command, it only includes the application classes in the jar file. Any dependency jar will not be in there and will need to be included in order for the app to be able to run.

While that's convenient in some instances, there are many times where having a single jar with all the dependencies bundled in there would be much easier. Luckily, there's a plugin for gradle which can do this called Gradle Shadow.

To use it, first add it as a plugin in your Gradle file (we are using Gradle 7.0 here):

1plugins {
2    kotlin("jvm") version "1.5.0"
3    id("com.github.johnrengelman.shadow") version "7.0.0"
4    application

You do need to check the version of Gradle Shadow you need to use for your version of gradle.

That's all it takes to install it in your project.

To create the fat jar, you execute:

1$ ./gradlew shadowJar     
42 actionable tasks: 2 up-to-date

The resulting jar file can be found under build/libs/*-all.jar.