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.xmlfile (without Ktor) - Add Ktor dependencies and configure build settings
- Choose your engine and configure it
- Final
pom.xml(with Ktor) - Configure logging
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">
4. <modelVersion>4.0.0</modelVersion>
6. <groupId>org.jetbrains</groupId>
7. <artifactId>sample</artifactId>
8. <version>1.0-SNAPSHOT</version>
9. <packaging>jar</packaging>
11. <name>org.jetbrains sample</name>
13. <properties>
14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15. <kotlin.version>1.3.61</kotlin.version>
16. <junit.version>4.12</junit.version>
17. </properties>
19. <dependencies>
20. <dependency>
21. <groupId>org.jetbrains.kotlin</groupId>
22. <artifactId>kotlin-stdlib</artifactId>
23. <version>${kotlin.version}</version>
24. </dependency>
25. <dependency>
26. <groupId>org.jetbrains.kotlin</groupId>
27. <artifactId>kotlin-test-junit</artifactId>
28. <version>${kotlin.version}</version>
29. <scope>test</scope>
30. </dependency>
31. <dependency>
32. <groupId>junit</groupId>
33. <artifactId>junit</artifactId>
34. <version>${junit.version}</version>
35. <scope>test</scope>
36. </dependency>
37. </dependencies>
39. <build>
40. <sourceDirectory>src/main/kotlin</sourceDirectory>
41. <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. </plugin>
65. </plugins>
66. </build>
67. </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">
5. <modelVersion>4.0.0</modelVersion>
7. <groupId>org.jetbrains</groupId>
8. <artifactId>sample</artifactId>
9. <version>1.0-SNAPSHOT</version>
10. <packaging>jar</packaging>
12. <name>org.jetbrains sample</name>
14. <properties>
15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16. <kotlin.version>1.3.61</kotlin.version>
17. <ktor.version>1.3.0</ktor.version>
18. <junit.version>4.12</junit.version>
19. </properties>
21. <dependencies>
22. <dependency>
23. <groupId>org.jetbrains.kotlin</groupId>
24. <artifactId>kotlin-stdlib</artifactId>
25. <version>${kotlin.version}</version>
26. </dependency>
27. <dependency>
28. <groupId>org.jetbrains.kotlin</groupId>
29. <artifactId>kotlin-test-junit</artifactId>
30. <version>${kotlin.version}</version>
31. <scope>test</scope>
32. </dependency>
33. <dependency>
34. <groupId>junit</groupId>
35. <artifactId>junit</artifactId>
36. <version>${junit.version}</version>
37. <scope>test</scope>
38. </dependency>
40. <dependency>
41. <groupId>io.ktor</groupId>
42. <artifactId>ktor-server-netty</artifactId>
43. <version>${ktor.version}</version>
44. </dependency>
46. </dependencies>
48. <build>
49. <sourceDirectory>src/main/kotlin</sourceDirectory>
50. <testSourceDirectory>src/test/kotlin</testSourceDirectory>
52. <plugins>
53. <plugin>
54. <groupId>org.jetbrains.kotlin</groupId>
55. <artifactId>kotlin-maven-plugin</artifactId>
56. <version>${kotlin.version}</version>
57. <executions>
58. <execution>
59. <id>compile</id>
60. <phase>compile</phase>
61. <goals>
62. <goal>compile</goal>
63. </goals>
64. </execution>
65. <execution>
66. <id>test-compile</id>
67. <phase>test-compile</phase>
68. <goals>
69. <goal>test-compile</goal>
70. </goals>
71. </execution>
72. </executions>
73. <configuration>
74. <jvmTarget>1.8</jvmTarget>
75. <args>
76. <arg>-Xcoroutines=enable</arg>
77. </args>
78. </configuration>
79. </plugin>
80. </plugins>
81. </build>
83. <repositories>
84. <repository>
85. <id>jcenter</id>
86. <url>http://jcenter.bintray.com</url>
87. </repository>
88. </repositories>
89. </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.
