diff --git a/build.gradle b/build.gradle index 6a4f171ae5..244bc18946 100644 --- a/build.gradle +++ b/build.gradle @@ -49,7 +49,6 @@ configure([rootProject] + javaProjects) { project -> apply plugin: "java" apply plugin: "java-test-fixtures" - apply plugin: "checkstyle" apply plugin: 'org.springframework.build.conventions' apply from: "${rootDir}/gradle/toolchains.gradle" apply from: "${rootDir}/gradle/ide.gradle" @@ -77,18 +76,6 @@ configure([rootProject] + javaProjects) { project -> "--add-opens=java.base/java.util=ALL-UNNAMED"]) } - checkstyle { - toolVersion = "10.9.3" - configDirectory.set(rootProject.file("src/checkstyle")) - } - - tasks.named("checkstyleMain").configure { - maxHeapSize = "1g" - } - - tasks.named("checkstyleTest").configure { - maxHeapSize = "1g" - } dependencies { dependencyManagement(enforcedPlatform(dependencies.project(path: ":framework-platform"))) @@ -109,7 +96,6 @@ configure([rootProject] + javaProjects) { project -> // JSR-305 only used for non-required meta-annotations compileOnly("com.google.code.findbugs:jsr305") testCompileOnly("com.google.code.findbugs:jsr305") - checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.38") } ext.javadocLinks = [ @@ -168,8 +154,4 @@ configure(rootProject) { } } - tasks.named("checkstyleNohttp").configure { - maxHeapSize = "1g" - } - } diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index e21f9231a9..34561436fb 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -1,5 +1,7 @@ plugins { id 'java-gradle-plugin' + id 'checkstyle' + id 'io.spring.javaformat' version "${javaFormatVersion}" } repositories { @@ -17,10 +19,12 @@ ext { } dependencies { + checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}" implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}") implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}") implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0" implementation "org.gradle:test-retry-gradle-plugin:1.4.1" + implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}") } gradlePlugin { diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties index 160890028a..7d2fd63bba 100644 --- a/buildSrc/gradle.properties +++ b/buildSrc/gradle.properties @@ -1 +1,2 @@ org.gradle.caching=true +javaFormatVersion=0.0.38 diff --git a/buildSrc/src/main/java/org/springframework/build/CheckstyleConventions.java b/buildSrc/src/main/java/org/springframework/build/CheckstyleConventions.java new file mode 100644 index 0000000000..3c3c84929f --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/build/CheckstyleConventions.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2023 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 io.spring.javaformat.gradle.SpringJavaFormatPlugin; +import io.spring.javaformat.gradle.tasks.Format; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.DependencySet; +import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.plugins.quality.Checkstyle; +import org.gradle.api.plugins.quality.CheckstyleExtension; +import org.gradle.api.plugins.quality.CheckstylePlugin; + +/** + * {@link Plugin} that applies conventions for checkstyle. + * @author Brian Clozel + */ +public class CheckstyleConventions { + + /** + * Applies the Spring Java Format and Checkstyle plugins with the project conventions. + * @param project the current project + */ + public void apply(Project project) { + project.getPlugins().withType(JavaBasePlugin.class, (java) -> { + project.getPlugins().apply(CheckstylePlugin.class); + project.getTasks().withType(Checkstyle.class).forEach(checkstyle -> checkstyle.getMaxHeapSize().set("1g")); + CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class); + checkstyle.setToolVersion("10.9.1"); + 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)); + }); + } + +} diff --git a/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java index e54ddce559..61bfbbc479 100644 --- a/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java @@ -25,10 +25,8 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin; * Plugin to apply conventions to projects that are part of Spring Framework's build. * Conventions are applied in response to various plugins being applied. * - * When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions} - * are applied. - * When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions} - * are applied. + * When the {@link JavaBasePlugin} is applied, the conventions in {@link CheckstyleConventions}, + * {@link TestConventions} and {@link JavaConventions} are applied. * When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions} * are applied. * @@ -38,6 +36,7 @@ public class ConventionsPlugin implements Plugin { @Override public void apply(Project project) { + new CheckstyleConventions().apply(project); new JavaConventions().apply(project); new KotlinConventions().apply(project); new TestConventions().apply(project);