Skip to content

Commit fc4115b

Browse files
committed
Fix module-info generation for multiplatform artifact
1 parent bf48b41 commit fc4115b

File tree

4 files changed

+50
-43
lines changed

4 files changed

+50
-43
lines changed

multiplatform-annotations/build.gradle.kts

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.gradle.jvm.tasks.Jar
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
23

34
plugins {
45
kotlin("multiplatform") version "1.9.22"
@@ -75,19 +76,56 @@ kotlin {
7576
}
7677
}
7778

78-
val jvmJar by tasks.getting(Jar::class) {
79-
into ("META-INF/versions/9") {
80-
from (project(":multiplatform-module-info").sourceSets["main"].output) {
81-
include("module-info.class")
82-
}
79+
// from https://github.com/Kotlin/kotlinx-datetime/blob/bc8adee2b9e3659e8e1c38fc09f3a4a1bdf85276/core/build.gradle.kts
80+
tasks {
81+
val compileJavaModuleInfo by registering(JavaCompile::class) {
82+
val moduleName = "org.jetbrains.annotations.multiplatform" // this module's name
83+
val compileKotlinJvm by getting(KotlinCompile::class)
84+
val sourceDir = file("src/jvmMain/moduleInfo/")
85+
println("sourceDir: $sourceDir")
86+
val targetDir = compileKotlinJvm.destinationDirectory.map { it.dir("../moduleInfo/") }
87+
println("targetDir: $targetDir")
88+
89+
// Use a Java 11 compiler for the module info.
90+
javaCompiler.set(project.javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(11)) })
91+
92+
// Always compile kotlin classes before the module descriptor.
93+
dependsOn(compileKotlinJvm)
94+
95+
// Add the module-info source file.
96+
source(sourceDir)
97+
98+
// Set the task outputs and destination dir
99+
outputs.dir(targetDir)
100+
destinationDirectory.set(targetDir)
101+
102+
// Configure JVM compatibility
103+
sourceCompatibility = JavaVersion.VERSION_1_9.toString()
104+
targetCompatibility = JavaVersion.VERSION_1_9.toString()
105+
106+
// Set the Java release version.
107+
options.release.set(9)
108+
109+
// Ignore warnings about using 'requires transitive' on automatic modules.
110+
// not needed when compiling with recent JDKs, e.g. 17
111+
options.compilerArgs.add("-Xlint:-requires-transitive-automatic")
112+
113+
// Patch the compileKotlinJvm output classes into the compilation so exporting packages works correctly.
114+
options.compilerArgs.addAll(listOf("--patch-module", "$moduleName=${compileKotlinJvm.destinationDirectory.get()}"))
115+
116+
// Use the classpath of the compileKotlinJvm task.
117+
// Also ensure that the module path is used instead of classpath.
118+
classpath = compileKotlinJvm.libraries
119+
modularity.inferModulePath.set(true)
83120
}
84-
manifest.attributes("Multi-Release" to true)
85-
}
86121

87-
val jvmSourcesJar by tasks.getting(Jar::class) {
88-
into ("META-INF/versions/9") {
89-
from (project(":multiplatform-module-info").sourceSets["main"].output) {
90-
include("module-info.class")
122+
// Configure the JAR task so that it will include the compiled module-info class file.
123+
val jvmJar by existing(Jar::class) {
124+
manifest {
125+
attributes("Multi-Release" to true)
126+
}
127+
from(compileJavaModuleInfo.map { it.destinationDirectory }) {
128+
into("META-INF/versions/9/")
91129
}
92130
}
93131
}

multiplatform-module-info/build.gradle

Lines changed: 0 additions & 31 deletions
This file was deleted.

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
rootProject.name = 'annotations-parent'
1818

19-
include 'java-annotations', 'java-module-info', 'multiplatform-annotations', 'multiplatform-module-info'
19+
include 'java-annotations', 'java-module-info', 'multiplatform-annotations'

0 commit comments

Comments
 (0)