Maven Checkstyle plugin with google_checks and 4 space indentSize

2.2k views Asked by At

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?

1

There are 1 answers

0
Sergey Vyacheslavovich Brunov On

Overview

Currently, Checkstyle does not support such configuration composition.

Here are some of the related GitHub issues:

  1. How to extend/override an existing configuration (sun, google) · Issue #4484 · checkstyle/checkstyle.

  2. 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:

  1. Create the first execution that uses the entire configuration file and suppress the checks to be overridden.
  2. 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>