Java 21 works with Gradle versions prior to 8.4

Gradle 8.4 came out on Wednesday. Great stuff.

The release notes state:

“Gradle now supports using Java 21 for compiling, testing, and starting other Java programs. This can be accomplished using toolchains.”

Fantastic! But from my testing I have been able to compile and run Java code in Gradle 8.3 by configuring the Java toolchain.

It even worked when I tried Gradle 8.0.2!

Are there some unknown edge cases? Why say you can’t use Java 21 until Gradle 8.4 when it already works.

This could prevent people from updating to Java 21 because they think they have to first update Gradle to 8.4.

before gradle-8.3, the default kotlin-dsl version gave an error and we used:

plugins {
    id("org.gradle.kotlin.kotlin-dsl") version "4.1.0"
    `kotlin-dsl`
}

here:

this was the same for java-20 and java-21. for gradle-8.3 to gradle-8.4 i cannot tell,

Are there some unknown edge cases?

Having a quick look at the PR that added 21 support, it might indeed be there before.
But it was not tested, so they can hardly state previous versions are compatible when they just don’t know.
The JavaDoc also said before:

Not officially supported by Gradle. Use at your own risk.

So if you tested it and it works fine for your use-case, then it is fine.
The PR looks like they just added tests and it just worked.
But that does not mean it worked in previous versions either, just that it works since they added those tests.

before gradle-8.3, the default kotlin-dsl version gave an error and we used:

That is about running Gradle itself with Java 21 which is not even supported with Gradle 8.4.
That’s not the question here, here the question was about using Java 21 toolchain for the production code.
Using a non-matching kotlin-dsl plugin version should usually not be done, this can lead to major problems.

2 Likes

i am running gradle itself all the time on java-21, and i saw Support running with Java 21 · Issue #25574 · gradle/gradle · GitHub and support JDK21 JVM and Migration · Issue #3601 · openrewrite/rewrite · GitHub.

but now, as you say “not supported” i could read out of kotlin that it should support java-21 as jvmTarget. there is stuff removed from java-21, that was deprecated much earlier. and, last but not least, there is the class file format where one could try to play “spot the difference” between java 21 and java 20. thanks for all the hints, @vampire!!

for your second remark, what would be an example problem you refer to when you write “using a non-matching kotlin-dsl plugin versoin can lead to major problems”.

Let me quote the user guide at Gradle Kotlin DSL Primer

CAUTION:
Avoid specifying a version for the kotlin-dsl plugin

Each Gradle release is meant to be used with a specific version of the kotlin-dsl plugin and compatibility between arbitrary Gradle releases and kotlin-dsl plugin versions is not guaranteed. Using an unexpected version of the kotlin-dsl plugin in a build will emit a warning and can cause hard to diagnose problems.

that java-21 story is confusing a little, to be honest, the toolchain support especially, @Vampire .

i am building and running [terasology]{GitHub - MovingBlocks/Terasology: Terasology - open source voxel world}, which works. i understand that it does not need to. for quality assurance the gradle spotbugs plugin is used, which fails. spotbugs itself works with java-21, with maven. where in gradle it us buried that dependency?

In Gradle?
That’s irrelevant.
That has nothing to do with Gradle, but purely with Spotbugs or at most the Spotbugs Gradle plugin.
Gradle also has ASM included for some things, but that is totally irrelevant here.
The Spotbugs Gradle Plugin runs Spotbugs in separate processes, because Spotbugs uses some static state which otherwise causes Spotbugs runs even of different projects done withing the same Gradle daemon influencing each other.
So this is more a question to the Spotbugs Gradle plugin, than to a Gradle community.

But actually, you just have to add either

spotbugs("com.github.spotbugs:spotbugs:4.8.0")

or

spotbugs("com.github.spotbugs:spotbugs:4.7.3")
spotbugs(platform("org.ow2.asm:asm-bom:9.6"))

to the dependencies { ... } block in your terasology-metrics.gradle.kts and it works.

1 Like

ok, i see, this was in spotbugs gradle plugin:

now it works, have so many thanks!

1 Like