Setting up a Maven Build

In this guide, we will show you how to create a Maven pom.xml fileand how to configure it to support Ktor.

Table of contents:

Basic Kotlin pom.xml file (without Ktor)

Maven is a build automation tool used primarily for Java projects.It reads project configuration from pom.xml files.Here is a basic pom.xml file for building Kotlin applications:

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.jetbrains</groupId>
  5. <artifactId>sample</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>org.jetbrains sample</name>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <kotlin.version>1.3.61</kotlin.version>
  12. <junit.version>4.12</junit.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.jetbrains.kotlin</groupId>
  17. <artifactId>kotlin-stdlib</artifactId>
  18. <version>${kotlin.version}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.jetbrains.kotlin</groupId>
  22. <artifactId>kotlin-test-junit</artifactId>
  23. <version>${kotlin.version}</version>
  24. <scope>test</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>junit</groupId>
  28. <artifactId>junit</artifactId>
  29. <version>${junit.version}</version>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <sourceDirectory>src/main/kotlin</sourceDirectory>
  35. <testSourceDirectory>src/test/kotlin</testSourceDirectory>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.jetbrains.kotlin</groupId>
  39. <artifactId>kotlin-maven-plugin</artifactId>
  40. <version>${kotlin.version}</version>
  41. <executions>
  42. <execution>
  43. <id>compile</id>
  44. <phase>compile</phase>
  45. <goals>
  46. <goal>compile</goal>
  47. </goals>
  48. </execution>
  49. <execution>
  50. <id>test-compile</id>
  51. <phase>test-compile</phase>
  52. <goals>
  53. <goal>test-compile</goal>
  54. </goals>
  55. </execution>
  56. </executions>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

Add Ktor dependencies and configure build settings

Ktor artifacts are located in a specific repository on bintray.And its core has dependencies on the kotlinx.coroutines library thatcan be found on jcenter.

You have to add both to the repositories block in the pom.xml file:

  1. <repositories>
  2. <repository>
  3. <id>jcenter</id>
  4. <url>https://jcenter.bintray.com</url>
  5. </repository>
  6. </repositories>

Visit Bintray and determine the latest version of ktor.In this case it is 1.3.0.

You have to specify that version in each Ktor artifact reference,and to avoid repetitions, you can specify that version in an extra propertyin the properties block for using it later:

  1. <properties>
  2. <ktor.version>1.3.0</ktor.version>
  3. </properties>

Now you have to add ktor-server-core artifact using the ktor.version you specified:

  1. <dependency>
  2. <groupId>io.ktor</groupId>
  3. <artifactId>ktor-server-core</artifactId>
  4. <version>${ktor.version}</version>
  5. </dependency>

Choose your engine and configure it

Ktor can run in many environments, such as Netty, Jetty or any otherServlet-compatible Application Container such as Tomcat.

This example shows how to configure Ktor with Netty.For other engines see artifacts for a list ofavailable artifacts.

You will add a dependency for ktor-server-netty using thektor.version property you have created. This module providesNetty as a web server and all the required code to run Ktorapplication on top of it:

  1. <dependency>
  2. <groupId>io.ktor</groupId>
  3. <artifactId>ktor-server-netty</artifactId>
  4. <version>${ktor.version}</version>
  5. </dependency>

Final pom.xml (with Ktor)

When you are done, the pom.xml file should look like:

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>org.jetbrains</groupId>
  6. <artifactId>sample</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>org.jetbrains sample</name>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <kotlin.version>1.3.61</kotlin.version>
  13. <ktor.version>1.3.0</ktor.version>
  14. <junit.version>4.12</junit.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.jetbrains.kotlin</groupId>
  19. <artifactId>kotlin-stdlib</artifactId>
  20. <version>${kotlin.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.jetbrains.kotlin</groupId>
  24. <artifactId>kotlin-test-junit</artifactId>
  25. <version>${kotlin.version}</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>junit</groupId>
  30. <artifactId>junit</artifactId>
  31. <version>${junit.version}</version>
  32. <scope>test</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>io.ktor</groupId>
  36. <artifactId>ktor-server-netty</artifactId>
  37. <version>${ktor.version}</version>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <sourceDirectory>src/main/kotlin</sourceDirectory>
  42. <testSourceDirectory>src/test/kotlin</testSourceDirectory>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.jetbrains.kotlin</groupId>
  46. <artifactId>kotlin-maven-plugin</artifactId>
  47. <version>${kotlin.version}</version>
  48. <executions>
  49. <execution>
  50. <id>compile</id>
  51. <phase>compile</phase>
  52. <goals>
  53. <goal>compile</goal>
  54. </goals>
  55. </execution>
  56. <execution>
  57. <id>test-compile</id>
  58. <phase>test-compile</phase>
  59. <goals>
  60. <goal>test-compile</goal>
  61. </goals>
  62. </execution>
  63. </executions>
  64. <configuration>
  65. <jvmTarget>1.8</jvmTarget>
  66. <args>
  67. <arg>-Xcoroutines=enable</arg>
  68. </args>
  69. </configuration>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. <repositories>
  74. <repository>
  75. <id>jcenter</id>
  76. <url>http://jcenter.bintray.com</url>
  77. </repository>
  78. </repositories>
  79. </project>

You can now run mvn package to fetch dependencies and verify everything is set up correctly.

Configure logging

If you want to log application events and useful information,you can read further in the logging page.