Skip to content

Apply unused io.spring.javaformat plugin #35094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
plugins {
id 'java-gradle-plugin'
id 'checkstyle'
id 'io.spring.javaformat' version "${javaFormatVersion}"
}

repositories {
mavenCentral()
gradlePluginPortal()
}

tasks.named('compileJava') {
Copy link
Author

@Pankraz76 Pankraz76 Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to bind this properly, please? @bclozel

only the checkFormat goal is executed by default, but not the format.

dependsOn 'format'
mustRunAfter 'format'
}

ext {
def propertiesFile = new File(new File("$projectDir").parentFile, "gradle.properties")
propertiesFile.withInputStream {
Expand Down Expand Up @@ -64,4 +70,4 @@ test {
useJUnitPlatform()
}

jar.dependsOn check
jar.dependsOn check
2 changes: 1 addition & 1 deletion buildSrc/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.gradle.caching=true
assertjVersion=3.27.3
javaFormatVersion=0.0.43
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does renovate not take care about this?

javaFormatVersion=0.0.47
junitVersion=5.12.2
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@ public void apply(Project project) {
checkstyle.setToolVersion("10.25.0");
checkstyle.getConfigDirectory().set(project.getRootProject().file("src/checkstyle"));
String version = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion();
DependencySet checkstyleDependencies = project.getConfigurations().getByName("checkstyle").getDependencies();
checkstyleDependencies.add(
project.getDependencies().create("io.spring.javaformat:spring-javaformat-checkstyle:" + version));
DependencySet checkstyleDependencies = project.getConfigurations()
.getByName("checkstyle")
.getDependencies();
checkstyleDependencies
.add(project.getDependencies().create("io.spring.javaformat:spring-javaformat-checkstyle:" + version));
});
}

