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
This commit is contained in:
parent
c213724a47
commit
28273b9309
|
|
@ -11,6 +11,16 @@ The `org.springframework.build.conventions` plugin applies all conventions to th
|
||||||
* Configuring the Kotlin compiler, see `KotlinConventions`
|
* Configuring the Kotlin compiler, see `KotlinConventions`
|
||||||
* Configuring testing in the build with `TestConventions`
|
* 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
|
## Build Plugins
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ public class ConventionsPlugin implements Plugin<Project> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
|
project.getExtensions().create("springFramework", SpringFrameworkExtension.class);
|
||||||
new CheckstyleConventions().apply(project);
|
new CheckstyleConventions().apply(project);
|
||||||
new JavaConventions().apply(project);
|
new JavaConventions().apply(project);
|
||||||
new KotlinConventions().apply(project);
|
new KotlinConventions().apply(project);
|
||||||
|
|
|
||||||
|
|
@ -75,19 +75,25 @@ public class JavaConventions {
|
||||||
toolchain.getVendor().set(JvmVendorSpec.BELLSOFT);
|
toolchain.getVendor().set(JvmVendorSpec.BELLSOFT);
|
||||||
toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17));
|
toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17));
|
||||||
});
|
});
|
||||||
project.getTasks().withType(JavaCompile.class)
|
SpringFrameworkExtension frameworkExtension = project.getExtensions().getByType(SpringFrameworkExtension.class);
|
||||||
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME))
|
project.afterEvaluate(p -> {
|
||||||
.forEach(compileTask -> {
|
p.getTasks().withType(JavaCompile.class)
|
||||||
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
|
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_JAVA_TASK_NAME))
|
||||||
compileTask.getOptions().setEncoding("UTF-8");
|
.forEach(compileTask -> {
|
||||||
});
|
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
|
||||||
project.getTasks().withType(JavaCompile.class)
|
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
|
||||||
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
|
compileTask.getOptions().setEncoding("UTF-8");
|
||||||
|| compileTask.getName().equals("compileTestFixturesJava"))
|
});
|
||||||
.forEach(compileTask -> {
|
p.getTasks().withType(JavaCompile.class)
|
||||||
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
|
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
|
||||||
compileTask.getOptions().setEncoding("UTF-8");
|
|| compileTask.getName().equals("compileTestFixturesJava"))
|
||||||
});
|
.forEach(compileTask -> {
|
||||||
|
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
|
||||||
|
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
|
||||||
|
compileTask.getOptions().setEncoding("UTF-8");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<Boolean> enableJavaPreviewFeatures;
|
||||||
|
|
||||||
|
public SpringFrameworkExtension(Project project) {
|
||||||
|
this.enableJavaPreviewFeatures = project.getObjects().property(Boolean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Property<Boolean> getEnableJavaPreviewFeatures() {
|
||||||
|
return this.enableJavaPreviewFeatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineArgumentProvider asArgumentProvider() {
|
||||||
|
return () -> {
|
||||||
|
if (getEnableJavaPreviewFeatures().getOrElse(false)) {
|
||||||
|
return List.of("--enable-preview");
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -67,6 +67,8 @@ class TestConventions {
|
||||||
"--add-opens=java.base/java.util=ALL-UNNAMED",
|
"--add-opens=java.base/java.util=ALL-UNNAMED",
|
||||||
"-Xshare:off"
|
"-Xshare:off"
|
||||||
);
|
);
|
||||||
|
test.getJvmArgumentProviders().add(project.getExtensions()
|
||||||
|
.getByType(SpringFrameworkExtension.class).asArgumentProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureTestRetryPlugin(Project project, Test test) {
|
private void configureTestRetryPlugin(Project project, Test test) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue