Maven
Maven is a build system that you can use to build and manage any Java-based project.
Configure and enable the plugin
kotlin-maven-plugin
用于编译 Kotlin 源代码与模块,目前只支持 Maven V3。
In your pom.xml
file, define the version of Kotlin you want to use in the kotlin.version
property:
{{ site.data.releases.latest.version }}
To enable kotlin-maven-plugin
, update your pom.xml
file:
kotlin-maven-plugin
org.jetbrains.kotlin
2.1.20
Use JDK 17
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
Declare repositories
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
element:
spring-repo
https://repo.spring.io/release
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.{style="note"}
Set dependencies
Kotlin 有一个广泛的标准库可用于应用程序。
To use the standard library in your project, 在 pom.xml
文件中配置以下依赖关系:
org.jetbrains.kotlin
kotlin-stdlib
${kotlin.version}
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.{style="note"}
如果你的项目使用 Kotlin 反射
或者测试设施,那么你还需要添加相应的依赖项。
其构件 ID 对于反射库是 kotlin-reflect
,对于测试库是 kotlin-test
与 kotlin-test-junit
。
编译只有 Kotlin 的源代码
要编译源代码,请在
标签中指定源代码目录:
${project.basedir}/src/main/kotlin
${project.basedir}/src/test/kotlin
需要引用 Kotlin Maven 插件来编译源代码:
org.jetbrains.kotlin
kotlin-maven-plugin
${kotlin.version}
compile
compile
test-compile
test-compile
Starting from Kotlin 1.8.20, you can replace the whole
element above with
.
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 thesection has priority in terms of lifecycle settings. All earlier changes to lifecycle settings are ignored.
{style="note"}
同时编译 Kotlin 与 Java 源代码
要编译混合代码应用程序,必须在 Java 编译器之前调用 Kotlin 编译器。
按照 Maven 的方式,这意味着应该使用以下方法在 maven-compiler-plugin
之前运行 kotlin-maven-plugin
。
确保 pom.xml
文件中的 kotlin
插件位于 maven-compiler-plugin
之前:
org.jetbrains.kotlin
kotlin-maven-plugin
${kotlin.version}
true
compile
compile
${project.basedir}/src/main/kotlin
${project.basedir}/src/main/java
test-compile
test-compile
${project.basedir}/src/test/kotlin
${project.basedir}/src/test/java
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
default-compile
none
default-testCompile
none
java-compile
compile
compile
java-test-compile
test-compile
testCompile
Enable incremental compilation
为了使构建更快,可以通过添加 kotlin.compiler.incremental
属性来启用增量编译:
true
或者,使用 -Dkotlin.compiler.incremental=true
选项运行构建。
Configure annotation processing
Create JAR file
要创建一个仅包含模块代码的小型 JAR 文件,请在 Maven pom.xml
文件中的 build->plugins
下面包含以下内容,
其中 main.class
定义为一个属性,并指向主 Kotlin 或 Java 类:
org.apache.maven.plugins
maven-jar-plugin
2.6
true
${main.class}
Create a self-contained JAR file
要创建一个独立的(self-contained)JAR 文件,包含模块中的代码及其依赖项,请在 Maven pom.xml
文件中的
build->plugins
下面包含以下内容其中 main.class
定义为一个属性,并指向主 Kotlin 或 Java 类:
org.apache.maven.plugins
maven-assembly-plugin
2.6
make-assembly
package
single
${main.class}
jar-with-dependencies
这个独立的 JAR 文件可以直接传给 JRE 来运行应用程序:
java -jar target/mymodule-0.0.1-SNAPSHOT-jar-with-dependencies.jar
指定编译器选项
可以将额外的编译器选项与参数指定为 Maven 插件节点的
元素下的标签
:
org.jetbrains.kotlin
kotlin-maven-plugin
${kotlin.version}
true
……
true
-Xjsr305=strict
...
许多选项还可以通过属性来配置:
2.1
支持以下属性:
JVM 特有的属性
名称 | 属性名 | 描述 | 可能的值 | 默认值 |
---|---|---|---|---|
nowarn |
不生成警告 | true、 false | false | |
languageVersion |
kotlin.compiler.languageVersion |
提供与指定语言版本源代码兼容性 | "1.8"、 "1.9"、 "2.0"、 "2.1"、 "2.2"(实验性的) | |
apiVersion |
kotlin.compiler.apiVersion |
只允许使用来自捆绑库的指定版本中的声明 | "1.8"、 "1.9"、 "2.0"、 "2.1"、 "2.2"(实验性的) | |
sourceDirs |
包含要编译源文件的目录 | 该项目源代码根目录 | ||
compilerPlugins |
启用编译器插件 | [] | ||
pluginOptions |
编译器插件的选项 | [] | ||
args |
额外的编译器参数 | [] | ||
jvmTarget |
kotlin.compiler.jvmTarget |
生成的 JVM 字节码的目标版本 | "1.8"、 "9"、 "10"、……、 "20"、 "23" | "1.8" |
jdkHome |
kotlin.compiler.jdkHome |
Include a custom JDK from the specified location into the classpath instead of the default JAVA_HOME |
Use BOM
To use a Kotlin Bill of Materials (BOM),
write a dependency on kotlin-bom
:
org.jetbrains.kotlin
kotlin-bom
2.1.20
pom
import
生成文档
标准的 Javadoc 生成插件(maven-javadoc-plugin
)不支持 Kotlin 代码。要生成 Kotlin
项目的文档,请使用 Dokka。 Dokka 支持混合语言项目,并且可以生成多种格式的输出,包括标准 Javadoc。 For more information about how to configure Dokka in
your Maven project, see Maven.