Skip to content

Commit dece376

Browse files
committed
Compile Spring for GraphQL with Java 24
But targeting a Java 17 baseline for bytecode version.
1 parent a57fb15 commit dece376

File tree

1 file changed

+67
-15
lines changed

1 file changed

+67
-15
lines changed

buildSrc/src/main/java/org/springframework/graphql/build/conventions/JavaConventions.java

+67-15
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.gradle.api.Project;
2525
import org.gradle.api.plugins.JavaLibraryPlugin;
2626
import org.gradle.api.plugins.JavaPlugin;
27+
import org.gradle.api.plugins.JavaPluginExtension;
2728
import org.gradle.api.tasks.compile.JavaCompile;
29+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
30+
import org.gradle.jvm.toolchain.JvmVendorSpec;
2831

2932
/**
3033
* {@link Plugin} that applies conventions for compiling Java sources in Spring Framework.
@@ -39,6 +42,19 @@ public class JavaConventions {
3942

4043
private static final List<String> TEST_COMPILER_ARGS;
4144

45+
/**
46+
* The Java version we should use as the JVM baseline for building the project.
47+
* <p>NOTE: If you update this value, you should also update the value used in
48+
* the {@code javadoc} task in {@code framework-api.gradle}.
49+
*/
50+
private static final JavaLanguageVersion DEFAULT_LANGUAGE_VERSION = JavaLanguageVersion.of(24);
51+
52+
/**
53+
* The Java version we should use as the baseline for the compiled bytecode
54+
* (the "-release" compiler argument).
55+
*/
56+
private static final JavaLanguageVersion DEFAULT_RELEASE_VERSION = JavaLanguageVersion.of(17);
57+
4258
static {
4359
List<String> commonCompilerArgs = Arrays.asList(
4460
"-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann",
@@ -59,28 +75,64 @@ public class JavaConventions {
5975
}
6076

6177
public void apply(Project project) {
62-
project.getPlugins().withType(JavaLibraryPlugin.class, (javaPlugin) -> applyJavaCompileConventions(project));
78+
project.getPlugins().withType(JavaLibraryPlugin.class, (javaPlugin) -> {
79+
applyToolchainConventions(project);
80+
applyJavaCompileConventions(project);
81+
});
82+
}
83+
84+
/**
85+
* Configure the Toolchain support for the project.
86+
* @param project the current project
87+
*/
88+
private static void applyToolchainConventions(Project project) {
89+
project.getExtensions().getByType(JavaPluginExtension.class).toolchain(toolchain -> {
90+
toolchain.getVendor().set(JvmVendorSpec.BELLSOFT);
91+
toolchain.getLanguageVersion().set(DEFAULT_LANGUAGE_VERSION);
92+
});
6393
}
6494

6595
/**
66-
* Applies the common Java compiler options for main sources, test fixture sources, and
96+
* Apply the common Java compiler options for main sources, test fixture sources, and
6797
* test sources.
6898
* @param project the current project
6999
*/
70100
private void applyJavaCompileConventions(Project project) {
71-
project.getTasks().withType(JavaCompile.class)
72-
.matching((compileTask) -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME))
73-
.forEach((compileTask) -> {
74-
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
75-
compileTask.getOptions().setEncoding("UTF-8");
76-
});
77-
project.getTasks().withType(JavaCompile.class)
78-
.matching((compileTask) -> compileTask.getName().equals(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
79-
|| compileTask.getName().equals("compileTestFixturesJava"))
80-
.forEach((compileTask) -> {
81-
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
82-
compileTask.getOptions().setEncoding("UTF-8");
83-
});
101+
project.afterEvaluate(p -> {
102+
p.getTasks().withType(JavaCompile.class)
103+
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_JAVA_TASK_NAME))
104+
.forEach(compileTask -> {
105+
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
106+
compileTask.getOptions().setEncoding("UTF-8");
107+
setJavaRelease(compileTask);
108+
});
109+
p.getTasks().withType(JavaCompile.class)
110+
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
111+
|| compileTask.getName().equals("compileTestFixturesJava"))
112+
.forEach(compileTask -> {
113+
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
114+
compileTask.getOptions().setEncoding("UTF-8");
115+
setJavaRelease(compileTask);
116+
});
117+
118+
});
119+
}
120+
121+
/**
122+
* We should pick the {@link #DEFAULT_RELEASE_VERSION} for all compiled classes,
123+
* unless the current task is compiling multi-release JAR code with a higher version.
124+
*/
125+
private void setJavaRelease(JavaCompile task) {
126+
int defaultVersion = DEFAULT_RELEASE_VERSION.asInt();
127+
int releaseVersion = defaultVersion;
128+
int compilerVersion = task.getJavaCompiler().get().getMetadata().getLanguageVersion().asInt();
129+
for (int version = defaultVersion ; version <= compilerVersion ; version++) {
130+
if (task.getName().contains("Java" + version)) {
131+
releaseVersion = version;
132+
break;
133+
}
134+
}
135+
task.getOptions().getRelease().set(releaseVersion);
84136
}
85137

86138
}

0 commit comments

Comments
 (0)