Check style custom indentation

48 Views Asked by At

TLDR;

How to configure indentation and other configuration for maven build plugin by modifying google_checks.xml.

Details:

POM snipets:

...
    <build>
        <plugins>
        ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <configLocation>google_checks.xml</configLocation>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnError>
                    <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>
...

https://raw.githubusercontent.com/checkstyle/checkstyle/master/src/main/resources/google_checks.xml

Sniper from google_checks.xml

 <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>

SomeClass.java

public class SomeClass<T> implements SomeInterface,
                                     SomeInterface1 {

    private final CommentTableModel delegate; // Line with error. 

member def modifier' has incorrect indentation level 4, expected level should be 2.

The problem to solve:

I want it to remain at 4 ... I want check style to not report it as error. How can I do that?

2

There are 2 best solutions below

0
Rob Spoor On

Checkstyle does not (yet) support inheriting or embedding configuration (apart from using external DTDs, which is disabled by default because of security concerns). See https://github.com/checkstyle/checkstyle/issues/2873 for a discussion about adding it.

As far as I know, the only way to currently do this is to copy the existing configuration, tweak it as necessary, and use that instead. That does mean that you need to actively maintain the file, because any changes to the provided config will need to be copied to your own.

0
zur On
  <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>10.9.0</version>
                    </dependency>
                </dependencies>

Adding above to plugin fixed the problem. Thanks.