# Configure a Maven project When you introduce Kotlin to your existing Java Maven project or create a new Kotlin Maven project, you need to add the Kotlin Maven plugin that compiles Kotlin sources and modules. Currently, only Maven v3 is supported. Note: If you plan to move your project to Maven 4, use the [mvnup](https://maven.apache.org/tools/mvnup.html) Maven upgrade tool to migrate your build automatically. Learn more about migration in the [Starting with Maven 4](https://maven.apache.org/guides/mini/guide-migration-to-mvn4.html) guide. ## Automatic configuration You can simplify Maven configuration in both mixed Java-Kotlin projects and in pure Kotlin projects using the `` option. This approach saves you time because you don't need to configure the Maven compiler plugin. To apply the Kotlin Maven plugin with ``, update your `pom.xml` build file as follows: 1. In the `` section, define the target versions of Kotlin and the JVM: ```XML 17 2.4.21 ``` 2. In the `` section, add the Kotlin Maven plugin with the enabled `` option: ```XML org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} true ``` The `` option: * Registers `src/main/kotlin` and `src/test/kotlin` directories as source roots if they already exist but are not specified in the plugin configuration. * Adds the [kotlin-stdlib dependency](maven-set-dependencies.html#dependency-on-the-standard-library) if it's not already defined in the project. * Adds `compile`, `test-compile`, `kapt`, and `test-kapt` executions to your build, bound to their appropriate [lifecycle phases](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html). So you don't need to manually set up the `` section with `` and `` for `kapt`, Kotlin's `compile`, and Java's `compile` executions to ensure they run in the correct order. * [Automatically aligns the JVM target version with the Java compiler version configured in the project.](#jvm-target-version) If you have a mixed Java and Kotlin project, the configuration ensures that: * Kotlin code is compiled first. * Java code is compiled after Kotlin and can reference Kotlin classes. * Default Maven behavior doesn't override the plugin order. The extension configuration replaces the whole `` section. If you need to configure an execution, see an example in [Compile Kotlin and Java sources](#compile-kotlin-and-java-sources). Note: If several build plugins overwrite the default lifecycle, and you have also enabled the `` option, the last plugin in the `` section has priority for lifecycle settings. All earlier changes to lifecycle settings are ignored. Currently, the default version of the Maven compiler plugin used with `` is 3.10.1. You can set a different version separately: ```XML org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} true org.apache.maven.plugins maven-compiler-plugin 3.15.0 ``` ### JVM target version The `` option ensures that the Kotlin and Maven compilers target the same bytecode version. The Kotlin Maven plugin automatically resolves the JVM target version in the following priority order: ```MERMAID graph TD A["Priority 1
kotlin.compiler.jdkRelease
or kotlin.compiler.jvmTarget"] B["Priority 2
maven.compiler.release"] C["Priority 3
maven.compiler.target"] A --> B B --> C ``` #### Kotlin compiler versions The version set in `kotlin.compiler.jdkRelease` or `kotlin.compiler.jvmTarget` property takes priority if either is defined in the project. Keep in mind that these Kotlin compiler options behave differently: | Kotlin compiler option |Controls bytecode version of the output |Limits API to specified JDK | ------------------------------------------------------------------------------------------------ | `kotlin.compiler.jvmTarget` |Yes |No restrictions on JDK APIs in your code | | `kotlin.compiler.jdkRelease` |Yes |Yes − only specific API version is allowed (equivalent to Java's `--release` compiler option) | Note: Do not set different JDK options for `kotlin.compiler.jdkRelease` and `kotlin.compiler.jvmTarget` at the same time. Otherwise, you'll get an error. #### Maven compiler versions * If neither the `kotlin.compiler.jdkRelease` nor the `kotlin.compiler.jvmTarget` option is set, the plugin takes the `maven.compiler.release` version. The `maven.compiler.release` version can be defined either as a project property or within the `maven-compiler-plugin` configuration. * If the Maven release version isn't set, the plugin takes the `maven.compiler.target` version. It can be defined either as a project property or within the `maven-compiler-plugin` configuration. Keep in mind that `target` and `release` options of the Maven compiler behave differently: | Maven compiler option |Sets Kotlin's `jvmTarget` |Sets Kotlin's `jdkRelease` |Limits API to specified JDK | ------------------------------------------------------------------------------------------------------------- | `maven.compiler.target` |Yes |No |No − the build's JDK classpath stays visible | | `maven.compiler.release` |Yes |Yes |Yes − to the specific API version only | Note: The `` option only checks project-level properties and the global `maven-compiler-plugin` configuration. It doesn't check the configurations defined in the plugin's `` section. ## Manual configuration Without enabling `` in the Kotlin Maven plugin, you need to configure the project manually to ensure that the source code compiles correctly. You can set up your Maven project to compile a combination of [Java and Kotlin sources](#compile-kotlin-and-java-sources) or [Kotlin-only sources](#compile-kotlin-only-sources). ### Compile Kotlin and Java sources To compile a project with both Kotlin and Java source files, ensure that the Kotlin compiler runs before the Java compiler. The Java compiler can't see Kotlin declarations until they are compiled into `.class` files. If your Java code uses Kotlin classes, those classes must be compiled first to avoid `cannot find symbol` errors. Maven determines plugin execution order based on two main factors: * The order of plugin declarations in the `pom.xml` file. * Built-in default executions, such as `default-compile` and `default-testCompile`, which always run before user-defined executions, regardless of their position in the `pom.xml` file. To control the execution order: * Declare `kotlin-maven-plugin` before `maven-compiler-plugin`. * Disable the Java compiler plugin's default executions. * Add custom executions to control compile phases explicitly. Note: You can use the special `none` phase in Maven to disable a default execution. To apply the Kotlin Maven plugin, update your `pom.xml` build file as follows: ```XML org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} kotlin-compile compile compile ${project.basedir}/src/main/kotlin ${project.basedir}/src/main/java kotlin-test-compile test-compile test-compile ${project.basedir}/src/test/kotlin ${project.basedir}/src/test/java org.apache.maven.plugins maven-compiler-plugin 3.15.0 default-compile none default-testCompile none java-compile compile compile java-test-compile test-compile testCompile ``` This configuration ensures that: * Kotlin code is compiled first. * Java code is compiled after Kotlin and can reference Kotlin classes. * Default Maven behavior doesn't override the plugin order. For more details on how Maven handles plugin executions, see [Guide to default plugin execution IDs](https://maven.apache.org/guides/mini/guide-default-execution-ids.html) in the official Maven documentation. ### Compile Kotlin-only sources To compile a project with just Kotlin source files, declare source roots and configure the Kotlin Maven plugin: 1. Specify the source directories in the `` section: ```XML src/main/kotlin src/test/kotlin ``` 2. Ensure that the Kotlin Maven plugin is applied: ```XML org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} compile compile test-compile test-compile ``` ### Set JDK version Kotlin supports [Maven Toolchains](https://maven.apache.org/guides/mini/guide-using-toolchains.html) that help you manage the JDK version in your build. If you configure the `maven-toolchains-plugin` in your build, you can specify the JDK used for Kotlin compilation, independent of the JVM version running Maven (set in the `JAVA_HOME` environment variable). The Kotlin Maven plugin then automatically picks up the selected JDK toolchain and passes the JDK home directory to the compiler using the [-jdk-home option](compiler-reference.html#jdk-home-path). This allows you to configure a single toolchain that controls the JDK used across all plugins in the build, including Kotlin compilation. For example: ```XML org.apache.maven.plugins maven-toolchains-plugin 3.2.0 toolchain 21 ``` You can configure the JDK used for Kotlin compilation in multiple ways. The JDK version is resolved in the following priority order: ```MERMAID graph TD A["Priority 1
jdkHome option of the kotlin-maven-plugin"] B["Priority 2
JDK version set in the
maven-toolchains-plugin"] C["Priority 3
JAVA_HOME version"] A --> B B --> C ``` * The JDK version set in the `jdkHome` option of the `kotlin-maven-plugin` configuration always takes precedence over the toolchain version. * The JDK version in the `maven-toolchains-plugin` overrides the JDK version set in the `JAVA_HOME` environment variable. You can also use a plugin-specific `` option to directly set the JDK version in the toolchain of the `kotlin-maven-plugin`. Compared to using the `maven-toolchains-plugin`, this parameter only affects Kotlin compilation and has no impact on other plugins in the build. The JDK version for the toolchain (set in the `maven-toolchains-plugin` or the `` option) only affects the JDK that the Kotlin compiler uses. It doesn't change the version of the generated bytecode. To control the bytecode version, set the [jvmTarget](maven-kotlin-compiler.html#attributes-specific-to-jvm) option explicitly. If you don't set it, the Kotlin Maven plugin takes the value of the [jdkRelease](maven-kotlin-compiler.html#attributes-specific-to-jvm) option or falls back to the default `1.8` if `jdkRelease` isn't set either. Note: Currently, setting up the `maven-toolchains-plugin` to use a specific JDK version [does not affect the kapt and test-kapt goals](https://youtrack.jetbrains.com/issue/KT-79897) of the `kotlin-maven-plugin`. Instead, set the necessary version in the `JAVA_HOME` environment variable. ## Configure Java modules (JPMS) The Kotlin Maven plugin supports the [Java Platform Module System (JPMS)](https://dev.java/learn/modules/), so you can compile Kotlin code alongside a `module-info.java` descriptor and consume the resulting module like other Java modules. You don't need any extra JPMS-specific options in the build file. Just configure the Kotlin compiler before Maven. When a `module-info.java` descriptor is present, the Kotlin compiler uses it as a source file and compiles against its module path instead of the classpath. The Kotlin compiler reads the descriptor to resolve the module graph, and the Maven compiler then compiles it into a `module-info.class` file. To configure a Java module, create the `module-info.java` file in the `${project.basedir}/src/main/java` directory. In the module descriptor, declare all the dependencies that your module requires and the packages it exports. For example: ```JAVA module org.example.myapp { requires transitive kotlin.stdlib; requires java.net.http; exports org.example.myapp; } ``` Keep in mind that: * Your Java module can only use what you declare. Since compilation uses the module path instead of the classpath, the descriptor should include all dependencies that your Kotlin code uses: the standard library, JDK modules (except for `java.base`), and other libraries. Otherwise, you may get `Unresolved reference` errors. * For a module, the package name in Kotlin files must match the package name from `module-info.java` to avoid the `Package is empty or does not exist` build failures. * The `pom.xml` build file should be configured so that [Kotlin is compiled before Java](#compile-kotlin-and-java-sources). If you use [automatic project configuration](#automatic-configuration), the `` option already ensures this. ## What's next? [Set dependencies in your Kotlin Maven project](maven-set-dependencies.html)