Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax to suppress specific checkstyle rules on certain files?

I am using Maven 3 and Checkstyle 2.9.1 in my java project and I have a common checkstyle configuration that we're using in several other projects as well. To avoid copying and editing that configuration file into my own project I use a suppressions file to disable all checks in some packages (generated code).

Now I want to suppress specific rules in all files.

This is my suppressions file:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <suppress files="my[\\/]generated[\\/]code[\\/]package" checks="."/>
    <suppress files=".*\\.java$" checks="IndentationCheck"/>
    <suppress files=".*\\.java$" checks="LineLengthCheck"/>
    <suppress files=".*\\.java$" checks="ExplicitInitializationCheck"/>
    <suppress files=".*\\.java$" checks="MemberNameCheck"/>
</suppressions>

This is my POM:

...
<build>
    ...
    <plugins>
    ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <configLocation>${checkstyle.ruleset}</configLocation>
                <suppressionsLocation>${checkstyle.suppressions}</suppressionsLocation>
            </configuration>
        </plugin>
    </plugins>
</build>

I'm then calling

mvn checkstyle:checkstyle

to generate the reports, but the suppressed warnings are still in there.

What am I doing wrong here?

like image 452
cringe Avatar asked Dec 14 '12 07:12

cringe


People also ask

What is Checkstyle suppression?

Checkstyle allows the definition of a list of files and their line ranges that should be suppressed from reporting any violations (known as a suppressions filter ).

How can I stop Checkstyle?

Then to disable Checkstyle, you use //CHECKSTYLE:OFF and //CHECKSTYLE:ON (default values) in your code. In this example, we want the disable it because we don't want to get the warning for the missing Javadoc tags for the getter/setter methods.

What is Checkstyle format?

Overview. Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.


1 Answers

Ok, I finally managed to build a working suppressions file for my checkstyle configuration.

My original problem was with the regex, so instead of files=".*\\.java$" I'm now using files="." to suppress special checks on all files. I also suppress all checks on certain files.

Here are some examples from my suppressions file:

<?xml version="1.0"?> suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <!-- suppress certain checks on all files in a package -->
    <suppress files="my[\\/]super[\\/]package[\\/]name" checks="ModifierOrderCheck|NeedBracesCheck|MagicNumberCheck"/>
    <!-- suppress all checks on all files in a package -->
    <suppress files="another[\\/]super[\\/]package[\\/]of[\\/]mine" checks=".*"/>
    <!-- suppress certain checks on all files -->
    <suppress files="." checks="IndentationCheck"/>
    <suppress files="." checks="LineLengthCheck"/>
    <suppress files="." checks="ExplicitInitializationCheck"/>
    <suppress files="." checks="MemberNameCheck"/>
    <suppress files="." checks="FinalClassCheck"/>
</suppressions>

If you need help with the Maven part of the configuration, see the answer of @Mawia.

like image 147
cringe Avatar answered Oct 13 '22 00:10

cringe