From b85a76f9833c81ae90ec8d48d9a0af1931df769e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 8 Jan 2025 18:37:48 +0100 Subject: [PATCH] Configure the Gradle toolchain to use Java 23 In order to be able to fix gh-34140 which requires using at least a Java 22 compiler, this commit intends to change the configuration of the Gradle toolchain to use Java 23, while setting the Java release to Java 17 (or other versions when using MRJARs) when relevant in order to keep the current Java 17 baseline. See gh-34220 --- .../build/JavaConventions.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/build/JavaConventions.java b/buildSrc/src/main/java/org/springframework/build/JavaConventions.java index 60581d44cb9..4ffd6916c1f 100644 --- a/buildSrc/src/main/java/org/springframework/build/JavaConventions.java +++ b/buildSrc/src/main/java/org/springframework/build/JavaConventions.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,7 +73,7 @@ public class JavaConventions { private void applyJavaCompileConventions(Project project) { project.getExtensions().getByType(JavaPluginExtension.class).toolchain(toolchain -> { toolchain.getVendor().set(JvmVendorSpec.BELLSOFT); - toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17)); + toolchain.getLanguageVersion().set(JavaLanguageVersion.of(23)); }); SpringFrameworkExtension frameworkExtension = project.getExtensions().getByType(SpringFrameworkExtension.class); project.afterEvaluate(p -> { @@ -83,6 +83,7 @@ public class JavaConventions { compileTask.getOptions().setCompilerArgs(COMPILER_ARGS); compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider()); compileTask.getOptions().setEncoding("UTF-8"); + setJavaRelease(compileTask); }); p.getTasks().withType(JavaCompile.class) .matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME) @@ -91,9 +92,23 @@ public class JavaConventions { compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS); compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider()); compileTask.getOptions().setEncoding("UTF-8"); + setJavaRelease(compileTask); }); }); } + private void setJavaRelease(JavaCompile task) { + int defaultVersion = 17; + int releaseVersion = defaultVersion; + int compilerVersion = task.getJavaCompiler().get().getMetadata().getLanguageVersion().asInt(); + for (int version = defaultVersion ; version <= compilerVersion ; version++) { + if (task.getName().contains("Java" + version)) { + releaseVersion = version; + break; + } + } + task.getOptions().getRelease().set(releaseVersion); + } + }