2424import org .gradle .api .Project ;
2525import org .gradle .api .plugins .JavaLibraryPlugin ;
2626import org .gradle .api .plugins .JavaPlugin ;
27+ import org .gradle .api .plugins .JavaPluginExtension ;
2728import 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}
0 commit comments