Skip to content

Commit ce90154

Browse files
committed
Compile Spring for GraphQL with Java 24
But targeting a Java 17 baseline for bytecode version. Closes gh-1206
1 parent 60b3e16 commit ce90154

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

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

Lines changed: 67 additions & 15 deletions
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
}

spring-graphql-docs/build.gradle

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ javadoc {
4141
enabled = false
4242
}
4343

44-
4544
/**
4645
* Produce Javadoc for all Spring for GraphQL modules in "build/docs/javadoc"
4746
*/
48-
task api(type: Javadoc) {
47+
tasks.register("api", Javadoc) {
48+
4949
group = "Documentation"
5050
description = "Generates aggregated Javadoc API documentation."
5151
title = "${rootProject.description} ${version} API"
@@ -74,6 +74,9 @@ task api(type: Javadoc) {
7474
}.sum()
7575
maxMemory = "1024m"
7676
destinationDir = file("$buildDir/docs/javadoc")
77+
javadocTool.set(javaToolchains.javadocToolFor({
78+
languageVersion = JavaLanguageVersion.of(24)
79+
}))
7780
}
7881

7982

@@ -98,7 +101,8 @@ tasks.named("antora") {
98101
/**
99102
* Zip all docs into a single archive
100103
*/
101-
task docsZip(type: Zip, dependsOn: ['api']) {
104+
tasks.register("docsZip", Zip) {
105+
dependsOn = ['api']
102106
group = "Distribution"
103107
description = "Builds -${archiveClassifier} archive containing api and reference " +
104108
"for deployment at https://docs.spring.io/spring-graphql/docs."

0 commit comments

Comments
 (0)