Skip to content

Commit 0407370

Browse files
committed
Update build pipeline for multi-release
1 parent 2e54ef1 commit 0407370

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

.github/actions/build-and-publish/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: 'Builds the project, optionally publishing it to a local deployment
33
inputs:
44
java-version:
55
required: false
6-
default: '17'
6+
default: '24'
77
description: 'The Java version to compile and test with'
88
java-distribution:
99
required: false

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
java:
1919
- version: 21
2020
toolchain: true
21-
- version: 22
21+
- version: 24
2222
toolchain: true
2323
steps:
2424
- name: Check Out Code

build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ configure(moduleProjects) {
4040
apply plugin: 'java-library'
4141
apply plugin: 'java-test-fixtures'
4242

43-
java {
44-
toolchain {
45-
languageVersion = JavaLanguageVersion.of(17)
46-
}
47-
}
48-
4943
configurations {
5044
dependencyManagement {
5145
canBeConsumed = false

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

Lines changed: 65 additions & 14 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,7 +75,21 @@ 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
/**
@@ -68,19 +98,40 @@ public void apply(Project project) {
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+
* We should pick the {@link #DEFAULT_RELEASE_VERSION} for all compiled classes,
122+
* unless the current task is compiling multi-release JAR code with a higher version.
123+
*/
124+
private void setJavaRelease(JavaCompile task) {
125+
int defaultVersion = DEFAULT_RELEASE_VERSION.asInt();
126+
int releaseVersion = defaultVersion;
127+
int compilerVersion = task.getJavaCompiler().get().getMetadata().getLanguageVersion().asInt();
128+
for (int version = defaultVersion ; version <= compilerVersion ; version++) {
129+
if (task.getName().contains("Java" + version)) {
130+
releaseVersion = version;
131+
break;
132+
}
133+
}
134+
task.getOptions().getRelease().set(releaseVersion);
84135
}
85136

86137
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pluginManagement {
1313
}
1414

1515
plugins {
16+
id "org.gradle.toolchains.foojay-resolver-convention" version "1.0.0"
1617
id "io.spring.develocity.conventions" version "0.0.22"
1718
}
1819

0 commit comments

Comments
 (0)