Checkstyle - 排除文件夹

时间:2013-08-02 09:26:10

标签: eclipse checkstyle

我想忽略我的checkstyle报告中的特定文件夹(名为generated-sources),因为它们是生成的。

我正在使用eclipse-cs来显示我的违规行为。

我在我的xml中添加了一个suppressfilter:

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

我的suppressions.xml看起来像这样:

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

但它不起作用。任何想法?

4 个答案:

答案 0 :(得分:29)

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

这有效:)

答案 1 :(得分:2)

除了Philipp的回答,我必须使用绝对路径名(:-()作为抑制文件:

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

看起来Checkstyle插件没有使用项目主目录。

(至少在eclipse luna / Mac OS X下)

答案 2 :(得分:0)

Thomas Welsch 在他的回答中指出的那样,对于抑制xml文件使用相对路径名似乎存在问题。

对于gradle构建,This gist建议一种解决方法:

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')]
 }

checkstyle.xml中:

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

(默认值允许未对gradle变量进行排序的IDE插件正常工作)

答案 3 :(得分:0)

这个答案试图填补之前答案中缺失的细节。

假设我有如下maven项目,只列出了项目内部的目录。

.
├── 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 是 checkstyle 配置文件,sam-suppressions.xmlsuppression xml document。在 sam-checks.xml 中,我有

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

注意 sam-suppressions.xml 的位置是相对于项目的 pom.xml

我想取消对 sam 目录 (main/java/edu/utexas/cs/sam) 下所有 java 文件的检查。为此,我的 sam-suppressions.xml 如下所示

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

我使用 mvn checkstyle:check 验证我的设置。一切正常。