Ktor 1.6.8 Help

Adding Ktor to an existing Maven project

In this tutorial, we'll show you how to integrate Ktor into the existing Maven project.

Prerequisites

Before starting this tutorial, do the following:

Create a new Maven project

To create a new Maven project, open IntelliJ IDEA, and follow the steps below:

  1. On the Welcome screen, click New Project.

    Otherwise, from the main menu, select File | New | Project.

  2. In the New Project wizard, choose Kotlin from the list on the left and specify the following settings:

    Gradle Project Settings
    • Specify a project's Name.

    • Set Project Template to Application.

    • Select Maven as a build system.

    • Choose the Project JDK from the list.

    Click Next.

  3. On the next wizard page, leave the default settings and click Finish.

    Gradle Project Name

    Wait until IntelliJ IDEA creates and builds a project.

Add Ktor dependencies

After creating an empty project, we are ready to examine a Maven configuration file and add Ktor dependencies to it:

  1. Open the pom.xml file. It should look something like this:

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>mainModule</artifactId> <groupId>me.jetbrains</groupId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mainModule</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <kotlin.code.style>official</kotlin.code.style> <kotlin.compiler.jvmTarget>13</kotlin.compiler.jvmTarget> </properties> <repositories> <repository> <id>mavenCentral</id> <url>https://repo1.maven.org/maven2/</url> </repository> </repositories> <build> <sourceDirectory>src/main/kotlin</sourceDirectory> <testSourceDirectory>src/test/kotlin</testSourceDirectory> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>1.5.31</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test-junit</artifactId> <version>1.5.31</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>1.5.31</version> </dependency> </dependencies> </project>

    This configuration file includes the kotlin-maven-plugin for compiling Kotlin sources. Moreover, it declares the Maven Central repository in the repositories block. Since Ktor dependencies are stored in Maven Central, we don't need to change anything here.

  2. To create a simple Ktor application, we need to add at least the following dependencies:

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

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

    • Logback artifacts for logging.

    The properties and dependencies blocks might look as follows:

    <properties> <ktor.version>1.6.8</ktor.version> <logback_version>1.2.5</logback_version> </properties> <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-core</artifactId> <version>${ktor.version}</version> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-netty</artifactId> <version>${ktor.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback_version}</version> </dependency> </dependencies>

    Note that other engines and plugins that extend Ktor functionality might required additional dependencies. You can learn more from corresponding topics.

  3. The resulting pom.xml file should look as shown below.

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>mainModule</artifactId> <groupId>me.jetbrains</groupId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mainModule</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <kotlin.code.style>official</kotlin.code.style> <kotlin.compiler.jvmTarget>13</kotlin.compiler.jvmTarget> <ktor.version>1.6.8</ktor.version> <logback_version>1.2.5</logback_version> <main.class>io.ktor.server.netty.EngineMain</main.class> </properties> <repositories> <repository> <id>mavenCentral</id> <url>https://repo1.maven.org/maven2/</url> </repository> </repositories> <build> <sourceDirectory>src/main/kotlin</sourceDirectory> <testSourceDirectory>src/test/kotlin</testSourceDirectory> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>1.5.31</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test-junit</artifactId> <version>1.5.31</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>1.5.31</version> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-core</artifactId> <version>${ktor.version}</version> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-netty</artifactId> <version>${ktor.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback_version}</version> </dependency> </dependencies> </project>

Create a server

Now we a ready to add some code for creating a Ktor server and running our application:

  1. Create a com.example package within the src/main/kotlin folder.

  2. In the com.example package, add an empty Application.kt file.

Since Ktor supports two approaches for creating a server, you can choose one of the following:

Create a server using embeddedServer

  • Add the following code to Application.kt to use the embeddedServer function for starting a Ktor server:

    package com.example import io.ktor.application.* import io.ktor.response.* import io.ktor.routing.* import io.ktor.server.engine.* import io.ktor.server.netty.* fun main() { embeddedServer(Netty, port = 8080) { routing { get("/") { call.respondText("Hello, world!") } } }.start(wait = true) }

Create a server using EngineMain

  1. Add the following code to Application.kt to use EngineMain for starting a Ktor server:

    package com.example import io.ktor.application.* import io.ktor.response.* import io.ktor.routing.* fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args) fun Application.module(testing: Boolean = false) { routing { get("/") { call.respondText("Hello, world!") } } }

    The Application.module function is a module loaded by the server. Modules to load should be specified in the application.conf file, which will be created in the next step.

  2. Create the application.conf file in the src/main/resources folder and copy the following configuration:

    ktor { deployment { port = 8080 } application { modules = [ com.example.ApplicationKt.module ] } }

    This file specifies a fully qualified name of the Application.module module to load and sets a listening port to 8080.

Run an application

Now we can run our Ktor application:

  1. To run the application, click the gutter icon next to the main function and choose Run 'ApplicationKt'.

    Run a Ktor application
  2. Wait until Intellij IDEA runs the application. The Run tool window should show the following message:

    [main] INFO Application - Responding at http://0.0.0.0:8080

    This means that the server is ready to accept requests at the http://0.0.0.0:8080 address. You can click this link to open the application in a default browser:

    Ktor app in a browser
Last modified: 12 July 2021