Skip to content

ktorio/ktor-build-plugins

Repository files navigation

Ktor Gradle plugin

This plugin simplifies the deployment process of server Ktor applications and provides the following capabilities:

  • Building fat JARs.
  • Dockerizing your applications.

Install the plugin

To install the plugin, add it to the plugins block of your build script:

plugins {
    id("io.ktor.plugin") version "3.0.3"
}

Requirements:

  • Gradle 8.3+
  • JVM 11+

EAP builds

You can also use EAP versions of the plugin published on Space Packages. To do this, consider adding https://maven.pkg.jetbrains.space/public/p/ktor/eap to the list of plugin repositories in the settings.gradle file, which may look like this:

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("https://maven.pkg.jetbrains.space/public/p/ktor/eap")
    }
}

Build a fat JAR

To build and run a fat JAR, use the buildFatJar/runFatJar tasks. Note that a main class should be configured for your application, for example:

// build.gradle.kts
application {
    mainClass.set("com.example.ApplicationKt")
}

After the task is executed, you should see the ***-all.jar file in the build/libs directory. You can optionally configure the name of the fat JAR to be generated:

// build.gradle.kts
ktor {
    fatJar {
        archiveFileName.set("fat.jar")
    }
}

You can find a sample build script here: ktor-fatjar-sample/build.gradle.kts.

Dockerize your application

The following tasks are available for packaging, running, and deploying your application using Docker:

  • buildImage: builds a project's Docker image to a tarball. This task generates a ***.tar file in the build directory. You can load this image to a Docker daemon using the docker load command.
  • runDocker: builds a project's image to a Docker daemon and runs it.
  • publishImageToLocalRegistry: builds and publishes a project's Docker image to a local registry.
  • publishImage: builds and publishes a project's Docker image to an external registry. Note that you need to configure the external registry using the ktor.docker.externalRegistry property for this task.

A sample configuration for Docker-related tasks might look as follows:

// build.gradle.kts
ktor {
    docker {
        jreVersion.set(JreVersion.JRE_17)
        localImageName.set("sample-docker-image")
        imageTag.set("0.0.1-preview")

        externalRegistry.set(
            DockerImageRegistry.dockerHub(
                appName = provider { "ktor-app" },
                username = providers.environmentVariable("DOCKER_HUB_USERNAME"),
                password = providers.environmentVariable("DOCKER_HUB_PASSWORD")
            )
        )
    }
}

You can find a sample build script here: ktor-docker-sample/build.gradle.kts.