【问题标题】:What is the correct syntax to suppress specific checkstyle rules on certain files?抑制某些文件的特定检查样式规则的正确语法是什么?
【发布时间】:2023-04-01 04:36:01
【问题描述】:

我在我的 java 项目中使用 Maven 3 和 Checkstyle 2.9.1,并且我有一个我们正在使用的通用 checkstyle 配置在其他几个项目中也是如此。为了避免将该配置文件复制和编辑到我自己的项目中,我使用 抑制文件 来禁用某些包(生成的代码)中的所有检查。

现在我想禁止所有文件中的特定规则。

这是我的抑制文件:

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

这是我的 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>

我正在打电话

mvn checkstyle:checkstyle

生成报告,但抑制的警告仍然存在。

我在这里做错了什么?

【问题讨论】:

    标签: java checkstyle


    【解决方案1】:

    好的,我终于设法为我的 checkstyle 配置构建了一个有效的抑制文件。

    我的原始问题与正则表达式有关,所以我现在使用files="." 代替files="." 来禁止对所有文件进行特殊检查。我还禁止对某些文件进行所有检查。

    以下是我的抑制文件中的一些示例:

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

    如果您需要有关配置的 Maven 部分的帮助,请参阅@Mawia 的answer。

    【讨论】:

      【解决方案2】:

      这对我有用。

      而不是做

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

      我把它放在 ${checkstyle.ruleset} 中(我假设这是你的检查样式的 xml 文件)

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

      我的 ${checkstyle.suppressions} 有

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

      【讨论】:

      • 嗯,抑制文件被识别和使用。但实际的压制并没有奏效。我找到了解决方案,但今天没有源代码。我会在星期一更新我的问题。
      • ${checkstyle.ruleset} 和 ${checkstyle.suppressions} 这两个是什么?
      猜你喜欢
      • 2011-10-05
      • 2013-01-25
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 2015-06-02
      相关资源
      最近更新 更多