24
24
import org .gradle .api .Project ;
25
25
import org .gradle .api .plugins .JavaLibraryPlugin ;
26
26
import org .gradle .api .plugins .JavaPlugin ;
27
+ import org .gradle .api .plugins .JavaPluginExtension ;
27
28
import org .gradle .api .tasks .compile .JavaCompile ;
29
+ import org .gradle .jvm .toolchain .JavaLanguageVersion ;
30
+ import org .gradle .jvm .toolchain .JvmVendorSpec ;
28
31
29
32
/**
30
33
* {@link Plugin} that applies conventions for compiling Java sources in Spring Framework.
@@ -39,6 +42,19 @@ public class JavaConventions {
39
42
40
43
private static final List <String > TEST_COMPILER_ARGS ;
41
44
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
+
42
58
static {
43
59
List <String > commonCompilerArgs = Arrays .asList (
44
60
"-Xlint:serial" , "-Xlint:cast" , "-Xlint:classfile" , "-Xlint:dep-ann" ,
@@ -59,28 +75,64 @@ public class JavaConventions {
59
75
}
60
76
61
77
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
+ });
63
93
}
64
94
65
95
/**
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
67
97
* test sources.
68
98
* @param project the current project
69
99
*/
70
100
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 );
84
136
}
85
137
86
138
}
0 commit comments