Use Gradle toolchain to specify the jdk to use#558
Conversation
| repositories { mavenCentral() } | ||
|
|
||
| def javaVersion = JavaLanguageVersion.of(System.env.JAVA_VERSION ?: 11) | ||
| java.toolchain.languageVersion = javaVersion |
There was a problem hiding this comment.
Should we do this within java?
java {
toolchain.languageVersion = JavaLanguageVersion.of(...)
if (toolchain.languageVersion.canCompileOrRun(15)) { ...
From Gradle docs.
There was a problem hiding this comment.
Since this project uses script plugins, I preferred doing this before they were applied to avoid any future ordering issues if they eagerly resolved their configuration to a fixed state. In reality this doesn't matter as your scripts do not appear to rely on any of this state in a problematic way. You are certainly welcome to move it for stylistic formatting purposes.
Long term, I'd recommend using precompiled script plugins to avoid quirky surprises that can arise from embedding scriptlets compared to true plugins (e.g. java-library.caffeine.gradle.kts).
|
Fwiw, I tend to agree with https://jakewharton.com/gradle-toolchains-are-rarely-a-good-idea/ |
| with: | ||
| distribution: temurin | ||
| java-version: 17 | ||
| java-version: ${{ env.JAVA_VERSION }} |
There was a problem hiding this comment.
actions/setup-java can install several versions at once, so this could be used to install both 17 and 21 (or 22), setting up the latter as the default version, and this would remove the need for:
- the gradle-daemon-jvm.properties toolchainVersion
- the foojay plugin
|
Thank you! If would lean toward the suggestion from @tbroyer of using (assuming that I have this right):
Then the toolchains would cover just the testing step (where we already have a test configured to run under Java 8). But I also think that OSGi is more important than the build hygiene, so I don't want to miss out on that while we optimize things here. So I think we could merge this and then #428 as-is, but I won't press any buttons yet in case the tweaks are easy enough that you might choose to just do them. (No pressure. It's not like I'm going to volunteer... :)) |
|
Gradle supports jdk 22, and can compile/run against EA builds using The only potential flaw with the I don't agree with the other rational in that article, e.g. Compact String are a runtime feature and specifying the build's jdk improves the new contributors experience (as they would otherwise only see the class file version mismatch error). But all of that is moot, what matters is that any change fit within the maintainers' workflow and obviously it did not fit Jake's. What best fits the team here is most important. |
|
Thanks. There's more that I'd want to dig into to understand the situation with toolchains fully, so let's roll with this for now. |
|
It looks like this causes Gradle to need a local JDK 21 that it's able to autodetect. That's not a significant different from my perspective because my default JDK is a JDK 22, which Gradle doesn't like, so I have been needing to set Here's a run with no So I'll now run |
|
oh, you can change to that version in the properties file as Gradle is compatible with 22. All it needs is for the daemon to run on jdk17 or above for the bnd plugin. |
fyi the latest Gradle release will now auto-provision the daemon so you don't need any workarounds. You can run #This file is generated by updateDaemonJvm
toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/e2d97f28068cf05b0467aa8e97b19f69/redirect
toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/a41f952f4496c2309be30fd168c6c117/redirect
toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/e2d97f28068cf05b0467aa8e97b19f69/redirect
toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/a41f952f4496c2309be30fd168c6c117/redirect
toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/e7806cd9471741d622398825f14d2da6/redirect
toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/0402cc5012ae8124ea0ad01bd29342ef/redirect
toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/e2d97f28068cf05b0467aa8e97b19f69/redirect
toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/a41f952f4496c2309be30fd168c6c117/redirect
toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/86ea5d26c5757681ffe78d87258b45ec/redirect
toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/ea8232621e1368089cec8b12816df5e3/redirect
toolchainVersion=21 |
This resolves #428 (comment) by specifying java toolchains for the Gradle build daemon and the project. Previously the build daemon, the compiler, and runtime execution all inherited the system JDK, e.g. via
JAVA_HOME. The daemon's is specified at Java 21 (gradle-daemon-jvm.properties) and the compile/execution by the project's language toolchain version. The latter defaults to Java 11 and may be overridden by the env variableJAVA_VERSION, which is used in the github actions. The toolchains are automatically downloaded if absent using the resolver plugin.