Skip to content

Commit 28273b9

Browse files
committed
Add build DSL extension to enable Java preview features
This commit adds a DSL Gradle extension for optionally enabling Java preview features in a specific project module. The "--enable-preview" JVM flag will be configured automatically for compile and test tasks where this is applied: ``` springFramework { enableJavaPreviewFeatures = true } ``` See gh-33616
1 parent c213724 commit 28273b9

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

buildSrc/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ The `org.springframework.build.conventions` plugin applies all conventions to th
1111
* Configuring the Kotlin compiler, see `KotlinConventions`
1212
* Configuring testing in the build with `TestConventions`
1313

14+
This plugin also provides a DSL extension to optionally enable Java preview features for
15+
compiling and testing sources in a module. This can be applied with the following in a
16+
module build file:
17+
18+
```groovy
19+
springFramework {
20+
enableJavaPreviewFeatures = true
21+
}
22+
```
23+
1424

1525
## Build Plugins
1626

buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ConventionsPlugin implements Plugin<Project> {
3636

3737
@Override
3838
public void apply(Project project) {
39+
project.getExtensions().create("springFramework", SpringFrameworkExtension.class);
3940
new CheckstyleConventions().apply(project);
4041
new JavaConventions().apply(project);
4142
new KotlinConventions().apply(project);

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,25 @@ private void applyJavaCompileConventions(Project project) {
7575
toolchain.getVendor().set(JvmVendorSpec.BELLSOFT);
7676
toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17));
7777
});
78-
project.getTasks().withType(JavaCompile.class)
79-
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME))
80-
.forEach(compileTask -> {
81-
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
82-
compileTask.getOptions().setEncoding("UTF-8");
83-
});
84-
project.getTasks().withType(JavaCompile.class)
85-
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
86-
|| compileTask.getName().equals("compileTestFixturesJava"))
87-
.forEach(compileTask -> {
88-
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
89-
compileTask.getOptions().setEncoding("UTF-8");
90-
});
78+
SpringFrameworkExtension frameworkExtension = project.getExtensions().getByType(SpringFrameworkExtension.class);
79+
project.afterEvaluate(p -> {
80+
p.getTasks().withType(JavaCompile.class)
81+
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_JAVA_TASK_NAME))
82+
.forEach(compileTask -> {
83+
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
84+
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
85+
compileTask.getOptions().setEncoding("UTF-8");
86+
});
87+
p.getTasks().withType(JavaCompile.class)
88+
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
89+
|| compileTask.getName().equals("compileTestFixturesJava"))
90+
.forEach(compileTask -> {
91+
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
92+
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
93+
compileTask.getOptions().setEncoding("UTF-8");
94+
});
95+
96+
});
9197
}
9298

9399
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.gradle.api.Project;
23+
import org.gradle.api.provider.Property;
24+
import org.gradle.process.CommandLineArgumentProvider;
25+
26+
public class SpringFrameworkExtension {
27+
28+
private final Property<Boolean> enableJavaPreviewFeatures;
29+
30+
public SpringFrameworkExtension(Project project) {
31+
this.enableJavaPreviewFeatures = project.getObjects().property(Boolean.class);
32+
}
33+
34+
public Property<Boolean> getEnableJavaPreviewFeatures() {
35+
return this.enableJavaPreviewFeatures;
36+
}
37+
38+
public CommandLineArgumentProvider asArgumentProvider() {
39+
return () -> {
40+
if (getEnableJavaPreviewFeatures().getOrElse(false)) {
41+
return List.of("--enable-preview");
42+
}
43+
return Collections.emptyList();
44+
};
45+
}
46+
}

buildSrc/src/main/java/org/springframework/build/TestConventions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private void configureTests(Project project, Test test) {
6767
"--add-opens=java.base/java.util=ALL-UNNAMED",
6868
"-Xshare:off"
6969
);
70+
test.getJvmArgumentProviders().add(project.getExtensions()
71+
.getByType(SpringFrameworkExtension.class).asArgumentProvider());
7072
}
7173

7274
private void configureTestRetryPlugin(Project project, Test test) {

0 commit comments

Comments
 (0)