Ktor 2.3.10 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 Ktor 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.3.10") implementation("io.ktor:ktor-server-netty:2.3.10") }
dependencies { implementation "io.ktor:ktor-server-core:2.3.10" implementation "io.ktor:ktor-server-netty:2.3.10" }
<dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-core-jvm</artifactId> <version>2.3.10</version> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-netty-jvm</artifactId> <version>2.3.10</version> </dependency> </dependencies>

Plugin dependencies

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

Ensure dependencies consistency

Applying the Ktor Gradle plugin implicitly adds the Ktor BOM dependency and allows you to ensure that all Ktor dependencies are at the same version. In this case, you no longer need to specify a version when depending on Ktor artifacts:

plugins { // ... id("io.ktor.plugin") version "2.3.10" } dependencies { implementation("io.ktor:ktor-server-core") implementation("io.ktor:ktor-server-netty") // ... }
plugins { // ... id "io.ktor.plugin" version "2.3.10" } dependencies { implementation "io.ktor:ktor-server-core" implementation "io.ktor:ktor-server-netty" // ... }

Add 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.

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: 02 April 2024