【问题标题】:Checkstyle - Exclude folderCheckstyle - 排除文件夹
【发布时间】:2013-08-03 12:19:10
【问题描述】:

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

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

我在我的 xml 中添加了一个抑制过滤器:

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

我的 suppresss.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>

但它不起作用。有什么想法吗?

【问题讨论】:

    标签: eclipse checkstyle


    【解决方案1】:
    <suppress files="[\\/]generated-sources[\\/]" checks="[a-zA-Z0-9]*"/>
    

    这行得通:)

    【讨论】:

    • 诀窍是 Checkstyle 将正则表达式应用于 文件路径,而不是二进制类名或其他内容。您可以将checks 属性缩短为checks="."
    • 是的,需要更多细节。 IE什么文件。在哪里
    • @StarWind 如我的问题所述,该文件是 suppresss.xml。该文件的路径在 checkstyles.xml 中定义。
    • 如果您解释一下它是如何工作的以及为什么它可以解决问题,这个答案会更好!
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      正如 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插件正常工作)

      【讨论】:

        【解决方案4】:

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

        假设我有以下 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 验证我的设置。一切都应该正常。

        【讨论】:

          猜你喜欢
          • 2017-08-17
          • 2015-06-11
          • 2017-10-12
          • 2013-07-17
          • 2014-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-26
          相关资源
          最近更新 更多