Philipp Sander
Philipp Sander

Reputation: 10249

Checkstyle - Exclude folder

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?

Upvotes: 28

Views: 27558

Answers (5)

Vuk Djapic
Vuk Djapic

Reputation: 886

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

Upvotes: 2

xxks-kkk
xxks-kkk

Reputation: 2608

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.

Upvotes: 0

Gilad Bar Orion
Gilad Bar Orion

Reputation: 574

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)

Upvotes: 2

Thomas Welsch
Thomas Welsch

Reputation: 61

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)

Upvotes: 2

Philipp Sander
Philipp Sander

Reputation: 10249

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

this works :)

Upvotes: 39

Related Questions