Maven is a build system that you can use to build and manage any Java-based project.
The kotlin-maven-plugin compiles Kotlin sources and modules. Currently, only Maven v3 is supported.
In your pom.xml
file, define the version of Kotlin you want to use in the kotlin.version
property:
<properties>
<kotlin.version>%kotlinVersion%</kotlin.version>
</properties>
To use JDK 17, in your .mvn/jvm.config
file, add:
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.io=ALL-UNNAMED
By default, the mavenCentral
repository is available for all Maven projects. To access artifacts in other repositories,
specify the ID and URL of each repository in the <repositories>
element:
<repositories>
<repository>
<id>spring-repo</id>
<url>https://repo.spring.io/release</url>
</repository>
</repositories>
If you declare
mavenLocal()
as a repository in a Gradle project, you may experience problems when switching between Gradle and Maven projects. For more information, see Declare repositories.
{type="note"}
Kotlin has an extensive standard library that can be used in your applications.
To use the standard library in your project, add the following dependency to your pom.xml
file:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
If you're targeting JDK 7 or 8 with Kotlin versions older than:
- 1.8, use
kotlin-stdlib-jdk7
orkotlin-stdlib-jdk8
, respectively.- 1.2, use
kotlin-stdlib-jre7
orkotlin-stdlib-jre8
, respectively.
{type="note"}
If your project uses Kotlin reflection
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.
To compile source code, specify the source directories in the <build>
tag:
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
</build>
The Kotlin Maven Plugin needs to be referenced to compile the sources:
<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>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Starting from Kotlin 1.8.20, you can replace the whole <executions>
element above with <extensions>true</extensions>
.
Enabling extensions automatically adds the compile
, test-compile
, kapt
, and test-kapt
executions to your build,
bound to their appropriate lifecycle phases.
If you need to configure an execution, you need to specify its ID. You can find an example of this in the next section.
If several build plugins overwrite the default lifecycle and you have also enabled the
extensions
option, the last plugin in the<build>
section has priority in terms of lifecycle settings. All earlier changes to lifecycle settings are ignored.
{type="note"}
To compile projects that include Kotlin and Java source code, invoke the Kotlin compiler before the Java compiler.
In Maven terms it means that kotlin-maven-plugin
should be run before maven-compiler-plugin
using the following method,
making 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>
<extensions>true</extensions> <!-- You can set this option
to automatically take information about lifecycles -->
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal> <!-- You can skip the <goals> element
if you enable extensions for the plugin -->
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal> <!-- You can skip the <goals> element
if you enable extensions for the plugin -->
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</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>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
To make your builds faster, you can enable incremental compilation by adding 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.
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:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To create a self-contained JAR file containing the code from your module along with its dependencies, 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:
<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:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<extensions>true</extensions> <!-- If you want to enable automatic addition of executions to your build -->
<executions>...</executions>
<configuration>
<nowarn>true</nowarn> <!-- Disable warnings -->
<args>
<arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations -->
...
</args>
</configuration>
</plugin>
Many of the options can also be configured through properties:
<project ...>
<properties>
<kotlin.compiler.languageVersion>1.9</kotlin.compiler.languageVersion>
</properties>
</project>
The following attributes are supported:
Name | Property name | Description | Possible values | Default value |
---|---|---|---|---|
nowarn |
Generate no warnings | true, false | false | |
languageVersion |
kotlin.compiler.languageVersion | Provide source compatibility with the specified version of Kotlin | "1.3" (DEPRECATED), "1.4" (DEPRECATED), "1.5", "1.6", "1.7", "1.8", "1.9" (EXPERIMENTAL) | |
apiVersion |
kotlin.compiler.apiVersion | Allow using declarations only from the specified version of bundled libraries | "1.3" (DEPRECATED), "1.4" (DEPRECATED), "1.5", "1.6", "1.7", "1.8", "1.9" (EXPERIMENTAL) | |
sourceDirs |
The directories containing the source files to compile | The project source roots | ||
compilerPlugins |
Enabled compiler plugins | [] | ||
pluginOptions |
Options for compiler plugins | [] | ||
args |
Additional compiler arguments | [] | ||
jvmTarget |
kotlin.compiler.jvmTarget |
Target version of the generated JVM bytecode | "1.8", "9", "10", ..., "21" | "%defaultJvmTargetVersion%" |
jdkHome |
kotlin.compiler.jdkHome |
Include a custom JDK from the specified location into the classpath instead of the default JAVA_HOME |
To use a Kotlin Bill of Materials (BOM),
write a dependency on kotlin-bom
:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>%kotlinVersion%</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The standard Javadoc generation plugin (maven-javadoc-plugin
) doesn't support Kotlin code. To generate documentation
for Kotlin projects, use Dokka. Dokka supports mixed-language projects and can
generate output in multiple formats, including standard Javadoc. For more information about how to configure Dokka in
your Maven project, see Maven.