diff --git a/buildSrc/README.md b/buildSrc/README.md index 9e35b5b766c..260ebcf24ee 100644 --- a/buildSrc/README.md +++ b/buildSrc/README.md @@ -11,6 +11,16 @@ The `org.springframework.build.conventions` plugin applies all conventions to th * Configuring the Kotlin compiler, see `KotlinConventions` * Configuring testing in the build with `TestConventions` +This plugin also provides a DSL extension to optionally enable Java preview features for +compiling and testing sources in a module. This can be applied with the following in a +module build file: + +```groovy +springFramework { + enableJavaPreviewFeatures = true +} +``` + ## Build Plugins diff --git a/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java index 34978ba377d..3e63c16ed42 100644 --- a/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java @@ -36,6 +36,7 @@ public class ConventionsPlugin implements Plugin { @Override public void apply(Project project) { + project.getExtensions().create("springFramework", SpringFrameworkExtension.class); new CheckstyleConventions().apply(project); new JavaConventions().apply(project); new KotlinConventions().apply(project); diff --git a/buildSrc/src/main/java/org/springframework/build/JavaConventions.java b/buildSrc/src/main/java/org/springframework/build/JavaConventions.java index 60b791799f5..60581d44cb9 100644 --- a/buildSrc/src/main/java/org/springframework/build/JavaConventions.java +++ b/buildSrc/src/main/java/org/springframework/build/JavaConventions.java @@ -75,19 +75,25 @@ public class JavaConventions { toolchain.getVendor().set(JvmVendorSpec.BELLSOFT); toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17)); }); - project.getTasks().withType(JavaCompile.class) - .matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME)) - .forEach(compileTask -> { - compileTask.getOptions().setCompilerArgs(COMPILER_ARGS); - compileTask.getOptions().setEncoding("UTF-8"); - }); - project.getTasks().withType(JavaCompile.class) - .matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME) - || compileTask.getName().equals("compileTestFixturesJava")) - .forEach(compileTask -> { - compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS); - compileTask.getOptions().setEncoding("UTF-8"); - }); + SpringFrameworkExtension frameworkExtension = project.getExtensions().getByType(SpringFrameworkExtension.class); + project.afterEvaluate(p -> { + p.getTasks().withType(JavaCompile.class) + .matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_JAVA_TASK_NAME)) + .forEach(compileTask -> { + compileTask.getOptions().setCompilerArgs(COMPILER_ARGS); + compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider()); + compileTask.getOptions().setEncoding("UTF-8"); + }); + p.getTasks().withType(JavaCompile.class) + .matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME) + || compileTask.getName().equals("compileTestFixturesJava")) + .forEach(compileTask -> { + compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS); + compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider()); + compileTask.getOptions().setEncoding("UTF-8"); + }); + + }); } } diff --git a/buildSrc/src/main/java/org/springframework/build/SpringFrameworkExtension.java b/buildSrc/src/main/java/org/springframework/build/SpringFrameworkExtension.java new file mode 100644 index 00000000000..78dae151e45 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/build/SpringFrameworkExtension.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.build; + +import java.util.Collections; +import java.util.List; + +import org.gradle.api.Project; +import org.gradle.api.provider.Property; +import org.gradle.process.CommandLineArgumentProvider; + +public class SpringFrameworkExtension { + + private final Property enableJavaPreviewFeatures; + + public SpringFrameworkExtension(Project project) { + this.enableJavaPreviewFeatures = project.getObjects().property(Boolean.class); + } + + public Property getEnableJavaPreviewFeatures() { + return this.enableJavaPreviewFeatures; + } + + public CommandLineArgumentProvider asArgumentProvider() { + return () -> { + if (getEnableJavaPreviewFeatures().getOrElse(false)) { + return List.of("--enable-preview"); + } + return Collections.emptyList(); + }; + } +} diff --git a/buildSrc/src/main/java/org/springframework/build/TestConventions.java b/buildSrc/src/main/java/org/springframework/build/TestConventions.java index 1283d233765..715abe0d702 100644 --- a/buildSrc/src/main/java/org/springframework/build/TestConventions.java +++ b/buildSrc/src/main/java/org/springframework/build/TestConventions.java @@ -67,6 +67,8 @@ class TestConventions { "--add-opens=java.base/java.util=ALL-UNNAMED", "-Xshare:off" ); + test.getJvmArgumentProviders().add(project.getExtensions() + .getByType(SpringFrameworkExtension.class).asArgumentProvider()); } private void configureTestRetryPlugin(Project project, Test test) {