Skip to content

Commit deb75a6

Browse files
authored
feat: compilation for JVM (#4766)
1 parent 07819a0 commit deb75a6

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed
13 KB
Loading

docs/topics/multiplatform/multiplatform-configure-compilations.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,87 @@ kotlin {
205205
You also need to create a custom compilation in other cases, for example, if you want to combine compilations for different
206206
JVM versions in your final artifact, or you have already set up source sets in Gradle and want to migrate to a multiplatform project.
207207

208-
## Use Java sources in JVM compilations
208+
## Compilation for JVM
209209

210-
When creating a project with the [project wizard](https://kmp.jetbrains.com/), Java sources are created by default and included in the compilations of
211-
the JVM target.
210+
When you declare the `jvm` target in your multiplatform project, the Kotlin Multiplatform plugin automatically
211+
creates Java sources sets and includes them in the compilations of the JVM target.
212212

213-
The Java source files are placed in the child directories of the Kotlin source roots. For example, the paths are:
213+
The common source sets can't include Java resources, so you should place them in the corresponding child directories
214+
of your multiplatform project. For example:
214215

215216
![Java source files](java-source-paths.png){width=200}
216217

217-
The common source sets can't include Java sources.
218+
Currently, the Kotlin Multiplatform plugin replaces some tasks configured by the Java plugin:
218219

219-
Due to current limitations, the Kotlin plugin replaces some tasks configured by the Java plugin:
220+
* JAR task: instead of a standard `jar`, it uses a target-specific task based on the artifact's name, for example,
221+
`jvmJar` for the `jvm()` target declaration and `desktopJar` for `jvm("desktop")`.
222+
* Test task: instead of a standard `test`, it uses a target-specific task based on the artifact's name, for example, `jvmTest`.
223+
* Resource processing: instead of `*ProcessResources` tasks, resources are handled by the corresponding compilation tasks.
220224

221-
* The target's JAR task instead of `jar` (for example, `jvmJar`).
222-
* The target's test task instead of `test` (for example, `jvmTest`).
223-
* The resources are processed by the equivalent tasks of the compilations instead of `*ProcessResources` tasks.
225+
These tasks are created automatically when the target is declared. However, you can manually define the JAR task
226+
and configure it if necessary:
224227

225-
The publication of this target is handled by the Kotlin plugin and doesn't require steps that are specific for the Java plugin.
228+
<tabs group="build-script">
229+
<tab title="Kotlin" group-key="kotlin">
230+
231+
```kotlin
232+
// Shared module's `build.gradle.kts` file
233+
plugins {
234+
kotlin("multiplatform") version "%kotlinVersion%"
235+
}
236+
237+
kotlin {
238+
// Specify the JVM target
239+
jvm {
240+
// Add the task for JAR generation
241+
tasks.named<Jar>(artifactsTaskName).configure {
242+
// Configure the task
243+
}
244+
}
245+
246+
sourceSets {
247+
jvmMain {
248+
dependencies {
249+
// Add JVM-specific dependencies
250+
}
251+
}
252+
}
253+
}
254+
```
255+
256+
</tab>
257+
<tab title="Groovy" group-key="groovy">
258+
259+
```groovy
260+
// Shared module's `build.gradle` file
261+
plugins {
262+
id 'org.jetbrains.kotlin.multiplatform' version '%kotlinVersion%'
263+
}
264+
265+
kotlin {
266+
// Specify the JVM target
267+
jvm {
268+
// Add the task for JAR generation
269+
tasks.named<Jar>(artifactsTaskName).configure {
270+
// Configure the task
271+
}
272+
}
273+
274+
sourceSets {
275+
jvmMain {
276+
dependencies {
277+
// Add JVM-specific dependencies
278+
}
279+
}
280+
}
281+
}
282+
```
283+
284+
</tab>
285+
</tabs>
286+
287+
This target is published by the Kotlin Multiplatform plugin and doesn't require steps that are specific
288+
to the Java plugin.
226289

227290
## Configure interop with native languages
228291

docs/topics/multiplatform/multiplatform-publish-lib.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ Learn more about [adding dependencies](multiplatform-add-dependencies.md).
4444

4545
This `kotlinMultiplatform` publication includes metadata artifacts and references the other publications as its variants.
4646

47-
> Some repositories, such as Maven Central, require that the root module contains a JAR artifact without a classifier, for example `kotlinMultiplatform-1.0.jar`.
48-
> The Kotlin Multiplatform plugin automatically produces the required artifact with the embedded metadata artifacts.
49-
> This means you don't have to customize your build by adding an empty artifact to the root module of your library to meet the repository's requirements.
47+
Some repositories, such as Maven Central, require the root module to contain a JAR artifact without a classifier,
48+
for example `kotlinMultiplatform-1.0.jar`.
49+
The Kotlin Multiplatform plugin automatically produces the required artifact with the embedded metadata artifacts.
50+
This means you don't have to add an empty artifact to the root module of your library to
51+
meet the repository's requirements.
52+
53+
> Learn more about JAR artifact generation with [Gradle](multiplatform-configure-compilations.md#compilation-for-jvm)
54+
> and [Maven](maven.md#create-jar-file) build systems.
5055
>
51-
{style="note"}
56+
{style="tip"}
5257

5358
The `kotlinMultiplatform` publication may also need the sources and documentation artifacts if that is required by the repository. In that case,
5459
add those artifacts by using [`artifact(...)`](https://docs.gradle.org/current/javadoc/org/gradle/api/publish/maven/MavenPublication.html#artifact-java.lang.Object-)

0 commit comments

Comments
 (0)