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:
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:
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>