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
.
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com</url>
</repository>
</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:
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-core</artifactId>
<version>${ktor.version}</version>
</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
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains</groupId>
<artifactId>sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>org.jetbrains sample</name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.3.61</kotlin.version>
<ktor.version>1.3.0</ktor.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-netty</artifactId>
<version>${ktor.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</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>
<configuration>
<jvmTarget>1.8</jvmTarget>
<args>
<arg>-Xcoroutines=enable</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
</project>
You can now run mvn package
to fetch dependencies and verify everything is set up correctly.