Long short story: I'd like to publish a variant for jdk8 retro-compatibility for one of my kotlin-only libraries.
This is a long-wanted feature which I'm trying to tackle since quite some time but never got it right. However after many attempts and help on Gradle Slack, I think I'm quite close but I still have an error I can't seem to get rid off.
The idea is to have the main version (src/main and scr/jpms, with this latter containing simply module-info.class) compiled with jdk11, while having a jdk8 variant for src/main only compiled of course with jdk8.
This is my current build.gradle.kts:
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.5.10"
`java-library`
`maven-publish`
}
group = "kotlin.graphics"
version = "3.3.1"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation("io.kotest:kotest-runner-junit5:4.4.1")
testImplementation("io.kotest:kotest-assertions-core:4.4.1")
}
val jdk8 = sourceSets.create("jdk8") {
java.srcDir("src/main/java")
kotlin.srcDir("src/main/kotlin")
}
val jdk11 = sourceSets["main"].apply {
java.srcDir("src/jpms/java")
}
java.registerFeature("jdk8") {
usingSourceSet(jdk8)
capability("group", "name", "0.1")
}
configureCompileVersion(jdk8, 8)
configureCompileVersion(jdk11, 11)
val moduleName = "$group.$name"
fun configureCompileVersion(set: SourceSet, jdkVersion: Int) {
val compiler = project.javaToolchains.compilerFor {
languageVersion.set(JavaLanguageVersion.of(jdkVersion))
}.get()
val target = if (jdkVersion == 8) "1.8" else jdkVersion.toString()
tasks {
named<KotlinCompile>(set.compileKotlinTaskName) {
kotlinOptions {
jvmTarget = target
jdkHome = compiler.metadata.installationPath.asFile.absolutePath
}
source = sourceSets.main.get().kotlin
}
named<JavaCompile>(set.compileJavaTaskName) {
targetCompatibility = target
sourceCompatibility = target
modularity.inferModulePath.set(jdkVersion >= 9)
javaCompiler.set(compiler)
source = sourceSets.main.get().allJava + set.allJava
if (jdkVersion >= 9)
options.compilerArgs = listOf("--patch-module", "$moduleName=${set.output.asPath}")
}
}
}
val SourceSet.compileKotlinTaskName: String
get() = getCompileTaskName("kotlin")
val SourceSet.kotlin: SourceDirectorySet
get() = withConvention(KotlinSourceSet::class) { kotlin }
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "org.gradle.sample"
artifactId = "library"
version = "1.1"
from(components["java"])
}
}
repositories.maven {
name = "prova"
url = uri("repo")
}
}
If I run :assemble, the produced artifact is compiled properly with jdk11.
And till that everything as expected.
But If I try to publish, I get instead:
Task :compileJdk8Kotlin FAILED 5 actionable tasks: 1 executed, 4 up-to-date e: Module java.base cannot be found in the module graph
For some reasons, it looks like Gradle tries to compile the jdk8 variant using jpms, although it should be disabled automatically. I tried to manually set it on and off:
modularity.inferModulePath.set(jdkVersion >= 9)
but it didn't work neither.
The project is here
Gradle 7.1.1