如何在 Checkstyle 中取消对文件的所有检查?
我正在对第三方类进行覆盖,并且我想取消对它的所有检查(因为我只保留它直到补丁被接受)。
有没有办法禁止对文件的所有检查?
我尝试使用“*”但失败了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不知道您是使用命令行还是在 IDE 中,但您基本上需要一个抑制文件。 如果您手动编辑 Checkstyle 配置文件,请向其中添加一个新模块:
您的
mysuppression.xml
可能类似于:“
files
”属性的值基本上是源文件的正则表达式,“checks
”属性的值是要跳过的检查的正则表达式 (“[a-zA-Z0-9]*
”基本上意味着跳过一切)。 我猜这就是您正在寻找的模式?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:
Your
mysuppression.xml
can be something like: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?我能够通过在 checks.xml 中添加 SuppressionCommentFilter 标签来抑制文件中的检查:
首先,我添加了 FileContentHolder 标签作为 TreeWalker 标签的子标签(自版本 8.1 以来,不需要此步骤) com.puppycrawl.tools:checkstyle):
然后我在checks.xml中添加了SuppressionCommentFilter(从com.puppycrawl.tools:checkstyle的8.1版本开始 - 在“TreeWalker”节点下):
在我想要抑制检查的每个文件中我在文件的第一行插入了以下注释:
I was able to suppress the checks in a file by adding the SuppressionCommentFilter tag in my checks.xml:
First, I added the FileContentHolder tag as a child of TreeWalker tag (this step is not needed since version 8.1 of com.puppycrawl.tools:checkstyle):
Then I added the SuppressionCommentFilter in the checks.xml (since version 8.1 of com.puppycrawl.tools:checkstyle - under "TreeWalker" node):
In each file that I wanted to suppress the checks I inserted the following comment in the first line of the file:
aberrant80的回答很有帮助。 在他的提示 - 使用正则表达式模式匹配的帮助下,我还能够通过将其添加到 checkstyle-suppressions.xml 文件来抑制整个包的 checkstyle。
例如。 跳过对 jaxb 包下所有自动生成的 java 文件的 checkstyle 检查,
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,
如果您使用 Checkstyle 的 Checkclipse Eclipse 插件,则可以通过转到 Checkclipse > 来包含或排除文件模式(包括目录)。 项目属性下的文件过滤器选项卡。 例如,我的项目在顶层包含 src 和 test 目录。 我希望 Checkstyle 仅应用于 src 目录中的文件(省略 test),因此我添加了一个如下所示的包含模式:
如您所见,它使用正则表达式模式规范的风格语法。
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 src and test directories at the top level. I want Checkstyle applied to only files in the src directory (omitting test), so I added an include pattern that looks like this:
As you can see, it uses a regex-style syntax for pattern specification.
@aberrant80 的答案非常鼓舞人心,尽管它在我的项目中不起作用。 为了抑制 checkstyle 在 Foo.java 中查找的任何警告,根据 链接,如果使用maven:
并且在checkstyle-suppressions.xml中
到目前为止最高版本是1.1(1.2不存在)。
@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:
And in checkstyle-suppressions.xml
So far the highest version is 1.1 (1.2 does not exist).
有一个选项可以忽略写保护的文件。 或者是一个包中的文件。
There is an option to ignore write protected files. Or files in a package.
如果您不希望检查项目中的一组文件,则可以通过创建文件过滤器过滤掉这些文件,这样它们就不会被检查。
文件过滤器使用正则表达式来确定要排除的文件。 正则表达式对完整文件名进行操作 - 因此,您还可以排除整个文件夹。 在这种情况下,如果您愿意,您可以排除整个包。
如果你用谷歌搜索一下 - 你可能会找到一些 Checkstyle 配置属性文件,其中包含你正在寻找的内容的示例。 我建议您这样做后 - 将其保存为模板,以便您可以在将来的情况下参考它
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