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

plugins {
    kotlin("jvm") version "1.5.0"
    id("com.github.johnrengelman.shadow") version "7.0.0"
    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:

$ ./gradlew shadowJar     

BUILD SUCCESSFUL in 773ms
2 actionable tasks: 2 up-to-date

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

Related Posts

  • Upgrading your Gradle project to version 7
  • Auto-accepting terms of service with Gradle build scans
  • Creating a runnable jar using Gradle
  • Using Kotlin and Jsoup to scrape HTML
  • Checking if an Android app is installed via Google Play