Reputation: 365
I am finding out ways to configure google_checks to use 4 spaces
in the maven Checkstyle plugin. I set the indentSize
configuration parameter to 4, but it does not work. Is there a configuration options to set this? I don't want to have my own version of google_checks.xml
just to have 4 spaces indent.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<dependencies>
<dependency>
<artifactId>checkstyle</artifactId>
<groupId>com.puppycrawl.tools</groupId>
<version>8.36.1</version>
</dependency>
</dependencies>
<configuration>
<configLocation>google_checks.xml</configLocation>
<indentSize>4</indentSize>
<failsOnError>true</failsOnError>
<consoleOutput>true</consoleOutput>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Update: It seems like there is no way to have a single format that is compatible with maven-checkstyle-plugin
, Checkstyle with google_checks
and Intellij with google_java_format
. Has anyone been able to achieve this?
Upvotes: 7
Views: 2837
Reputation: 18106
Currently, Checkstyle does not support such configuration composition.
Here are some of the related GitHub issues:
How to extend/override an existing configuration (sun, google) · Issue #4484 · checkstyle/checkstyle.
There is a quite straightforward workaround to override some checks of a configuration file: split the single Checkstyle Maven plugin execution into two executions:
This workaround is also explained here: create concept of inheritance/override and compositions/extension of configs · Issue #2873 · checkstyle/checkstyle: The comment.
pom.xml
The /build/pluginManagement/plugins
plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.43</version>
</dependency>
</dependencies>
</plugin>
The /build/plugins
plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>check-google-checks</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<configLocation>google_checks.xml</configLocation>
<suppressionsLocation>maven-checkstyle-suppressions-google_checks.xml</suppressionsLocation>
<suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
</configuration>
</execution>
<execution>
<id>check-custom-checks</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<configLocation>maven-checkstyle-custom_checks.xml</configLocation>
</configuration>
</execution>
</executions>
<configuration>
<failsOnError>true</failsOnError>
<violationSeverity>warning</violationSeverity>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
</plugin>
maven-checkstyle-suppressions-google_checks.xml
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress checks="Indentation" files="." />
</suppressions>
maven-checkstyle-custom_checks.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="Indentation">
<property name="basicOffset" value="4" />
<property name="braceAdjustment" value="4" />
<property name="caseIndent" value="4" />
<property name="throwsIndent" value="4" />
<property name="lineWrappingIndentation" value="4" />
<property name="arrayInitIndent" value="4" />
</module>
</module>
</module>
Upvotes: 9