Hi! I wrote a precompiled script plugin in Kotlin ...
# community-support
d
Hi! I wrote a precompiled script plugin in Kotlin for my project inside
buildSrc
directory. The plugin applies
kotlin("jvm")
plugin inside. It doesn't specify the Kotlin plugin version there, and the implementation dependency of the
buildSrc
is set to the right Kotlin version, where, in my case,
2.0.0
. But when I apply the script plugin to my subprojects in the same root project. It seems that the version of Kotlin plugin applied to them is not the intended
2.0.0
but that of the Kotlin plugin bundled with Gradle, I guess. My guess is both the Kotlin plugin Gradle uses and another specified as a dependency to the
buildSrc
are in the build classpath, and the Gradle one seems picked up when the script plugin applied. Is there a problem I outlined above in the current version of Gradle? I tested with 8.8. And if so, is there any way to get the right version (2.0.0) of the Kotlin plugin applied as I intend?
If I try to apply the Kotlin plugin directly in subprojects'
build.gradle.kts
, it works ok.
a
Hi, can you share what you have in your buildSrc/build.gradle.kts please?
d
Copy code
plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(libs.plugin.kotlin.jvm)
    implementation(libs.plugin.ksp)
    implementation(libs.auto.service.ksp)
    implementation(libs.auto.service.annotations)
}

kotlin {
    jvmToolchain(21)
}
WHERE
Copy code
plugin-kotlin-jvm = { module = "org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin", version.ref = "kotlin" }
plugin-ksp = { module = "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
The script plugin is like:
Copy code
plugins {
    kotlin("jvm")
    `maven-publish`
}

repositories {
    mavenCentral()
    maven {
        url = uri("<https://jitpack.io>")
    }
}

kotlin {
    jvmToolchain(21)
}

publishing {
    publications {
        create<MavenPublication>("maven") {
            from(components["java"])
        }
    }
}
a
At first glance that all looks correct
Regarding the Kotlin version, can you more about what you observe, and what you expect?
d
Kotlin does not yet support 22 JDK target, falling back to Kotlin JVM_21 JVM target FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘buildSrccompileKotlin’.
Inconsistent JVM-target compatibility detected for tasks ‘compileJava’ (22) and ‘compileKotlin’ (21).
Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation * Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.
BUILD FAILED in 480ms
We get the above errors.
But Kotlin 2.0 supports the JDK 22 target.
We are suspecting the Kotiln plugin version in effect is 1.9.22, which is bundled with Gradle 8.8.
When we apply Kotiln plugin directly to the project, it compiles ok.
v
Can you share a build
--scan
URL? That should make it very obvious which versions of what are used where.
a
what's the output if you print the Kotlin version in a build.gradle.kts?
Copy code
println("KotlinVersion.CURRENT: ${KotlinVersion.CURRENT}")
println("kotlinToolingVersion: $kotlinToolingVersion")
d
Sorry. I tried to print Kotlin tooling versions and found that I should not have set the jvmToolchain to 22 for buildSrc.
jvmToolchain(21) in buildSrc and jvmToolchain(22) in other subprojects solved my problem. Sorry again for wasting your time. Thanks for the support.
👌 1
a
ahh right, that make sense. Yeah, the
buildSrc
project will use the Kotlin version embedded into Gradle, which isn't 2.0 yet https://docs.gradle.org/8.8/userguide/compatibility.html#kotlin
👍 1
in case it helps, you can also print the embedded Kotlin version:
Copy code
println("embeddedKotlinVersion: $embeddedKotlinVersion") // Gradle builds/runs build scripts with this Kotlin
println("kotlinToolingVersion: $kotlinToolingVersion") // Kotlin version used to build/run project sources
👍 1