You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/maven.md
+45-33Lines changed: 45 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,8 @@ To use the standard library in your project, add the following dependency to you
64
64
<dependency>
65
65
<groupId>org.jetbrains.kotlin</groupId>
66
66
<artifactId>kotlin-stdlib</artifactId>
67
+
<!-- Uses the kotlin.version property
68
+
specified in <properties/>: -->
67
69
<version>${kotlin.version}</version>
68
70
</dependency>
69
71
</dependencies>
@@ -135,25 +137,43 @@ If you need to configure an execution, you need to specify its ID. You can find
135
137
136
138
## Compile Kotlin and Java sources
137
139
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:
141
161
142
162
```xml
143
163
<build>
144
164
<plugins>
165
+
<!-- Kotlin compiler plugin -->
145
166
<plugin>
146
167
<groupId>org.jetbrains.kotlin</groupId>
147
168
<artifactId>kotlin-maven-plugin</artifactId>
148
169
<version>${kotlin.version}</version>
149
-
<extensions>true</extensions> <!-- You can set this option
150
-
to automatically take information about lifecycles -->
170
+
<extensions>true</extensions>
151
171
<executions>
152
172
<execution>
153
-
<id>compile</id>
173
+
<id>kotlin-compile</id>
174
+
<phase>compile</phase>
154
175
<goals>
155
-
<goal>compile</goal> <!-- You can skip the <goals> element
156
-
if you enable extensions for the plugin -->
176
+
<goal>compile</goal>
157
177
</goals>
158
178
<configuration>
159
179
<sourceDirs>
@@ -163,10 +183,10 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
163
183
</configuration>
164
184
</execution>
165
185
<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>
170
190
</goals>
171
191
<configuration>
172
192
<sourceDirs>
@@ -177,11 +197,14 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
177
197
</execution>
178
198
</executions>
179
199
</plugin>
200
+
201
+
<!-- Java compiler plugin -->
180
202
<plugin>
181
203
<groupId>org.apache.maven.plugins</groupId>
182
204
<artifactId>maven-compiler-plugin</artifactId>
183
-
<version>3.5.1</version>
205
+
<version>3.8.1</version>
184
206
<executions>
207
+
<!-- Disable default executions -->
185
208
<execution>
186
209
<id>default-compile</id>
187
210
<phase>none</phase>
@@ -190,6 +213,8 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
190
213
<id>default-testCompile</id>
191
214
<phase>none</phase>
192
215
</execution>
216
+
217
+
<!-- Define custom executions -->
193
218
<execution>
194
219
<id>java-compile</id>
195
220
<phase>compile</phase>
@@ -210,28 +235,15 @@ making sure that the `kotlin` plugin comes before the `maven-compiler-plugin` in
210
235
</build>
211
236
```
212
237
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:
220
239
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.
231
243
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
0 commit comments