Ktor 2.0.1 Help

Adding Ktor dependencies

In this topic, we'll show you how to add dependencies required for Ktor to the existing Gradle/Maven project.

Configure the repositories

Before adding Ktor dependencies, you need to configure the repositories for this project:

  • Production

    Production releases of Ktor are available in the Maven central repository. You can declare this repository in your build script as follows:

    repositories { mavenCentral() }
    repositories { mavenCentral() }
  • Early Access Program (EAP)

    To get access to the EAP versions of Ktor, you need to reference the Space repository:

    repositories { maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") } }
    repositories { maven { url "https://maven.pkg.jetbrains.space/public/p/ktor/eap" } }
    <repositories> <repository> <id>ktor-eap</id> <url>https://maven.pkg.jetbrains.space/public/p/ktor/eap</url> </repository> </repositories>

    Note that Ktor EAPs might require the Kotlin dev repository:

    repositories { maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } }
    repositories { maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } }
    <repositories> <repository> <id>ktor-eap</id> <url>https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev</url> </repository> </repositories>

Add dependencies

Core dependencies

Every Ktor application requires at least the following dependencies:

  • ktor-server-core: contains core Ktor functionality.

  • A dependency for an engine (for example, ktor-server-netty).

For different platforms, Ktor provides platform-specific artifacts with suffixes such as -jvm, for example, ktor-server-core-jvm or ktor-server-netty-jvm. Note that Gradle resolves artifacts appropriate for a given platform while Maven doesn't support this capability. This means that for Maven you need to add a platform-specific suffix manually. The dependencies block for a basic Ktor application might look as follows:

dependencies { implementation("io.ktor:ktor-server-core:2.0.1") implementation("io.ktor:ktor-server-netty:2.0.1") }
dependencies { implementation "io.ktor:ktor-server-core:2.0.1" implementation "io.ktor:ktor-server-netty:2.0.1" }
<dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-core-jvm</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-netty-jvm</artifactId> <version>2.0.1</version> </dependency> </dependencies>

Logging dependency

Ktor uses SLF4J API as a facade for various logging frameworks (for example, Logback or Log4j) and allows you to log application events. To learn how to add the required artifacts, see Add logger dependencies.

Plugin dependencies

Plugins that extend Ktor functionality might require additional dependencies. You can learn more from corresponding topics.

Create an entry point for running an application

Running a Ktor server using Gradle/Maven depends on the way used to create a server. You can specify the application main class in one of the following ways:

  • If you use embeddedServer, specify the main class as follows:

    application { mainClass.set("com.example.ApplicationKt") }
    application { mainClass = "com.example.ApplicationKt" }
    <properties> <main.class>com.example.ApplicationKt</main.class> </properties>
  • If you use EngineMain, you need to configure it as the main class. For Netty, it will look as follows:

    application { mainClass.set("io.ktor.server.netty.EngineMain") }
    application { mainClass = "io.ktor.server.netty.EngineMain" }
    <properties> <main.class>io.ktor.server.netty.EngineMain</main.class> </properties>
Last modified: 13 May 2022