Skip to content

Commit b2c8aae

Browse files
committed
update: update Compiler Kotlin and Java sources in Maven
1 parent f828790 commit b2c8aae

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

docs/topics/maven.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ To use the standard library in your project, add the following dependency to you
6464
<dependency>
6565
<groupId>org.jetbrains.kotlin</groupId>
6666
<artifactId>kotlin-stdlib</artifactId>
67+
<!-- Uses the kotlin.version property
68+
specified in <properties/>: -->
6769
<version>${kotlin.version}</version>
6870
</dependency>
6971
</dependencies>
@@ -135,25 +137,43 @@ If you need to configure an execution, you need to specify its ID. You can find
135137

136138
## Compile Kotlin and Java sources
137139

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:
140+
To compile a project with both Kotlin and Java source files, make sure the Kotlin compiler runs before the Java compiler.
141+
The Java compiler can't see Kotlin declarations until they are compiled into `.class` files.
142+
If your Java code uses Kotlin classes, those classes must be compiled first to avoid `cannot find symbol` errors.
143+
144+
Maven determines plugin execution order based on two main factors:
145+
146+
* The order of plugin declarations in the `pom.xml` file.
147+
* Built-in default executions, such as `default-compile` and `default-testCompile`, which always run before user-defined executions,
148+
regardless of their position in the `pom.xml` file.
149+
150+
To control the execution order:
151+
152+
* Declare `kotlin-maven-plugin` before `maven-compiler-plugin`.
153+
* Disable the Java compiler plugin's default executions.
154+
* Add custom executions to control the compile phases explicitly.
155+
156+
> You can use the special none phase in Maven to disable a default execution.
157+
>
158+
{style="note"}
159+
160+
Here's an example configuration:
141161

142162
```xml
143163
<build>
144164
<plugins>
165+
<!-- Kotlin compiler plugin -->
145166
<plugin>
146167
<groupId>org.jetbrains.kotlin</groupId>
147168
<artifactId>kotlin-maven-plugin</artifactId>
148169
<version>${kotlin.version}</version>
149-
<extensions>true</extensions> <!-- You can set this option
150-
to automatically take information about lifecycles -->
170+
<extensions>true</extensions>
151171
<executions>
152172
<execution>
153-
<id>compile</id>
173+
<id>kotlin-compile</id>
174+
<phase>compile</phase>
154175
<goals>
155-
<goal>compile</goal> <!-- You can skip the <goals> element
156-
if you enable extensions for the plugin -->
176+
<goal>compile</goal>
157177
</goals>
158178
<configuration>
159179
<sourceDirs>
@@ -163,10 +183,10 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
163183
</configuration>
164184
</execution>
165185
<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 -->
186+
<id>kotlin-test-compile</id>
187+
<phase>test-compile</phase>
188+
<goals>
189+
<goal>test-compile</goal>
170190
</goals>
171191
<configuration>
172192
<sourceDirs>
@@ -177,11 +197,14 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
177197
</execution>
178198
</executions>
179199
</plugin>
200+
201+
<!-- Java compiler plugin -->
180202
<plugin>
181203
<groupId>org.apache.maven.plugins</groupId>
182204
<artifactId>maven-compiler-plugin</artifactId>
183-
<version>3.5.1</version>
205+
<version>3.8.1</version>
184206
<executions>
207+
<!-- Disable default executions -->
185208
<execution>
186209
<id>default-compile</id>
187210
<phase>none</phase>
@@ -190,6 +213,8 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
190213
<id>default-testCompile</id>
191214
<phase>none</phase>
192215
</execution>
216+
217+
<!-- Define custom executions -->
193218
<execution>
194219
<id>java-compile</id>
195220
<phase>compile</phase>
@@ -210,28 +235,15 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
210235
</build>
211236
```
212237

213-
There are a couple of notes to make here. First, since Maven 3.0.3, the plugins that bound to the same phase have specific execution order.
214-
In particular, if plugin `A` is bind to phase `compile` and plugin `B` is bind to phase `compile`, then whoever is executed first is resolved by
215-
their declaration order in the `pom.xml`. It simply means if `A` is declared before `B` in `pom.xml`, the `A` will be executed prior to `B`. However,
216-
the built-in plugins native executions, like for plugins `maven-compiler-plugin`, `maven-jar-plugin` e.t.c are executed first regardless of their
217-
order in `pom.xml`. Their execution ids are typically named `default-_someting_`.
218-
219-
Therefore, by specifying the following execution for `maven-compiler-plugin`:
238+
This configuration ensures the following:
220239

221-
```
222-
<execution>
223-
<id>default-compile</id>
224-
<phase>none</phase>
225-
</execution>
226-
<execution>
227-
<id>default-testCompile</id>
228-
<phase>none</phase>
229-
</execution>
230-
```
240+
* Kotlin code is compiled first.
241+
* Java code is compiled after Kotlin and can reference Kotlin classes.
242+
* Default Maven behavior doesn't override the plugin order.
231243

232-
we're effectively disabling the default executions of `maven-compiler-plugin`. Please, note, that usage of phase 'none' is just a well-known workaround for
233-
disabling the particualr execution of plugin in Maven. By disabling `default-compile` and `default-testCompile` executions, the new executions `java-compile`
234-
and not treated as predefined, therefore, and hence the `kotlin-maven-plugin`, as it is declared first, takes precedence.
244+
For more details on how Maven handles plugin executions,
245+
see [Guide to default plugin execution IDs](https://maven.apache.org/guides/mini/guide-default-execution-ids.html) in
246+
the official Maven documentation.
235247

236248
## Enable incremental compilation
237249

0 commit comments

Comments
 (0)