Java 如何在Checkstyle中取消对文件的所有检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1012407/
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
How to suppress all checks for a file in Checkstyle?
提问by Alceu Costa
I'm doing an override for a third party class and I want to suppress all checks for it (since I'm only keeping it around until the patch is accepted).
我正在为第三方类做一个覆盖,我想取消对它的所有检查(因为我只保留它直到补丁被接受)。
Is there a way to suppress all checks for a file?
有没有办法抑制对文件的所有检查?
I tried using "*" but that fails.
我尝试使用“*”但失败了。
采纳答案by aberrant80
Don't know whether you're using command line or in an IDE, but you'll basically need a suppresions file. If you're manually editing the Checkstyle config file, add a new module to it:
不知道您是在使用命令行还是在 IDE 中,但您基本上需要一个抑制文件。如果您手动编辑 Checkstyle 配置文件,请向其中添加一个新模块:
<module name="SuppressionFilter">
<property name="file" value="mysuppressions.xml" />
</module>
Your mysuppression.xml
can be something like:
你mysuppression.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="TheClassToIgnore\.java" checks="[a-zA-Z0-9]*"/>
</suppressions>
The value for "files
" attribute is basically a regex for your source files, and the value for the "checks
" attribute is regex for what checks to skip ("[a-zA-Z0-9]*
" basically means skip everything). This is the pattern you're looking for I guess?
" files
" 属性的值基本上是源文件的正则表达式,而 " checks
" 属性的值是要跳过的检查的正则表达式(" [a-zA-Z0-9]*
" 基本上意味着跳过所有内容)。这就是你要找的模式,我猜?
回答by akarnokd
There is an option to ignore write protected files. Or files in a package.
有一个选项可以忽略写保护文件。或包中的文件。
回答by Rob H
If you're using the Checkclipse Eclipse plugin for Checkstyle, you can include or exclude file patterns (including directories) by going to the Checkclipse > File Filter tab under project properties. For example, my project contains srcand testdirectories at the top level. I want Checkstyle applied to only files in the srcdirectory (omitting test), so I added an include pattern that looks like this:
如果您将 Checkclipse Eclipse 插件用于 Checkstyle,您可以通过转到项目属性下的 Checkclipse > 文件过滤器选项卡来包含或排除文件模式(包括目录)。例如,我的项目在顶层包含src和test目录。我希望 Checkstyle 仅应用于src目录中的文件(省略test),因此我添加了一个如下所示的包含模式:
src/.+java$
As you can see, it uses a regex-style syntax for pattern specification.
如您所见,它使用正则表达式样式的语法进行模式规范。
回答by ist_lion
If you wish to not have a group of files within a project inspected, you can filter these files out so they are not inspected by creating a file filter
如果您不希望检查项目中的一组文件,您可以通过创建文件过滤器过滤掉这些文件,这样它们就不会被检查
The file filter uses regex to determine what files to exclude. The regex operates on the complete file name - because of this, you could also exclude whole folders. In this case you could exclude the whole package if you wished.
文件过滤器使用正则表达式来确定要排除的文件。正则表达式对完整的文件名进行操作 - 因此,您还可以排除整个文件夹。在这种情况下,您可以根据需要排除整个包。
If you google around a bit - you could probably find some Checkstyle Configuration Propertie files that have examples of what you're looking for. I would suggest after you do so - save it as a bit of a template so you can refer to it in future situations
如果您搜索一下 - 您可能会找到一些 Checkstyle 配置属性文件,其中包含您正在寻找的示例。我建议你这样做之后 - 将它保存为一个模板,以便你可以在将来的情况下参考它
回答by Alceu Costa
I was able to suppress the checks in a file by adding the SuppressionCommentFilter tag in my checks.xml:
通过在我的checks.xml 中添加SuppressionCommentFilter 标记,我能够抑制文件中的检查:
First, I added the FileContentHolder tag as a child of TreeWalker tag (this step is notneeded since version 8.1 of com.puppycrawl.tools:checkstyle):
首先,我添加了FileContentHolder标签作为TreeWalker标签的孩子(这一步是没有必要的,因为com.puppycrawl.tools:checkstyle版本8.1):
<module name="TreeWalker">
...
<module name="FileContentsHolder"/>
...
</module>
Then I added the SuppressionCommentFilter in the checks.xml (since version 8.1 of com.puppycrawl.tools:checkstyle - under "TreeWalker" node):
然后我在 checks.xml 中添加了 SuppressionCommentFilter(从 com.puppycrawl.tools:checkstyle 8.1 版开始 - 在“TreeWalker”节点下):
<module name="SuppressionCommentFilter"/>
In each file that I wanted to suppress the checks I inserted the following comment in the first line of the file:
在我想取消检查的每个文件中,我在文件的第一行插入了以下注释:
// CHECKSTYLE:OFF
回答by Inxsible
aberrant80's answer helped a lot. With the help of his hint - using regex pattern matching, I was also able to suppress checkstyle for an entire package by adding this to the checkstyle-suppressions.xml file. For eg. to skip checkstyle checks on all auto generated java files under the jaxb package,
aberrant80 的回答很有帮助。在他的提示的帮助下 - 使用正则表达式模式匹配,我还能够通过将其添加到 checkstyle-suppressions.xml 文件来抑制整个包的 checkstyle。例如。跳过对 jaxb 包下所有自动生成的 java 文件的 checkstyle 检查,
<suppressions>
<suppress checks="[a-zA-Z0-9]*" files="[\/]jaxb[\/]" />
</suppressions>
回答by Tiina
@aberrant80's answer is very inspiring although it didn't work in my project. In order to suppress any warnings that checkstyle looks for in Foo.java, according to link, if maven is used:
@ aberrant80 的回答非常鼓舞人心,尽管它在我的项目中不起作用。为了抑制 checkstyle 在 Foo.java 中查找的任何警告,根据链接,如果使用 maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
...
<configuration>
...
<configLocation>checkstyle.xml</configLocation>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
...
</configuration>
...
</plugin>
And in checkstyle-suppressions.xml
并在checkstyle-suppressions.xml
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files="Foo.java" checks="[a-zA-Z0-9]*" />
</suppressions>
So far the highest version is 1.1 (1.2 does not exist).
到目前为止,最高版本是 1.1(1.2 不存在)。