Configure Spring JavaFormat in Gradle build

This commit creates a new build convention that applies the Spring
JavaFormat Gradle plugin.
This is also adding a default checkstyle configuration for this project.

See gh-54
This commit is contained in:
Brian Clozel
2021-06-01 20:55:09 +02:00
parent 266a2ddcda
commit fc3a6403cb
8 changed files with 173 additions and 0 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
build
!/**/src/**/build
.gradle
.idea/
out

38
buildSrc/build.gradle Normal file
View File

@@ -0,0 +1,38 @@
plugins {
id "java-gradle-plugin"
id "io.spring.javaformat" version "${javaFormatVersion}"
id "checkstyle"
}
repositories {
mavenCentral()
gradlePluginPortal()
maven { url "https://repo.spring.io/release" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}"
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}")
}
checkstyle {
def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")}
config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml")
toolVersion = 8.11
}
gradlePlugin {
plugins {
conventionsPlugin {
id = "org.springframework.graphql.conventions"
implementationClass = "org.springframework.graphql.build.ConventionsPlugin"
}
}
}
test {
useJUnitPlatform()
}

View File

@@ -0,0 +1 @@
javaFormatVersion=0.0.28

13
buildSrc/settings.gradle Normal file
View File

@@ -0,0 +1,13 @@
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "io.spring.javaformat") {
useModule "io.spring.javaformat:spring-javaformat-gradle-plugin:${requested.version}"
}
}
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2020-2021 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.graphql.build;
import io.spring.javaformat.gradle.FormatTask;
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
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.CheckstyleExtension;
import org.gradle.api.plugins.quality.CheckstylePlugin;
/**
* Conventions that are applied in the presence of the {@link JavaBasePlugin}. When the
* plugin is applied, the {@link SpringJavaFormatPlugin Spring Java Format} and
* {@link CheckstylePlugin Checkstyle}.
*
* @author Brian Clozel
*/
public class ConventionsPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getPlugins().withType(JavaBasePlugin.class, (java) -> applySpringJavaFormat(project));
}
private void applySpringJavaFormat(Project project) {
project.getPlugins().apply(SpringJavaFormatPlugin.class);
project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8"));
project.getPlugins().apply(CheckstylePlugin.class);
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
checkstyle.setToolVersion("8.43");
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));
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" "https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<!-- generated sources -->
<suppress files="[\\/]build[\\/]generated[\\/]sources[\\/]" checks=".*" />
</suppressions>

View File

@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="com.puppycrawl.tools.checkstyle.Checker">
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>
</module>
<module name="io.spring.javaformat.checkstyle.SpringChecks" />
<module name="com.puppycrawl.tools.checkstyle.TreeWalker">
<module name="io.spring.javaformat.checkstyle.check.SpringJUnit5Check" />
<module
name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">
<property name="id" value="mainCodeIllegalImportCheck"/>
<property name="regexp" value="true" />
<property name="illegalClasses"
value="^javax.annotation.PostConstruct"/>
</module>
<module
name="com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck">
<property name="file"
value="${config_loc}/import-control.xml" />
<property name="path" value="^.*[\\/]src[\\/]main[\\/].*$" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0"/>
<property name="format" value="org\.junit\.Assert\.assert" />
<property name="message"
value="Please use AssertJ imports." />
<property name="ignoreComments" value="true" />
</module>
<module
name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0" />
<property name="format"
value="assertThatExceptionOfType\((NullPointerException|IllegalArgumentException|IOException|IllegalStateException)\.class\)" />
<property name="message"
value="Please use specialized AssertJ assertThat*Exception method." />
<property name="ignoreComments" value="true" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0"/>
<property name="format" value="org\.mockito\.(Mockito|BDDMockito)\.(when|doThrow|doAnswer)" />
<property name="message"
value="Please use BDD-style (given, when, then) using BDDMockito imports." />
<property name="ignoreComments" value="true" />
</module>
<module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck">
<property name="publicOnlySinceTags" value="true" />
<property name="requireSinceTag" value="true" />
</module>
</module>
</module>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<!DOCTYPE import-control PUBLIC "-//Checkstyle//DTD ImportControl Configuration 1.4//EN" "https://checkstyle.org/dtds/import_control_1_4.dtd">
<import-control pkg="org.springframework.graphql">
<allow pkg=".*" regex="true" />
</import-control>