Ktor 2.1.3 Help

Creating fat JARs using the Maven Assembly plugin

The Maven Assembly plugin provides the ability to combine project output into a single distributable archive that contains dependencies, modules, site documentation, and other files.

Configure the Assembly plugin

To build an assembly, you need to configure the Assembly plugin first:

  1. Go to the pom.xml file and make sure the main application class is specified:

    <properties> <main.class>com.example.ApplicationKt</main.class> </properties>

    If you use EngineMain without the explicit main function, the application's main class depends on the used engine and might look as follows: io.ktor.server.netty.EngineMain.

  2. Add maven-assembly-plugin to the plugins block as follows:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>${main.class}</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>assemble-all</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>

Build an assembly

To build an assembly for the application, open the terminal and execute the following command:

mvn package

When this build completes, you should see the ktor-maven-sample-0.0.1-jar-with-dependencies.jar file in the target directory.

Run the application

To run the built application:

  1. Open the terminal and execute the following command to run the application:

    java -jar target/ktor-maven-sample-0.0.1-jar-with-dependencies.jar
  2. 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: 20 July 2022