Skip to content

Commit de69da8

Browse files
committed
Allow building with a newer JDK while targeting Java 17
Compile with --release 17, so the output keeps Java 17 bytecode and API usage whichever JDK compiles it. The toolchain still defaults to 17 and can be overridden with -PjavaToolchain=21, which also selects the JDK the tests run on. Java 21 works with the current Gradle and test libraries. Newer JDKs need a Gradle upgrade. userApiJavadoc now runs the toolchain's javadoc instead of the one from the JDK running Gradle, and its version is a task input, so switching toolchains regenerates the docs. CONTRIBUTING.md describes the new option. Compared with the JDK 17 toolchain, a build with the JDK 21 toolchain produces only Java 17 (major version 61) class files. Of the 3,640 classes, 1,487 are byte-identical. The vocab classes, which build does not compile, are all byte-identical. The rest differ only in ways that come from newer javac versions and do not change behavior: - Metadata: constant pool order, MethodParameters attributes for the mandated and synthetic parameters of inner class constructors, and argN parameter names in the LocalVariableTable of anonymous class constructors. - Object methods called on an interface type (toString, equals, getClass) are emitted as invokeinterface on the interface instead of invokevirtual on Object (25 classes). The JVM resolves both to the same method. - A switch on an enum inside that enum's own class uses ordinal() directly instead of a $SwitchMap. This drops two synthetic classes, MetaDataColumnType$1 and ContextType$1, and three switch map fields from other synthetic classes. Closes #215. Issue: #215 Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Tony Germano <tony@germano.name>
1 parent 52eb08d commit de69da8

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ The build is driven by Gradle through the included wrapper; no separate Gradle i
3838
```
3939
On Windows use `gradlew.bat` instead of `./gradlew`. The assembled distribution lands in `server/setup`, the same location the previous Ant build used. For release artifacts, run a clean build: `./gradlew clean build dist`. Windows with SDKMan may generate an error about the file path being too long in the javadoc step, skip this by adding `-x :server:userApiJavadoc` to your Gradle command.
4040

41+
By default the build compiles and runs the tests with any Java 17 JDK Gradle finds installed, including those installed by SDKMAN. To use Java 21 instead, add `-PjavaToolchain=21`, or put `javaToolchain=21` in `~/.gradle/gradle.properties`. The compiled output still targets Java 17. Gradle does not download JDKs, so the version you name must already be installed.
42+
4143
Dependencies are pinned and checksum-verified. To change a dependency version: edit `gradle/libs.versions.toml`, then refresh the checksum metadata **with a cold dependency cache and CI's flags**:
4244
```bash
4345
GRADLE_USER_HOME=$(mktemp -d) ./gradlew --write-verification-metadata sha256 build dist -PdisableSigning=true -Pcoverage=true

‎build.gradle‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* ./gradlew test [-Pcoverage=true] tests only (JaCoCo optional)
3535
* ./gradlew dist distribution extension zips
3636
* ./gradlew clean clean all modules
37+
* ./gradlew build -PjavaToolchain=21 compile/test with another JDK
38+
* (output still targets Java 17)
3739
*
3840
* For release artifacts run a clean build (./gradlew clean build dist):
3941
* the staging into server/setup removes stale files where it owns the
@@ -71,6 +73,9 @@ ext {
7173
disableTests = flag('disableTests')
7274
disableSigning = flag('disableSigning')
7375
coverageEnabled = flag('coverage')
76+
// the shipped bytecode and API level; the JDK used to build may be newer
77+
javaRelease = 17
78+
javaToolchain = (providers.gradleProperty('javaToolchain').getOrNull() ?: javaRelease) as int
7479
}
7580

7681
allprojects {
@@ -158,7 +163,7 @@ subprojects {
158163

159164
java {
160165
toolchain {
161-
languageVersion = JavaLanguageVersion.of(17)
166+
languageVersion = JavaLanguageVersion.of(rootProject.javaToolchain)
162167
}
163168
}
164169

@@ -177,6 +182,7 @@ subprojects {
177182

178183
tasks.withType(JavaCompile).configureEach {
179184
options.encoding = 'UTF-8'
185+
options.release = rootProject.javaRelease
180186
options.debug = true
181187
}
182188
// Match Ant's javac invocation: -sourcepath defaults to srcdir, and

‎server/build.gradle‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,12 @@ def userApiJavadoc = tasks.register('userApiJavadoc') {
492492
})
493493
outputs.dir outDir
494494
dependsOn syncDonkeyLibs
495+
def javadocTool = javaToolchains.javadocToolFor(java.toolchain)
496+
// the output depends on the javadoc version, which is not otherwise an input
497+
inputs.property('javadocVersion', javadocTool.map { it.metadata.languageVersion.asInt() })
495498
doLast {
496499
outDir.mkdirs()
497-
def cmd = [new File(System.getProperty('java.home'), 'bin/javadoc').absolutePath,
500+
def cmd = [javadocTool.get().executablePath.asFile.absolutePath,
498501
'-d', outDir.absolutePath,
499502
'-notimestamp',
500503
'-classpath', (configurations.compileClasspath + fileTree(dir: 'lib', include: '**/*.jar')).asPath,

0 commit comments

Comments
 (0)