java 在某些文件上抑制特定 checkstyle 规则的正确语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13874582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct syntax to suppress specific checkstyle rules on certain files?
提问by cringe
I am using Maven 3and Checkstyle 2.9.1in my javaproject and I have a common checkstyle configuration that we're using in several other projects as well. To avoid copying and editing that configuration file into my own project I use a suppressions fileto disable all checks in some packages (generated code).
我在我的java项目中使用Maven 3和Checkstyle 2.9.1,我有一个通用的 checkstyle 配置,我们也在其他几个项目中使用。为了避免将该配置文件复制和编辑到我自己的项目中,我使用抑制文件来禁用某些包(生成的代码)中的所有检查。
Now I want to suppress specific rules in all files.
现在我想抑制所有文件中的特定规则。
This is my suppressions file:
这是我的抑制文件:
<?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>
This is my POM:
这是我的 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>
I'm then calling
然后我打电话
mvn checkstyle:checkstyle
to generate the reports, but the suppressed warnings are still in there.
生成报告,但被抑制的警告仍然在那里。
What am I doing wrong here?
我在这里做错了什么?
回答by cringe
Ok, I finally managed to build a working suppressions file for my checkstyle configuration.
好的,我终于设法为我的 checkstyle 配置构建了一个有效的抑制文件。
My original problemwas with the regex, so instead of files=".*\\.java$"
I'm now using files="."
to suppress special checks on all files. I also suppress all checks on certain files.
我最初的问题出在正则表达式上,所以files=".*\\.java$"
我现在用它files="."
来取消对所有文件的特殊检查。我还禁止对某些文件进行所有检查。
Here are some examples from my suppressions file:
以下是我的抑制文件中的一些示例:
<?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>
If you need help with the Mavenpart of the configuration, see the answerof @Mawia.
如果您在配置的Maven部分需要帮助,请参阅@Mawia的回答。
回答by Mawia
This worked for me.
这对我有用。
Instead of doing
而不是做
<suppressionsLocation>${checkstyle.suppressions}</suppressionsLocation>
I put this in ${checkstyle.ruleset} (which I assume is your xml file for check style)
我把它放在 ${checkstyle.ruleset} (我假设这是你的检查样式的 xml 文件)
<module name="SuppressionFilter">
<property name="file" value="${checkstyle.suppressions}" />
</module>
And my ${checkstyle.suppressions} has
我的 ${checkstyle.suppressions} 有
<suppressions>
<suppress files="package-info.java" checks="[a-zA-Z0-9]*"/>
<suppress files="SomeClass.java" checks="[a-zA-Z0-9]*"/>
</suppressions>