Skip to content

Commit b7eb6f0

Browse files
authored
update: update Compile Kotlin and Java sources in Maven (#4785)
1 parent f03cc72 commit b7eb6f0

File tree

2 files changed

+169
-20
lines changed

2 files changed

+169
-20
lines changed

docs/topics/maven.md

Lines changed: 168 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,30 @@ specify the ID and URL of each repository in the `<repositories>` element:
5656

5757
## Set dependencies
5858

59-
Kotlin has an extensive standard library that can be used in your applications.
59+
To add a dependency on a library, include it in the `<dependencies>` element:
60+
61+
```xml
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.jetbrains.kotlinx</groupId>
65+
<artifactId>kotlinx-serialization-json</artifactId>
66+
<version>%serializationVersion%</version>
67+
</dependency>
68+
</dependencies>
69+
```
70+
71+
### Dependency on the standard library
72+
73+
Kotlin has an extensive standard library that you can use in your applications.
6074
To use the standard library in your project, add the following dependency to your `pom.xml` file:
6175

6276
```xml
6377
<dependencies>
6478
<dependency>
6579
<groupId>org.jetbrains.kotlin</groupId>
6680
<artifactId>kotlin-stdlib</artifactId>
81+
<!-- Uses the kotlin.version property
82+
specified in <properties/>: -->
6783
<version>${kotlin.version}</version>
6884
</dependency>
6985
</dependencies>
@@ -73,13 +89,56 @@ To use the standard library in your project, add the following dependency to you
7389
> * 1.8, use `kotlin-stdlib-jdk7` or `kotlin-stdlib-jdk8`, respectively.
7490
> * 1.2, use `kotlin-stdlib-jre7` or `kotlin-stdlib-jre8`, respectively.
7591
>
76-
{style="note"}
92+
{style="note"}
93+
94+
### Dependencies on test libraries
7795

7896
If your project uses [Kotlin reflection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect.full/index.html)
79-
or testing facilities, you need to add the corresponding dependencies as well.
80-
The artifact IDs are `kotlin-reflect` for the reflection library, and `kotlin-test` and `kotlin-test-junit`
97+
or testing frameworks, add the relevant dependencies.
98+
Use `kotlin-reflect` for the reflection library, and `kotlin-test` and `kotlin-test-junit`
8199
for the testing libraries.
82100

101+
For example:
102+
103+
```xml
104+
<dependencies>
105+
<dependency>
106+
<groupId>org.jetbrains.kotlin</groupId>
107+
<artifactId>kotlin-reflect</artifactId>
108+
<version>${kotlin.version}</version>
109+
</dependency>
110+
</dependencies>
111+
```
112+
113+
### Dependency on a kotlinx library
114+
115+
Depending on the kotlinx library, you can either add the base artifact name or the name with a `-jvm` suffix. Refer to
116+
the library's README file on [klibs.io](https://klibs.io/).
117+
118+
For example, to add a dependency on `kotlinx.coroutines`:
119+
120+
```xml
121+
<dependencies>
122+
<dependency>
123+
<groupId>org.jetbrains.kotlinx</groupId>
124+
<artifactId>kotlinx-coroutines-core</artifactId>
125+
<version>%coroutinesVersion%</version>
126+
</dependency>
127+
</dependencies>
128+
```
129+
130+
To add a dependency on `kotlinx-datetime`:
131+
132+
```xml
133+
<dependencies>
134+
<dependency>
135+
<groupId>org.jetbrains.kotlinx</groupId>
136+
<artifactId>kotlinx-datetime-jvm</artifactId>
137+
<version>%dateTimeVersion%</version>
138+
</dependency>
139+
</dependencies>
140+
```
141+
83142
## Compile Kotlin-only source code
84143

85144
To compile source code, specify the source directories in the `<build>` tag:
@@ -135,38 +194,112 @@ If you need to configure an execution, you need to specify its ID. You can find
135194

136195
## Compile Kotlin and Java sources
137196

138-
To compile projects that include Kotlin and Java source code, invoke the Kotlin compiler before the Java compiler.
139-
In Maven terms it means that `kotlin-maven-plugin` should be run before `maven-compiler-plugin` using the following method,
140-
making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in your `pom.xml` file:
197+
To compile a project with both Kotlin and Java source files, make sure the Kotlin compiler runs before the Java compiler.
198+
The Java compiler can't see Kotlin declarations until they are compiled into `.class` files.
199+
If your Java code uses Kotlin classes, those classes must be compiled first to avoid `cannot find symbol` errors.
200+
201+
Maven determines plugin execution order based on two main factors:
202+
203+
* The order of plugin declarations in the `pom.xml` file.
204+
* Built-in default executions, such as `default-compile` and `default-testCompile`, which always run before user-defined executions,
205+
regardless of their position in the `pom.xml` file.
206+
207+
To control the execution order:
208+
209+
* Declare `kotlin-maven-plugin` before `maven-compiler-plugin`.
210+
* Disable the Java compiler plugin's default executions.
211+
* Add custom executions to control the compile phases explicitly.
212+
213+
> You can use the special `none` phase in Maven to disable a default execution.
214+
>
215+
{style="note"}
216+
217+
You can simplify the configuration of mixed Kotlin/Java compilation using `extensions`.
218+
It allows skipping the Maven compiler plugin configuration:
219+
220+
<tabs group="kotlin-java-maven">
221+
<tab title="With extensions" group-key="with-extensions">
141222

142223
```xml
143224
<build>
144225
<plugins>
226+
<!-- Kotlin compiler plugin configuration -->
145227
<plugin>
146228
<groupId>org.jetbrains.kotlin</groupId>
147229
<artifactId>kotlin-maven-plugin</artifactId>
148230
<version>${kotlin.version}</version>
149-
<extensions>true</extensions> <!-- You can set this option
150-
to automatically take information about lifecycles -->
231+
<extensions>true</extensions>
151232
<executions>
152233
<execution>
153-
<id>compile</id>
234+
<id>default-compile</id>
235+
<phase>compile</phase>
236+
<configuration>
237+
<sourceDirs>
238+
<sourceDir>src/main/kotlin</sourceDir>
239+
<!-- Ensure Kotlin code can reference Java code -->
240+
<sourceDir>src/main/java</sourceDir>
241+
</sourceDirs>
242+
</configuration>
243+
</execution>
244+
<execution>
245+
<id>default-test-compile</id>
246+
<phase>test-compile</phase>
247+
<configuration>
248+
<sourceDirs>
249+
<sourceDir>src/test/kotlin</sourceDir>
250+
<sourceDir>src/test/java</sourceDir>
251+
</sourceDirs>
252+
</configuration>
253+
</execution>
254+
</executions>
255+
</plugin>
256+
<!-- No need to configure Maven compiler plugin with extensions -->
257+
</plugins>
258+
</build>
259+
```
260+
261+
If your project previously had a Kotlin-only configuration, you also need to remove the following lines from the `<build>` section:
262+
263+
```xml
264+
<build>
265+
<sourceDirectory>src/main/kotlin</sourceDirectory>
266+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
267+
</build>
268+
```
269+
270+
It ensures that both Kotlin code can reference Java code and vice versa with the `extensions` setup.
271+
272+
</tab>
273+
<tab title="Without extensions" group-key="no-extensions">
274+
275+
```xml
276+
<build>
277+
<plugins>
278+
<!-- Kotlin compiler plugin configuration -->
279+
<plugin>
280+
<groupId>org.jetbrains.kotlin</groupId>
281+
<artifactId>kotlin-maven-plugin</artifactId>
282+
<version>${kotlin.version}</version>
283+
<executions>
284+
<execution>
285+
<id>kotlin-compile</id>
286+
<phase>compile</phase>
154287
<goals>
155-
<goal>compile</goal> <!-- You can skip the <goals> element
156-
if you enable extensions for the plugin -->
288+
<goal>compile</goal>
157289
</goals>
158290
<configuration>
159291
<sourceDirs>
160292
<sourceDir>src/main/kotlin</sourceDir>
293+
<!-- Ensure Kotlin code can reference Java code -->
161294
<sourceDir>src/main/java</sourceDir>
162295
</sourceDirs>
163296
</configuration>
164297
</execution>
165298
<execution>
166-
<id>test-compile</id>
167-
<goals>
168-
<goal>test-compile</goal> <!-- You can skip the <goals> element
169-
if you enable extensions for the plugin -->
299+
<id>kotlin-test-compile</id>
300+
<phase>test-compile</phase>
301+
<goals>
302+
<goal>test-compile</goal>
170303
</goals>
171304
<configuration>
172305
<sourceDirs>
@@ -177,21 +310,24 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
177310
</execution>
178311
</executions>
179312
</plugin>
313+
314+
<!-- Maven compiler plugin configuration -->
180315
<plugin>
181316
<groupId>org.apache.maven.plugins</groupId>
182317
<artifactId>maven-compiler-plugin</artifactId>
183-
<version>3.5.1</version>
318+
<version>3.14.0</version>
184319
<executions>
185-
<!-- Replacing default-compile as it is treated specially by Maven -->
320+
<!-- Disable default executions -->
186321
<execution>
187322
<id>default-compile</id>
188323
<phase>none</phase>
189324
</execution>
190-
<!-- Replacing default-testCompile as it is treated specially by Maven -->
191325
<execution>
192326
<id>default-testCompile</id>
193327
<phase>none</phase>
194328
</execution>
329+
330+
<!-- Define custom executions -->
195331
<execution>
196332
<id>java-compile</id>
197333
<phase>compile</phase>
@@ -212,6 +348,19 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
212348
</build>
213349
```
214350

351+
</tab>
352+
</tabs>
353+
354+
This configuration ensures that:
355+
356+
* Kotlin code is compiled first.
357+
* Java code is compiled after Kotlin and can reference Kotlin classes.
358+
* Default Maven behavior doesn't override the plugin order.
359+
360+
For more details on how Maven handles plugin executions,
361+
see [Guide to default plugin execution IDs](https://maven.apache.org/guides/mini/guide-default-execution-ids.html) in
362+
the official Maven documentation.
363+
215364
## Configure Kotlin compiler execution strategy
216365

217366
The _Kotlin compiler execution strategy_ defines where the Kotlin compiler runs. There are two available strategies:

docs/topics/releases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Alternatively, you can change the version of the `kotlin-maven-plugin` in your `
108108
```
109109

110110
If you have projects created with earlier Kotlin versions, check if you also need to [update the version of any kotlinx
111-
libraries](maven.md#set-dependencies).
111+
libraries](maven.md#dependency-on-a-kotlinx-library).
112112

113113
> To learn more about how to work with Maven in your project, see [Maven](maven.md).
114114
>

0 commit comments

Comments
 (0)