Gradle Shadow plugin
The Gradle Shadow plugin allows you to create an executable JAR that includes all code dependencies (fat JAR). In this topic, we'll show you how to generate and run a fat JAR for the engine-main sample project.
Configure the Shadow plugin
To build a Fat JAR, you need to configure the Shadow plugin first:
Open the
build.gradle(.kts)
file and add the plugin to theplugins
block:plugins { id("com.github.johnrengelman.shadow") version "7.0.0" }plugins { id 'com.github.johnrengelman.shadow' version '7.0.0' }Add the
shadowJar
task. Note that the application main class specified for this task depends on the way used to create a server. In the example below,Main-Class
depends on the used engine and looks as follows:io.ktor.server.netty.EngineMain
.tasks { shadowJar { manifest { attributes(Pair("Main-Class", "io.ktor.server.netty.EngineMain")) } } }shadowJar { manifest { attributes 'Main-Class': 'io.ktor.server.netty.EngineMain' } }
Build a Fat JAR
To build a Fat JAR, open the terminal and execute the shadowJar
task created in the previous step. For the fatjar sample project, the command looks as follows:
When this build completes, you should see the fatjar-all.jar
file in the build/libs
directory.
Run the application
To run the built application:
Go to the
build/libs
folder in a terminal.Execute the following command to run the application:
java -jar fatjar-all.jarWait until the following message is shown:
[main] INFO Application - Responding at http://0.0.0.0:8080Open the link in a browser to see a running application: