Ktor 1.6.8 Help

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 a Ktor application created in the Adding Ktor to an existing Gradle project topic.

Prerequisites

Before starting this tutorial, clone the ktor-gradle-sample repository.

Configure the Shadow plugin

To build a Fat JAR, you need to configure the Shadow plugin first:

  1. Open the build.gradle(.kts) file and add the plugin to the plugins block:

    plugins { id 'com.github.johnrengelman.shadow' version '7.0.0' }
    plugins { id("com.github.johnrengelman.shadow") version "7.0.0" }
  2. Add the shadowJar task:

    shadowJar { manifest { attributes 'Main-Class': 'com.example.ApplicationKt' } }
    tasks{ shadowJar { manifest { attributes(Pair("Main-Class", "com.example.ApplicationKt")) } } }

    If you use EngineMain without the explicit main function, your Main-Class depends on the used engine and might look as follows: 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:

./gradlew shadowJar
gradlew.bat shadowJar

When this build completes, you should see the ktor-gradle-sample-1.0-SNAPSHOT-all.jar file in the build/libs directory.

Run the application

To run the built application:

  1. Go to the build/libs folder in a terminal.

  2. Execute the following command to run the application:

    java -jar ktor-gradle-sample-1.0-SNAPSHOT-all.jar
  3. Wait until the following message is shown:

    [main] INFO Application - Responding at http://0.0.0.0:8080 You can click the link to open the application in a default browser:

    Ktor app in a browser
Last modified: 06 September 2021