Using Maven
Define the version of Kotlin you want to use via a kotlin.version property:
Dependencies
Kotlin has an extensive standard library that can be used in your applications. Configure the following dependency in the pom file:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
If you’re targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions. Instead of kotlin-stdlib
, use kotlin-stdlib-jdk7
or kotlin-stdlib-jdk8
, depending on your JDK version (for Kotlin 1.1.x use kotlin-stdlib-jre7
and kotlin-stdlib-jre8
as the jdk
counterparts were introduced in 1.2.0).
If your project uses or testing facilities, you need to add the corresponding dependencies as well. The artifact IDs are kotlin-reflect
for the reflection library, and kotlin-test
and kotlin-test-junit
for the testing libraries.
Compiling Kotlin only source code
To compile source code, specify the source directories in the tag:
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
</build>
Compiling Kotlin and Java sources
To compile mixed code applications Kotlin compiler should be invoked before Java compiler. In maven terms that means that kotlin-maven-plugin
should run before maven-compiler-plugin
using the following method. Make sure that the kotlin
plugin comes before the maven-compiler-plugin
in your pom.xml
file:
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
To make your builds faster, you can enable incremental compilation for Maven (supported since Kotlin 1.1.2). In order to do that, define the kotlin.compiler.incremental
property:
<properties>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
</properties>
Alternatively, run your build with the -Dkotlin.compiler.incremental=true
option.
Annotation processing
See the description of (kapt
).
Jar file
To create a small Jar file containing just the code from your module, include the following under build->plugins
in your Maven pom.xml file, where main.class
is defined as a property and points to the main Kotlin or Java class:
Self-contained Jar file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals> <goal>single</goal> </goals>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
This self-contained jar file can be passed directly to a JRE to run your application:
java -jar target/mymodule-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Additional options and arguments for the compiler can be specified as tags under the <configuration>
element of the Maven plugin node:
Many of the options can also be configured through properties:
<project ...>
<properties>
<kotlin.compiler.languageVersion>1.0</kotlin.compiler.languageVersion>
</project>
The following attributes are supported:
Generating documentation
The standard JavaDoc generation plugin () does not support Kotlin code. To generate documentation for Kotlin projects, use ; please refer to the Dokka README for configuration instructions. Dokka supports mixed-language projects and can generate output in multiple formats, including standard JavaDoc.
OSGi
Examples
An example Maven project can be