[英]What is the correct syntax to suppress specific checkstyle rules on certain files?
我在我的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
生成报告,但被抑制的警告仍在那里。
我在这做错了什么?
好吧,我终于设法为我的checkstyle配置构建一个工作的抑制文件。
我最初的问题是正则表达式,所以而不是files=".*\\\\.java$"
我现在正在使用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的答案 。
这对我有用。
而不是做
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.