Using 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:

  1. <properties>
  2. <kotlin.version>1.4.21</kotlin.version>
  3. </properties>

Dependencies

Kotlin has an extensive standard library that can be used in your applications. Configure the following dependency in the pom file:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.jetbrains.kotlin</groupId>
  4. <artifactId>kotlin-stdlib</artifactId>
  5. <version>${kotlin.version}</version>
  6. </dependency>
  7. </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 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.

Compiling Kotlin only source code

To compile source code, specify the source directories in the tag:

  1. <build>
  2. <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
  3. <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
  4. </build>

The Kotlin Maven Plugin needs to be referenced to compile the sources:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.jetbrains.kotlin</groupId>
  5. <artifactId>kotlin-maven-plugin</artifactId>
  6. <version>${kotlin.version}</version>
  7. <executions>
  8. <execution>
  9. <id>compile</id>
  10. <goals>
  11. <goal>compile</goal>
  12. </goals>
  13. </execution>
  14. <execution>
  15. <id>test-compile</id>
  16. <goals>
  17. <goal>test-compile</goal>
  18. </goals>
  19. </execution>
  20. </executions>
  21. </plugin>
  22. </plugins>
  23. </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:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.jetbrains.kotlin</groupId>
  5. <artifactId>kotlin-maven-plugin</artifactId>
  6. <version>${kotlin.version}</version>
  7. <executions>
  8. <execution>
  9. <id>compile</id>
  10. <goals>
  11. <goal>compile</goal>
  12. </goals>
  13. <configuration>
  14. <sourceDirs>
  15. <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
  16. <sourceDir>${project.basedir}/src/main/java</sourceDir>
  17. </sourceDirs>
  18. </configuration>
  19. </execution>
  20. <execution>
  21. <id>test-compile</id>
  22. <goals> <goal>test-compile</goal> </goals>
  23. <configuration>
  24. <sourceDirs>
  25. <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
  26. <sourceDir>${project.basedir}/src/test/java</sourceDir>
  27. </sourceDirs>
  28. </configuration>
  29. </execution>
  30. </executions>
  31. </plugin>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-compiler-plugin</artifactId>
  35. <version>3.5.1</version>
  36. <executions>
  37. <!-- Replacing default-compile as it is treated specially by maven -->
  38. <execution>
  39. <id>default-compile</id>
  40. <phase>none</phase>
  41. </execution>
  42. <!-- Replacing default-testCompile as it is treated specially by maven -->
  43. <execution>
  44. <id>default-testCompile</id>
  45. <phase>none</phase>
  46. </execution>
  47. <execution>
  48. <id>java-compile</id>
  49. <phase>compile</phase>
  50. <goals>
  51. <goal>compile</goal>
  52. </goals>
  53. </execution>
  54. <execution>
  55. <id>java-test-compile</id>
  56. <phase>test-compile</phase>
  57. <goals>
  58. <goal>testCompile</goal>
  59. </goals>
  60. <configuration>
  61. <skip>${maven.test.skip}</skip>
  62. </configuration>
  63. </execution>
  64. </executions>
  65. </plugin>
  66. </plugins>
  67. </build>

Incremental compilation

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:

  1. <properties>
  2. <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
  3. </properties>

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:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.6</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <addClasspath>true</addClasspath>
  9. <mainClass>${main.class}</mainClass>
  10. </manifest>
  11. </archive>
  12. </configuration>
  13. </plugin>

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:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. <version>2.6</version>
  5. <executions>
  6. <execution>
  7. <id>make-assembly</id>
  8. <phase>package</phase>
  9. <goals> <goal>single</goal> </goals>
  10. <configuration>
  11. <archive>
  12. <manifest>
  13. <mainClass>${main.class}</mainClass>
  14. </manifest>
  15. </archive>
  16. <descriptorRefs>
  17. <descriptorRef>jar-with-dependencies</descriptorRef>
  18. </descriptorRefs>
  19. </configuration>
  20. </execution>
  21. </executions>
  22. </plugin>

This self-contained jar file can be passed directly to a JRE to run your application:

  1. java -jar target/mymodule-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Specifying compiler options

Additional options and arguments for the compiler can be specified as tags under the <configuration> element of the Maven plugin node:

  1. <plugin>
  2. <groupId>org.jetbrains.kotlin</groupId>
  3. <artifactId>kotlin-maven-plugin</artifactId>
  4. <version>${kotlin.version}</version>
  5. <executions>...</executions>
  6. <configuration>
  7. <nowarn>true</nowarn> <!-- Disable warnings -->
  8. <args>
  9. <arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations -->
  10. ...
  11. </args>
  12. </configuration>
  13. </plugin>

Many of the options can also be configured through properties:

  1. <project ...>
  2. <properties>
  3. <kotlin.compiler.languageVersion>1.0</kotlin.compiler.languageVersion>
  4. </properties>
  5. </project>

The following attributes are supported:

Attributes common for JVM and JS

NameProperty nameDescriptionPossible valuesDefault value
nowarn Generate no warningstrue, falsefalse
languageVersionkotlin.compiler.languageVersionProvide source compatibility with the specified version of Kotlin“1.2 (DEPRECATED)”, “1.3”, “1.4”, “1.5 (EXPERIMENTAL)” 
apiVersionkotlin.compiler.apiVersionAllow using declarations only from the specified version of bundled libraries“1.2 (DEPRECATED)”, “1.3”, “1.4”, “1.5 (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 []

Attributes specific for JVM

NameProperty nameDescriptionPossible valuesDefault value
jvmTargetkotlin.compiler.jvmTargetTarget version of the generated JVM bytecode“1.6”, “1.8”, “9”, “10”, “11”, “12”, “13”, “14”, “15”“1.6”
jdkHomekotlin.compiler.jdkHomeInclude a custom JDK from the specified location into the classpath instead of the default JAVA_HOME  

Attributes specific for JS

NameProperty nameDescriptionPossible valuesDefault value
outputFile Destination *.js file for the compilation result  
metaInfo Generate .meta.js and .kjsm files with metadata. Use to create a librarytrue, falsetrue
sourceMap Generate source maptrue, falsefalse
sourceMapEmbedSources Embed source files into source map“never”, “always”, “inlining”“inlining”
sourceMapPrefix Add the specified prefix to paths in the source map  
moduleKind The kind of JS module generated by the compiler“umd”, “commonjs”, “amd”, “plain”“umd”

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.

Examples

An example Maven project can be downloaded directly from the GitHub repository