Automatically Fix Checkstyle Violations
Most programmers agree that having consistent formatting across a code base makes it easier to work with. Tools like Checkstyle provide automated enforcement of these policies. But it's never fun to have your workflow interrupted by complaints about formatting. This guide will show you how to configure rewrite to automate the remediation of Checkstyle policy violations.

Example Configuration

How you configure Checkstyle integration depends on whether or not your build also applies a Checkstyle plugin. For Gradle builds this means the built-in plugin named Checkstyle Plugin. For Maven builds this means maven-checkstyle-plugin.

With Checkstyle Plugin

If your build uses either the Gradle or Maven checkstyle plugins, then good news, the rewrite build plugins will detect that and automatically match their configuration. All that remains is to activate the Code cleanup recipe. Activating that recipe in your build file and running rewrite is all you need to do to begin auto-remediating Checkstyle policy violations.
Maven
Gradle
pom.xml
1
<project>
2
<plugins>
3
<plugin>
4
<groupId>org.openrewrite.maven</groupId>
5
<artifactId>rewrite-maven-plugin</artifactId>
6
<version>4.13.0</version>
7
<configuration>
8
<activeRecipes>
9
<recipe>org.openrewrite.java.cleanup.Cleanup</recipe>
10
</activeRecipes>
11
</configuration>
12
</plugin>
13
<plugin>
14
<groupId>org.apache.maven.plugins</groupId>
15
<artifactId>maven-checkstyle-plugin</artifactId>
16
<version>3.1.2</version>
17
<configuration>
18
<configLocation>checkstyle.xml</configLocation>
19
</configuration>
20
</plugin>
21
</plugins>
22
</project>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("checkstyle")
4
id("org.openrewrite.rewrite") version("5.12.0")
5
}
6
7
rewrite {
8
activeRecipe("org.openrewrite.java.cleanup.Cleanup")
9
}
10
11
repositories {
12
mavenCentral() // rewrite is published to Maven Central
13
}
Copied!
At this point, you're ready to fix Checkstyle policy violations by running mvn rewrite:run or gradlew rewriteRun.
OpenRewrite can be configured to use different checkstyle policies than your Checkstyle plugin. Follow the "No Checkstyle Plugin" instructions to override the location of the checkstyle configuration file.

No Checkstyle Plugin

If your build does not use either of the Gradle or Maven Checkstyle plugins, you can still configure the rewrite build plugins to use a checkstyle xml configuration file.
Maven
Gradle
pom.xml
1
<project>
2
<plugins>
3
<plugin>
4
<groupId>org.openrewrite.maven</groupId>
5
<artifactId>rewrite-maven-plugin</artifactId>
6
<version>4.13.0</version>
7
<configuration>
8
<activeRecipes>
9
<recipe>org.openrewrite.java.cleanup.Cleanup</recipe>
10
</activeRecipes>
11
<checkstyleConfigFile>checkstyle.xml</checkstyleConfigFile>
12
</configuration>
13
</plugin>
14
</plugins>
15
</project>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("org.openrewrite.rewrite") version("5.12.0")
4
}
5
6
rewrite {
7
activeRecipe("org.openrewrite.java.cleanup.Cleanup")
8
checkstyleConfigFile = file("checkstyle.xml")
9
}
10
11
repositories {
12
mavenCentral() // rewrite is published to Maven Central
13
}
Copied!
At this point, you're ready to fix Checkstyle policy violations by running mvn rewrite:run or gradlew rewriteRun.

Applying Refactors before Checkstyle

Depending on exactly how your build is configured, it might run Checkstyle before OpenRewrite. So the Checkstyle tool might report violations that would be fixed if your build were to run OpenRewrite first.
With this configuration, rewrite refactoring will be run as part of any build that runs checkstyle.

Gradle

In Gradle, Task.dependsOn() can be used to ensure that invoking one task always causes another task to run first. That means making all tasks of type org.gradle.api.plugins.quality.Checkstyle depend on the rewriteRun task provided by the OpenRewrite plugin.
Gradle
build.gradle
1
plugins {
2
id("java")
3
id("checkstyle")
4
id("org.openrewrite.rewrite") version("5.12.0")
5
}
6
7
rewrite {
8
activeRecipe("org.openrewrite.java.cleanup.Cleanup")
9
checkstyleConfigFile = file("checkstyle.xml")
10
}
11
12
repositories {
13
mavenCentral() // rewrite is published to Maven Central
14
}
15
16
// In older versions of gradle, use tasks.get() instead instead of tasks.named()
17
def rewriteRunTask = tasks.named("rewriteRun")
18
19
// In a kotlin build script, use tasks.withType<Checkstyle>()
20
tasks.withType(Checkstyle) {
21
dependsOn(rewriteRunTask)
22
}
Copied!

Maven

In Maven, the ordering of goals depends first on which phase of the Build Lifecycle they are declared in, then secondarily by the order in which plugins are declared. This means that the rewrite-maven-plugin should be declared above the maven-checkstyle-plugin in your pom.xml and configured to execute within the same phase as you configure checkstyle to run its "check" goal.
Maven
pom.xml
1
<project>
2
<plugins>
3
<plugin>
4
<groupId>org.openrewrite.maven</groupId>
5
<artifactId>rewrite-maven-plugin</artifactId>
6
<version>4.13.0</version>
7
<configuration>
8
<activeRecipes>
9
<recipe>org.openrewrite.java.cleanup.Cleanup</recipe>
10
</activeRecipes>
11
</configuration>
12
<executions>
13
<execution>
14
<id>validate</id>
15
<phase>validate</phase>
16
<goals>
17
<goal>run</goal>
18
</goals>
19
</execution>
20
</executions>
21
</plugin>
22
<plugin>
23
<groupId>org.apache.maven.plugins</groupId>
24
<artifactId>maven-checkstyle-plugin</artifactId>
25
<version>3.1.2</version>
26
<configuration>
27
<configLocation>checkstyle.xml</configLocation>
28
</configuration>
29
<executions>
30
<execution>
31
<id>validate</id>
32
<phase>validate</phase>
33
<goals>
34
<goal>check</goal>
35
</goals>
36
</execution>
37
</executions>
38
</plugin>
39
</plugins>
40
</project>
Copied!

Known Limitations

We don't have OpenRewrite recipes implemented for all publicly available policies. If you find a checkstyle policy violation you'd like automated, visit the rewrite repository and file an issue.
Checkstyle configuration loading only informs the behavior of recipes, not which recipes are active. Regardless of the presence or non-presence of Checkstyle configuration, OpenRewrite will never apply any refactoring you do not explicitly activate.
Last modified 10d ago