このガイドでは、Mavenのpom.xml
ファイルの作成方法とKtorをサポートするための設定方法について説明します。
目次:
pom.xml
ファイル(Ktor設定無し)Mavenは主にJavaプロジェクトに利用される自動ビルドツールです。
Mavenはpom.xml
ファイルからプロジェクトの設定を読み取ります。
以下は基本的なKotlinアプリケーションビルド用の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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.3.70</kotlin.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>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<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>
</plugin>
</plugins>
</build>
</project>
Ktorのアーティファクトはbintrayの特定のレポジトリに配置されています。
そしてそのコアとなる部分はjcenter
にあるkotlinx.coroutines
ライブラリに依存しています。
そのためその両方をpom.xml
ファイルのrepositories
ブロックに追加してやる必要があります。
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
Bintrayサイトを見て、Ktorの最新バージョンを見つけてください。
この場合それは 1.3.2
です.
Ktorのアーティファクトリファレンスからバージョンを指定する必要があり、重複を避けるためそのバージョンをproperties
ブロック内でextraプロパティとして指定する必要があります。
<properties>
<ktor.version>1.3.2</ktor.version>
</properties>
ktor-server-core
のアーティファクトを追加し、あなたが決めたktor.version
を指定します:
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-core</artifactId>
<version>${ktor.version}</version>
</dependency>
Ktorは様々な環境で動作します。例えばNetty、Jetty、そのたServlet互換のあるアプリケーションコンテナ(例:Tomcat)などでです。
以下の例はKtorをNettyで動作させる例です。 その他のエンジンについてはアーティファクトをご覧ください。
ktor-server-netty
への依存を追加しktor.version
プロパティを指定します。
このモジュールはNettyを提供し、Ktorアプリケーションがその上で動作するうえでWebサーバとして動作できるようにします。
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-netty</artifactId>
<version>${ktor.version}</version>
</dependency>
pom.xml
(Ktor設定有り)設定が終わったなら、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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.3.70</kotlin.version>
<ktor.version>1.3.2</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>
<plugins>
<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>
</repositories>
</project>
mvn package
コマンドを実行することで、依存ライブラリの解決や設定の妥当性検証を行うことができます。
アプリケーションイベントやその他有益な情報のログを残したい場合は、loggingページをご覧ください。