14

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?

2 Answers 2

19

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me.

Instead of doing

<suppressionsLocation>${checkstyle.suppressions}</suppressionsLocation>

I put this in ${checkstyle.ruleset} (which I assume is your xml file for check style)

<module name="SuppressionFilter">
    <property name="file" value="${checkstyle.suppressions}" />
</module>

And my ${checkstyle.suppressions} has

<suppressions>
    <suppress files="package-info.java" checks="[a-zA-Z0-9]*"/>
    <suppress files="SomeClass.java" checks="[a-zA-Z0-9]*"/>
</suppressions>

2 Comments

Well, the suppressions file is recognized and used. But the actual suppression wasn't working. I found the solution, but don't have the sourcecode with me today. I'll update my question on monday.
${checkstyle.ruleset} and ${checkstyle.suppressions} what goes in these two ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.