private static void configureNoHttpPlugin(Project project) {
project.getPlugins().apply(NoHttpPlugin.class);
NoHttpExtension noHttp = project.getExtensions().getByType(NoHttpExtension.class);
noHttp.setAllowlistFile(project.file("src/nohttp/allowlist.lines"));
noHttp.getSource().exclude("**/test-output/**", "**/.settings/**", "**/.classpath",
"**/.project", "**/.gradle/**", "**/node_modules/**", "**/spring-jcl/**", "buildSrc/build/**");
noHttp.getSource()
.exclude("**/test-output/**", "**/.settings/**", "**/.classpath", "**/.project", "**/.gradle/**",
"**/node_modules/**", "**/spring-jcl/**", "buildSrc/build/**");
List<String> buildFolders = List.of("bin", "build", "out");
project.allprojects(subproject -> {
Path rootPath = project.getRootDir().toPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
* Plugin to apply conventions to projects that are part of Spring Framework's build.
* Conventions are applied in response to various plugins being applied.
*
* <p>When the {@link JavaBasePlugin} is applied, the conventions in {@link CheckstyleConventions},
* {@link TestConventions} and {@link JavaConventions} are applied.
* The {@link ArchitecturePlugin} plugin is also applied.
* When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions}
* are applied.
* <p>
* When the {@link JavaBasePlugin} is applied, the conventions in
* {@link CheckstyleConventions}, {@link TestConventions} and {@link JavaConventions} are
* applied. The {@link ArchitecturePlugin} plugin is also applied. When the
* {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions} are
* applied.
*
* @author Brian Clozel
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,26 @@ public class JavaConventions {

/**
* The Java version we should use as the JVM baseline for building the project.
* <p>NOTE: If you update this value, you should also update the value used in
* the {@code javadoc} task in {@code framework-api.gradle}.
* <p>
* NOTE: If you update this value, you should also update the value used in the
* {@code javadoc} task in {@code framework-api.gradle}.
*/
private static final JavaLanguageVersion DEFAULT_LANGUAGE_VERSION = JavaLanguageVersion.of(24);

/**
* The Java version we should use as the baseline for the compiled bytecode
* (the "-release" compiler argument).
* The Java version we should use as the baseline for the compiled bytecode (the
* "-release" compiler argument).
*/
private static final JavaLanguageVersion DEFAULT_RELEASE_VERSION = JavaLanguageVersion.of(17);

static {
List<String> commonCompilerArgs = List.of(
"-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann",
"-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides",
"-Xlint:path", "-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options",
"-parameters"
);
List<String> commonCompilerArgs = List.of("-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann",
"-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides", "-Xlint:path",
"-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options", "-parameters");
COMPILER_ARGS = new ArrayList<>();
COMPILER_ARGS.addAll(commonCompilerArgs);
COMPILER_ARGS.addAll(List.of(
"-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
"-Xlint:unchecked", "-Werror"
));
COMPILER_ARGS.addAll(List.of("-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
"-Xlint:unchecked", "-Werror"));
TEST_COMPILER_ARGS = new ArrayList<>();
TEST_COMPILER_ARGS.addAll(commonCompilerArgs);
TEST_COMPILER_ARGS.addAll(List.of("-Xlint:-varargs", "-Xlint:-fallthrough", "-Xlint:-rawtypes",
Expand Down Expand Up @@ -96,21 +92,23 @@ private static void applyToolchainConventions(Project project) {
*/
private void applyJavaCompileConventions(Project project) {
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().setEncoding("UTF-8");
setJavaRelease(compileTask);
});
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().setEncoding("UTF-8");
setJavaRelease(compileTask);
});
p.getTasks()
.withType(JavaCompile.class)
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_JAVA_TASK_NAME))
.forEach(compileTask -> {
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
compileTask.getOptions().setEncoding("UTF-8");
setJavaRelease(compileTask);
});
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().setEncoding("UTF-8");
setJavaRelease(compileTask);
});

});
}
Expand All @@ -123,7 +121,7 @@ private void setJavaRelease(JavaCompile task) {
int defaultVersion = DEFAULT_RELEASE_VERSION.asInt();
int releaseVersion = defaultVersion;
int compilerVersion = task.getJavaCompiler().get().getMetadata().getLanguageVersion().asInt();
for (int version = defaultVersion ; version <= compilerVersion ; version++) {
for (int version = defaultVersion; version <= compilerVersion; version++) {
if (task.getName().contains("Java" + version)) {
releaseVersion = version;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
public class KotlinConventions {

void apply(Project project) {
project.getPlugins().withId("org.jetbrains.kotlin.jvm",
(plugin) -> project.getTasks().withType(KotlinCompile.class, this::configure));
project.getPlugins()
.withId("org.jetbrains.kotlin.jvm",
(plugin) -> project.getTasks().withType(KotlinCompile.class, this::configure));
}

private void configure(KotlinCompile compile) {
Expand All @@ -39,11 +40,14 @@ private void configure(KotlinCompile compile) {
options.getJvmTarget().set(JvmTarget.JVM_17);
options.getJavaParameters().set(true);
options.getAllWarningsAsErrors().set(true);
options.getFreeCompilerArgs().addAll(
"-Xsuppress-version-warnings",
"-Xjsr305=strict", // For dependencies using JSR 305
"-opt-in=kotlin.RequiresOptIn",
"-Xjdk-release=17" // Needed due to https://youtrack.jetbrains.com/issue/KT-49746
options.getFreeCompilerArgs()
.addAll("-Xsuppress-version-warnings", "-Xjsr305=strict", // For
// dependencies
// using JSR
// 305
"-opt-in=kotlin.RequiresOptIn", "-Xjdk-release=17" // Needed due
// to
// https://youtrack.jetbrains.com/issue/KT-49746
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public class SpringFrameworkExtension {

public SpringFrameworkExtension(Project project) {
this.enableJavaPreviewFeatures = project.getObjects().property(Boolean.class);
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile ->
javaCompile.getOptions().getCompilerArgumentProviders().add(asArgumentProvider()));
project.getTasks().withType(Test.class).configureEach(test ->
test.getJvmArgumentProviders().add(asArgumentProvider()));
project.getTasks()
.withType(JavaCompile.class)
.configureEach(
javaCompile -> javaCompile.getOptions().getCompilerArgumentProviders().add(asArgumentProvider()));
project.getTasks()
.withType(Test.class)
.configureEach(test -> test.getJvmArgumentProviders().add(asArgumentProvider()));

}

Expand All @@ -50,4 +53,5 @@ private CommandLineArgumentProvider asArgumentProvider() {
return Collections.emptyList();
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* Conventions that are applied in the presence of the {@link JavaBasePlugin}. When the
* plugin is applied:
* <ul>
* <li>The {@link TestRetryPlugin Test Retry} plugin is applied so that flaky tests
* are retried 3 times when running on the CI server.
* <li>The {@link TestRetryPlugin Test Retry} plugin is applied so that flaky tests are
* retried 3 times when running on the CI server.
* </ul>
*
* @author Brian Clozel
Expand All @@ -45,11 +45,10 @@ void apply(Project project) {
}

private void configureTestConventions(Project project) {
project.getTasks().withType(Test.class,
test -> {
configureTests(project, test);
configureTestRetryPlugin(project, test);
});
project.getTasks().withType(Test.class, test -> {
configureTests(project, test);
configureTestRetryPlugin(project, test);
});
}

private void configureTests(Project project, Test test) {
Expand All @@ -60,18 +59,12 @@ private void configureTests(Project project, Test test) {
}
});
test.include("**/*Tests.class", "**/*Test.class");
test.setSystemProperties(Map.of(
"java.awt.headless", "true",
"io.netty.leakDetection.level", "paranoid"
));
test.setSystemProperties(Map.of("java.awt.headless", "true", "io.netty.leakDetection.level", "paranoid"));
if (project.hasProperty("testGroups")) {
test.systemProperty("testGroups", project.getProperties().get("testGroups"));
}
test.jvmArgs(
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"-Xshare:off"
);
test.jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED",
"-Xshare:off");
}

private void configureTestRetryPlugin(Project project, Test test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public abstract class ArchitectureCheck extends DefaultTask {
public ArchitectureCheck() {
getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
getProhibitObjectsRequireNonNull().convention(true);
getRules().addAll(packageInfoShouldBeNullMarked(),
classesShouldNotImportForbiddenTypes(),
javaClassesShouldNotImportKotlinAnnotations(),
allPackagesShouldBeFreeOfTangles(),
getRules().addAll(packageInfoShouldBeNullMarked(), classesShouldNotImportForbiddenTypes(),
javaClassesShouldNotImportKotlinAnnotations(), allPackagesShouldBeFreeOfTangles(),
noClassesShouldCallStringToLowerCaseWithoutLocale(),
noClassesShouldCallStringToUpperCaseWithoutLocale());
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
Expand All @@ -76,12 +74,12 @@ public ArchitectureCheck() {
@TaskAction
void checkArchitecture() throws IOException {
JavaClasses javaClasses = new ClassFileImporter()
.importPaths(this.classes.getFiles().stream().map(File::toPath).toList());
.importPaths(this.classes.getFiles().stream().map(File::toPath).toList());
List<EvaluationResult> violations = getRules().get()
.stream()
.map((rule) -> rule.evaluate(javaClasses))
.filter(EvaluationResult::hasViolation)
.toList();
.stream()
.map((rule) -> rule.evaluate(javaClasses))
.filter(EvaluationResult::hasViolation)
.toList();
File outputFile = getOutputDirectory().file("failure-report.txt").get().getAsFile();
outputFile.getParentFile().mkdirs();
if (!violations.isEmpty()) {
Expand Down Expand Up @@ -134,4 +132,5 @@ final FileTree getInputClasses() {
// The rules themselves can't be an input as they aren't serializable so we use
// their descriptions instead
abstract ListProperty<String> getRuleDescriptions();

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ private void registerTasks(Project project) {
continue;
}
TaskProvider<ArchitectureCheck> checkArchitecture = project.getTasks()
.register(taskName(sourceSet), ArchitectureCheck.class,
(task) -> {
task.setClasses(sourceSet.getOutput().getClassesDirs());
task.getResourcesDirectory().set(sourceSet.getOutput().getResourcesDir());
task.dependsOn(sourceSet.getProcessResourcesTaskName());
task.setDescription("Checks the architecture of the classes of the " + sourceSet.getName()
+ " source set.");
task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
});
.register(taskName(sourceSet), ArchitectureCheck.class, (task) -> {
task.setClasses(sourceSet.getOutput().getClassesDirs());
task.getResourcesDirectory().set(sourceSet.getOutput().getResourcesDir());
task.dependsOn(sourceSet.getProcessResourcesTaskName());
task.setDescription(
"Checks the architecture of the classes of the " + sourceSet.getName() + " source set.");
task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
});
architectureChecks.add(checkArchitecture);
}
if (!architectureChecks.isEmpty()) {
Expand All @@ -66,8 +65,7 @@ private void registerTasks(Project project) {
}

private static String taskName(SourceSet sourceSet) {
return "checkArchitecture"
+ sourceSet.getName().substring(0, 1).toUpperCase()
return "checkArchitecture" + sourceSet.getName().substring(0, 1).toUpperCase()
+ sourceSet.getName().substring(1);
}

Expand Down
Loading