Maven
Plugin and versions
The kotlin-maven-plugin compiles Kotlin sources and modules. Currently, only Maven v3 is supported.
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. To use the standard library in your project, add the following dependency in the pom file:
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.
Compile Kotlin-only source code
To compile source code, specify the source directories in the <build>
tag:
The Kotlin Maven Plugin needs to be referenced to compile the sources:
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.
Compile Kotlin and Java sources
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:
Incremental compilation
To make your builds faster, you can enable incremental compilation for Maven by defining the kotlin.compiler.incremental
property:
Alternatively, run your build with the -Dkotlin.compiler.incremental=true
option.
Annotation processing
See the description of Kotlin annotation processing tool (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
To create a self-contained Jar file containing the code from your module along with 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:
This self-contained jar file can be passed directly to a JRE to run your application:
Specifying compiler options
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:
The following attributes are supported:
Attributes common to JVM and JS
Name | Property name | Description | Possible values | Default value |
---|---|---|---|---|
| Generate no warnings | true, false | false | |
| 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) | |
| 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) | |
| The directories containing the source files to compile | The project source roots | ||
| Enabled compiler plugins | [] | ||
| Options for compiler plugins | [] | ||
| Additional compiler arguments | [] |
Attributes specific to JVM
Name | Property name | Description | Possible values | Default value |
---|---|---|---|---|
|
| Target version of the generated JVM bytecode | "1.8", "9", "10", ..., "19" | "1.8" |
|
| Include a custom JDK from the specified location into the classpath instead of the default JAVA_HOME |
Attributes specific to JS
Name | Property name | Description | Possible values | Default value |
---|---|---|---|---|
| Destination *.js file for the compilation result | |||
| Generate .meta.js and .kjsm files with metadata. Use to create a library | true, false | true | |
| Generate source map | true, false | false | |
| Embed source files into source map | "never", "always", "inlining" | "inlining" | |
| Add the specified prefix to paths in the source map | |||
| The kind of JS module generated by the compiler | "umd", "commonjs", "amd", "plain" | "umd" |
Using BOM
To use a Kotlin Bill of Materials (BOM), write a dependency on kotlin-bom
:
Generating documentation
The standard Javadoc generation plugin (maven-javadoc-plugin
) does not support Kotlin code. To generate documentation for Kotlin projects, use Dokka; 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
For OSGi support see the Kotlin OSGi page.