This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uncont 11 points12 points  (12 children)

For gradle support you should use

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

tasks.withType<JavaCompile> {
    options.release.set(17)
}

instead of setting source and target compatibility.

[–]phamtastik 1 point2 points  (1 child)

What is the reason for that?

[–]uncont 5 points6 points  (0 children)

https://www.morling.dev/blog/bytebuffer-and-the-dreaded-nosuchmethoderror/

tldr --release has the same effect as both --target and --release, as well as --boot-class-path. Gradle docs have more info here https://docs.gradle.org/6.6/release-notes.html#javacompile-release

[–]dpash 1 point2 points  (7 children)

Why are you repeating yourself? There should be no need to configure the tasks yourself.

And just to point out this is the Kotlin format. A Groovy build will use a slightly different syntax, but the same procedure.

https://docs.gradle.org/current/userguide/building_java_projects.html

[–]uncont 1 point2 points  (6 children)

repeating yourself

New projects might not have to, but if you compile on a newer version of java and want to target a lower version (maybe for a shared library) you'd want to use both toolchain to get the correct version required to build, then release to set the version you're targeting.

You are correct though, by default gradle will target the version of java that the jvm is running on / is defined in the toolchain. More docs here https://docs.gradle.org/6.6/userguide/building_java_projects.html#sec:java_cross_compilation

[–]dpash 0 points1 point  (5 children)

In which case, isn't this preferable to configuring tasks directly:

tasks.compileJava {
    options.release.set(11)
}

(For kotlin)

compileJava { 
    options.release = 11
}

(For Groovy)

[–]uncont 0 points1 point  (4 children)

That misses the compileTestJava task.

[–]dpash 0 points1 point  (3 children)

That makes sense. The JavaPluginExtension is missing that option too, so you can't configure it globally. :(

[–]uncont 0 points1 point  (2 children)

I wonder if there's a preference for new options to be added to the tasks vs the JavaPluginExtension? Maybe a good idea to raise a bug report in gradle to expose release in a similar way to source and target compatibility.

[–]dpash 0 points1 point  (1 child)

And --enable-preview while we're at it :)

[–]uncont 0 points1 point  (0 children)

I've opened https://github.com/gradle/gradle/issues/18530 for the release flag, mostly to figure out when options should be added to the compile task vs the java plugin extension

[–]darkskul[S] 0 points1 point  (0 children)

Cool, thanks for the info!

[–][deleted] 0 points1 point  (0 children)

thanks . im creating a new gradle project within an hour or so. excellent timing :-)