Setting up a Maven Build

    Table of contents:

    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

    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.

    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:

    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>

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

    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:

    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. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    11. <kotlin.version>1.3.61</kotlin.version>
    12. <ktor.version>1.3.0</ktor.version>
    13. <junit.version>4.12</junit.version>
    14. </properties>
    15. <dependencies>
    16. <dependency>
    17. <groupId>org.jetbrains.kotlin</groupId>
    18. <artifactId>kotlin-stdlib</artifactId>
    19. <version>${kotlin.version}</version>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.jetbrains.kotlin</groupId>
    23. <artifactId>kotlin-test-junit</artifactId>
    24. <version>${kotlin.version}</version>
    25. <scope>test</scope>
    26. </dependency>
    27. <dependency>
    28. <groupId>junit</groupId>
    29. <artifactId>junit</artifactId>
    30. <version>${junit.version}</version>
    31. <scope>test</scope>
    32. </dependency>
    33. <dependency>
    34. <groupId>io.ktor</groupId>
    35. <artifactId>ktor-server-netty</artifactId>
    36. <version>${ktor.version}</version>
    37. </dependency>
    38. </dependencies>
    39. <build>
    40. <sourceDirectory>src/main/kotlin</sourceDirectory>
    41. <testSourceDirectory>src/test/kotlin</testSourceDirectory>
    42. <plugin>
    43. <groupId>org.jetbrains.kotlin</groupId>
    44. <artifactId>kotlin-maven-plugin</artifactId>
    45. <version>${kotlin.version}</version>
    46. <executions>
    47. <execution>
    48. <id>compile</id>
    49. <phase>compile</phase>
    50. <goals>
    51. <goal>compile</goal>
    52. </goals>
    53. </execution>
    54. <execution>
    55. <id>test-compile</id>
    56. <phase>test-compile</phase>
    57. <goals>
    58. <goal>test-compile</goal>
    59. </goals>
    60. </execution>
    61. </executions>
    62. <configuration>
    63. <jvmTarget>1.8</jvmTarget>
    64. <args>
    65. <arg>-Xcoroutines=enable</arg>
    66. </args>
    67. </configuration>
    68. </plugin>
    69. </plugins>
    70. </build>
    71. <repositories>
    72. <repository>
    73. <id>jcenter</id>
    74. <url>http://jcenter.bintray.com</url>
    75. </repository>
    76. </project>

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