Fat JAR

A fat-jar (or uber-jar) archive is a normal jar file single archive packing all the dependencies togetherso it can be run a standalone application directly using Java:

java -jar yourapplication.jar

This is the preferred way for running it in a container like docker, when deploying to herokuor when being reverse-proxied with nginx.

Gradle

When using Gradle, you can use the shadow gradle plugin to generate it. For example,to generate a fat JAR using netty as an engine:

build.gradle

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. }
  5. dependencies {
  6. classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
  7. }
  8. }
  9. apply plugin: 'com.github.johnrengelman.shadow'
  10. apply plugin: 'kotlin'
  11. apply plugin: 'application'
  12. //mainClassName = 'io.ktor.server.netty.DevelopmentEngine' // For versions < 1.0.0-beta-3
  13. mainClassName = 'io.ktor.server.netty.EngineMain' // Starting with 1.0.0-beta-3
  14. // This task will generate your fat JAR and put it in the ./build/libs/ directory
  15. shadowJar {
  16. manifest {
  17. attributes 'Main-Class': mainClassName
  18. }
  19. }

build.gradle.kts

  1. plugins {
  2. application
  3. kotlin("jvm") version "1.3.21"
  4. // Shadow 5.0.0 requires Gradle 5+. Check the shadow plugin manual if you're using an older version of Gradle.
  5. id("com.github.johnrengelman.shadow") version "5.0.0"
  6. }
  7. application {
  8. mainClassName = "io.ktor.server.netty.EngineMain"
  9. }
  10. tasks.withType<Jar> {
  11. manifest {
  12. attributes(
  13. mapOf(
  14. "Main-Class" to application.mainClassName
  15. )
  16. )
  17. }
  18. }

Maven

When using Maven, you can generate a fat JAR archive with the maven-assembly-plugin. For example, to generatea fat JAR using netty as an engine:

pom.xml

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. <version>3.1.0</version>
  5. <configuration>
  6. <descriptorRefs>
  7. <descriptorRef>jar-with-dependencies</descriptorRef>
  8. </descriptorRefs>
  9. <archive>
  10. <manifest>
  11. <addClasspath>true</addClasspath>
  12. <mainClass>io.ktor.server.netty.EngineMain</mainClass>
  13. </manifest>
  14. </archive>
  15. </configuration>
  16. <executions>
  17. <execution>
  18. <id>assemble-all</id>
  19. <phase>package</phase>
  20. <goals>
  21. <goal>single</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>