Skip to content

Commit f029dcb

Browse files
committed
kotlin 1.4.0 multirelease JAR out-of-the-box
1 parent 1e2fda8 commit f029dcb

File tree

2 files changed

+6
-47
lines changed

2 files changed

+6
-47
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
21
import org.gradle.api.attributes.java.TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE
32
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
43
import java.net.URL
@@ -23,10 +22,6 @@ dependencies {
2322
implementation(kotlin("stdlib"))
2423
implementation(kotlin("stdlib-jdk8"))
2524

26-
attributesSchema.attribute(LIBRARY_ELEMENTS_ATTRIBUTE).compatibilityRules.add(ModularJarCompatibilityRule::class)
27-
components { withModule<ModularKotlinRule>(kotlin("stdlib")) }
28-
components { withModule<ModularKotlinRule>(kotlin("stdlib-jdk8")) }
29-
3025
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion")
3126
testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
3227
}
@@ -68,8 +63,10 @@ tasks {
6863
sourceCompatibility = "11"
6964
}
7065

71-
// this is needed because we have a separate compile step in this example with the 'module-info.java' is in 'main/java' and the Kotlin code is in 'main/kotlin'
72-
compileJava.get().options.compilerArgs = listOf("--patch-module", "$moduleName=${sourceSets.main.get().output.asPath}")
66+
67+
compileJava { // this is needed because we have a separate compile step in this example with the 'module-info.java' is in 'main/java' and the Kotlin code is in 'main/kotlin'
68+
options.compilerArgs = listOf("--patch-module", "$moduleName=${sourceSets.main.get().output.asPath}")
69+
}
7370

7471
withType<Test> { useJUnitPlatform() }
7572
}
@@ -103,43 +100,4 @@ publishing.publications.register("mavenJava", MavenPublication::class) {
103100
artifact(sourceJar)
104101
}
105102

106-
// == Add access to the 'modular' variant of kotlin("stdlib"): Put this into a buildSrc plugin and reuse it in all your subprojects
107-
configurations.all {
108-
attributes.attribute(TARGET_JVM_VERSION_ATTRIBUTE, 11)
109-
val n = name.toLowerCase()
110-
if (n.endsWith("compileclasspath") || n.endsWith("runtimeclasspath"))
111-
attributes.attribute(LIBRARY_ELEMENTS_ATTRIBUTE, objects.named("modular-jar"))
112-
if (n.endsWith("compile") || n.endsWith("runtime"))
113-
isCanBeConsumed = false
114-
}
115-
116-
abstract class ModularJarCompatibilityRule : AttributeCompatibilityRule<LibraryElements> {
117-
override fun execute(details: CompatibilityCheckDetails<LibraryElements>): Unit = details.run {
118-
if (producerValue?.name == LibraryElements.JAR && consumerValue?.name == "modular-jar")
119-
compatible()
120-
}
121-
}
122-
123-
abstract class ModularKotlinRule : ComponentMetadataRule {
124-
125-
@javax.inject.Inject
126-
abstract fun getObjects(): ObjectFactory
127-
128-
override fun execute(ctx: ComponentMetadataContext) {
129-
val id = ctx.details.id
130-
listOf("compile", "runtime").forEach { baseVariant ->
131-
ctx.details.addVariant("${baseVariant}Modular", baseVariant) {
132-
attributes {
133-
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, getObjects().named("modular-jar"))
134-
}
135-
withFiles {
136-
removeAllFiles()
137-
addFile("${id.name}-${id.version}-modular.jar")
138-
}
139-
withDependencies {
140-
clear() // 'kotlin-stdlib-common' and 'annotations' are not modules and are also not needed
141-
}
142-
}
143-
}
144-
}
145-
}
103+
configurations.all { attributes.attribute(TARGET_JVM_VERSION_ATTRIBUTE, 11) }

0 commit comments

Comments
 (0)