Skip to content

Commit ed4404f

Browse files
committed
Move checkstyle config to Gradle convention
This commit moves the checkstyle conventions from the build.gradle script to a buildSrc convention, ensuring that the same configuration is applied to all checkstyle tasks. See gh-30339
1 parent bd55b64 commit ed4404f

File tree

5 files changed

+61
-22
lines changed

5 files changed

+61
-22
lines changed

build.gradle

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ configure([rootProject] + javaProjects) { project ->
4949

5050
apply plugin: "java"
5151
apply plugin: "java-test-fixtures"
52-
apply plugin: "checkstyle"
5352
apply plugin: 'org.springframework.build.conventions'
5453
apply from: "${rootDir}/gradle/toolchains.gradle"
5554
apply from: "${rootDir}/gradle/ide.gradle"
@@ -77,18 +76,6 @@ configure([rootProject] + javaProjects) { project ->
7776
"--add-opens=java.base/java.util=ALL-UNNAMED"])
7877
}
7978

80-
checkstyle {
81-
toolVersion = "10.9.3"
82-
configDirectory.set(rootProject.file("src/checkstyle"))
83-
}
84-
85-
tasks.named("checkstyleMain").configure {
86-
maxHeapSize = "1g"
87-
}
88-
89-
tasks.named("checkstyleTest").configure {
90-
maxHeapSize = "1g"
91-
}
9279

9380
dependencies {
9481
dependencyManagement(enforcedPlatform(dependencies.project(path: ":framework-platform")))
@@ -109,7 +96,6 @@ configure([rootProject] + javaProjects) { project ->
10996
// JSR-305 only used for non-required meta-annotations
11097
compileOnly("com.google.code.findbugs:jsr305")
11198
testCompileOnly("com.google.code.findbugs:jsr305")
112-
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.38")
11399
}
114100

115101
ext.javadocLinks = [
@@ -168,8 +154,4 @@ configure(rootProject) {
168154
}
169155
}
170156

171-
tasks.named("checkstyleNohttp").configure {
172-
maxHeapSize = "1g"
173-
}
174-
175157
}

buildSrc/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
plugins {
22
id 'java-gradle-plugin'
3+
id 'checkstyle'
4+
id 'io.spring.javaformat' version "${javaFormatVersion}"
35
}
46

57
repositories {
@@ -17,10 +19,12 @@ ext {
1719
}
1820

1921
dependencies {
22+
checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}"
2023
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
2124
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
2225
implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0"
2326
implementation "org.gradle:test-retry-gradle-plugin:1.4.1"
27+
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}")
2428
}
2529

2630
gradlePlugin {

buildSrc/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.gradle.caching=true
2+
javaFormatVersion=0.0.38
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-2023 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 io.spring.javaformat.gradle.SpringJavaFormatPlugin;
20+
import io.spring.javaformat.gradle.tasks.Format;
21+
import org.gradle.api.Plugin;
22+
import org.gradle.api.Project;
23+
import org.gradle.api.artifacts.DependencySet;
24+
import org.gradle.api.plugins.JavaBasePlugin;
25+
import org.gradle.api.plugins.quality.Checkstyle;
26+
import org.gradle.api.plugins.quality.CheckstyleExtension;
27+
import org.gradle.api.plugins.quality.CheckstylePlugin;
28+
29+
/**
30+
* {@link Plugin} that applies conventions for checkstyle.
31+
* @author Brian Clozel
32+
*/
33+
public class CheckstyleConventions {
34+
35+
/**
36+
* Applies the Spring Java Format and Checkstyle plugins with the project conventions.
37+
* @param project the current project
38+
*/
39+
public void apply(Project project) {
40+
project.getPlugins().withType(JavaBasePlugin.class, (java) -> {
41+
project.getPlugins().apply(CheckstylePlugin.class);
42+
project.getTasks().withType(Checkstyle.class).forEach(checkstyle -> checkstyle.getMaxHeapSize().set("1g"));
43+
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
44+
checkstyle.setToolVersion("10.9.1");
45+
checkstyle.getConfigDirectory().set(project.getRootProject().file("src/checkstyle"));
46+
String version = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion();
47+
DependencySet checkstyleDependencies = project.getConfigurations().getByName("checkstyle").getDependencies();
48+
checkstyleDependencies
49+
.add(project.getDependencies().create("io.spring.javaformat:spring-javaformat-checkstyle:" + version));
50+
});
51+
}
52+
53+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
* Plugin to apply conventions to projects that are part of Spring Framework's build.
2626
* Conventions are applied in response to various plugins being applied.
2727
*
28-
* When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions}
29-
* are applied.
30-
* When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions}
31-
* are applied.
28+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link CheckstyleConventions},
29+
* {@link TestConventions} and {@link JavaConventions} are applied.
3230
* When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions}
3331
* are applied.
3432
*
@@ -38,6 +36,7 @@ public class ConventionsPlugin implements Plugin<Project> {
3836

3937
@Override
4038
public void apply(Project project) {
39+
new CheckstyleConventions().apply(project);
4140
new JavaConventions().apply(project);
4241
new KotlinConventions().apply(project);
4342
new TestConventions().apply(project);

0 commit comments

Comments
 (0)