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:

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

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

    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 (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

    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

    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:

    1. <project ...>
    2. <properties>
    3. <kotlin.compiler.languageVersion>1.0</kotlin.compiler.languageVersion>
    4. </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