With 2.1.20, `jvm().withJava()` is deprecated in K...
# gradle
h
With 2.1.20,
jvm().withJava()
is deprecated in KMP to support Gradle 9. But KMP does also not allow multiple jvm targets. How exactly do I create a multi release jar (for JPMS support)? Currently, I use
jvm().withJava()
to create a jvm9 java sourceSet (bypassing the multiple jvm targets).
👀 1
Copy code
plugins {
    id("kotlinMPPSetup")
}

kotlin {
    jvm {
        withJava()
    }
}

val java9 by java.sourceSets.registering

tasks.jvmJar {
    into("META-INF/versions/9") {
        from(java9.map { it.output })
    }

    manifest.attributes("Multi-Release" to true)
}

tasks.named<JavaCompile>("compileJava9Java") {
    javaCompiler.set(javaToolchains.compilerFor {})
    options.release.set(9)

    options.compilerArgumentProviders += object : CommandLineArgumentProvider {

        @get:InputFiles
        @get:PathSensitive(PathSensitivity.RELATIVE)
        val kotlinClasses = tasks.compileKotlinJvm.flatMap { it.destinationDirectory }

        override fun asArguments(): List<String> = listOf(
            "--patch-module",
            "app.softwork.serviceloader.runtime=${kotlinClasses.get().asFile.absolutePath}"
        )
    }
}
m
I think you're supposed to use compilations
e
so trying it out, this sorta works
Copy code
kotlin {
    jvm {
        val main by compilations.getting
        val main9 by compilations.creating {
            compileTaskProvider.configure {
                compilerOptions.jvmTarget = JvmTarget.JVM_9
            }
            compileJavaTaskProvider?.configure {
                options.release = 9
            }
        }
        tasks.named<ProcessResources>(main.processResourcesTaskName) {
            into("META-INF/versions/9") {
                from(main9.output.allOutputs)
                exclude("META-INF")
            }
        }
    }
}
but doesn't give you the ability to reference
jvmMain
classes from within
jvmMain9
, and I'm not sure if there's a decent way to do so without introducing a task dependency cycle
(as in,
Copy code
val main9 by compilations.creating {
    associateWith(main)
and similar tricks with
sourceSet.dependsOn
fail)