Hi,
I’m trying to write kotlin-dsl script/convention plugin that allow the consumer to set a property / calla function.
The convention plugins are set via the usual includedBuild
pointing to a separate gradle project that has the kotlin-dsl
plugin. Then in the actual convention plugin, e.g. for java, I would like to write something like this (using kotlin extentions methods)
plugins {
id("sandbox.java-conventions")
}
java {
configureJavaToolChain(
20,
listOf("jdk.incubator.concurrent")
)
}
Or if using property extensions
java {
javaToolchain Version = 20,
addedModules = listOf("jdk.incubator.concurrent")
}
Here’s how I started to write this, notice the JavaCompile
and KotlinCompile
tasks are configured in the body by calling tasks.withType
.
While this code compiles the method configureJavaToolChain
does not seem to be reachable from the consumer. Also ideally the function approach doesn’t seem to scale much if I want to add other parameters, properties looks more appealing but also have the same visibility issues.
So I’m unsure how to proceed from this point.
plugins {
id("java-library")
}
fun JavaPluginExtension.configureJavaToolChain(
javaVersion: Int,
// other parameters
addedModules: List<String> = listOf(),
) {
toolchain {
languageVersion = JavaLanguageVersion.of(javaVersion)
}
tasks.withType<JavaCompile> {
// sets release or source & target
options.compilerArgs.addAll(
// transform to proper args the added modules
)
}
tasks.withType<JavaExec>().configureEach {
// Need to set the toolchain https://github.com/gradle/gradle/issues/16791
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
jvmArgs(
// transform to proper args the added modules
)
}
}
I’m currently experimenting on a public personal repo, if needed that may five more context.
Thanks in advance for any help