Overview
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.
create concept of inheritance/override and compositions/extension of configs · Issue #2873 · checkstyle/checkstyle.
- Please, note that this is an active issue.
A workaround
There is a quite straightforward workaround to override some checks of a configuration file: split the single Checkstyle Maven plugin execution into two executions:
- Create the first execution that uses the entire configuration file and suppress the checks to be overridden.
- Create the second execution that uses the customised configuration file with the «overridden» checks only.
This workaround is also explained here: create concept of inheritance/override and compositions/extension of configs · Issue #2873 · checkstyle/checkstyle: The comment.
Indentation-related example (draft)
Project: 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>
Checkstyle configuration file: 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>
Checkstyle configuration file: 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>