28

I want to ignore a specific folder (named generated-sources) from my checkstyle reports, because they are generated.

I'm using eclipse-cs for displaying my violations.

i added a suppressionfilter to my xml:

<module name="SuppressionFilter">
    <property name="file" value=".\suppressions.xml"/>
</module>

my suppressions.xml looks like this:

<?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="\*generated-sources\*\*\.\*" checks="[a-zA-Z0-9]*"/>
</suppressions>

but it is not working. any ideas?

5 Answers 5

40
<suppress files="[\\/]generated-sources[\\/]" checks="[a-zA-Z0-9]*"/>

this works :)

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

4 Comments

The trick is that Checkstyle applies the regex to the file path, not the binary class name or something. You could shorten the checks attribute to checks=".".
Yeah way more detail needed. IE what file. Where
@StarWind as stated in my question, the file is the suppressions.xml. the path to that file is defined in the checkstyles.xml.
This answer would be better if you explained how it works and why it solves the problem!
3

Additionally to the answer from Philipp, I had to use an absolute pathname ( :-( ) for the suppression file:

<module name="SuppressionFilter">
    <property name="file" value="/Users/xxx/workspace/suppressions.xml"/>
</module>

Looks like the Checkstyle plugin is not using the project home directory.

(at least under eclipse luna / Mac OS X)

Comments

2

As pointed by Thomas Welsch in his answer, there appears to be a problem with using relative pathname for the suppression xml file.

For gradle builds, This gist suggests a workaround:

in build.gradle:

checkstyle {
    // use one common config file for all subprojects
    configFile = project(':').file('config/checkstyle/checkstyle.xml')
    configProperties = [ "suppressionFile" : project(':').file('config/checkstyle/suppressions.xml')]
 }

in checkstyle.xml:

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

(the default value allows IDE plugins, that do not have the gradle variable sorted out, to work correctly)

Comments

2

For me using SuppressionFilter didn't worked at all. But this works, exactly for the purpose of excluding folder from scanning

<module name="BeforeExecutionExclusionFileFilter">
  <property name="fileNamePattern"  value=".*[\\/]folder-name-here[\\/].*$"/>
</module>

Needs to be inserted inside <module name="Checker"> section.

Docs https://checkstyle.sourceforge.io/config_filefilters.html

Comments

1

This answer tries to fill in missing details in the previous answers.

Suppose I have the following maven project, which only lists the directories inside the project.

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── edu
    │   │       └── utexas
    │   │           └── cs
    │   │               ├── liveoak
    │   │               │   ├── common
    │   │               │   ├── tree
    │   │               │   └── zero
    │   │               ├── logging
    │   │               └── sam
    │   │                   ├── core
    │   │                   │   └── instructions
    │   │                   ├── io
    │   │                   ├── ui
    │   │                   │   └── components
    │   │                   └── utils
    │   └── resources
    │       ├── sam-checks.xml
    │       └── sam-suppressions.xml
    └── test
 

sam-checks.xml is the checkstyle configuration file and sam-suppressions.xml is the suppression xml document. Inside sam-checks.xml, I have

<module name="SuppressionFilter">
    <property name="file" value="src/main/resources/sam-suppressions.xml"/>
    <property name="optional" value="false"/>
</module>

Note the location of sam-suppressions.xml is relative to the pom.xml of the project.

I want to suppress the checks for all the java files under sam directory (main/java/edu/utexas/cs/sam). To do so, my sam-suppressions.xml looks like below

<!DOCTYPE suppressions PUBLIC
        "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
        "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
    <suppress checks="[a-zA-Z0-9]*"
              files="[\\/]sam[\\/]"/>
</suppressions>

I verify my setup with mvn checkstyle:check. Everything should work.

Comments

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.