如何在 Checkstyle 中取消对文件的所有检查?

发布于 2024-07-25 02:17:20 字数 110 浏览 3 评论 0原文

我正在对第三方类进行覆盖,并且我想取消对它的所有检查(因为我只保留它直到补丁被接受)。

有没有办法禁止对文件的所有检查?

我尝试使用“*”但失败了。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烛影斜 2024-08-01 02:17:20

不知道您是使用命令行还是在 IDE 中,但您基本上需要一个抑制文件。 如果您手动编辑 Checkstyle 配置文件,请向其中添加一个新模块:

<module name="SuppressionFilter">
    <property name="file" value="mysuppressions.xml" />
</module>

您的 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>

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:

<module name="SuppressionFilter">
    <property name="file" value="mysuppressions.xml" />
</module>

Your mysuppression.xml can be something like:

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

梦回旧景 2024-08-01 02:17:20

我能够通过在 checks.xml 中添加 SuppressionCommentFilter 标签来抑制文件中的检查:

首先,我添加了 FileContentHolder 标签作为 TreeWalker 标签的子标签(自版本 8.1 以来,不需要此步骤) com.puppycrawl.tools:checkstyle):

<module name="TreeWalker">
    ...
    <module name="FileContentsHolder"/>
    ...
</module>

然后我在checks.xml中添加了SuppressionCommentFilter(从com.puppycrawl.tools:checkstyle的8.1版本开始 - 在“TreeWalker”节点下):

<module name="SuppressionCommentFilter"/>

在我想要抑制检查的每个文件中我在文件的第一行插入了以下注释:

// CHECKSTYLE:OFF

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):

<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):

<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
一个人练习一个人 2024-08-01 02:17:20

aberrant80的回答很有帮助。 在他的提示 - 使用正则表达式模式匹配的帮助下,我还能够通过将其添加到 checkstyle-suppressions.xml 文件来抑制整个包的 checkstyle。
例如。 跳过对 jaxb 包下所有自动生成的 java 文件的 checkstyle 检查,

    <suppressions>
        <suppress checks="[a-zA-Z0-9]*" files="[\\/]jaxb[\\/]" />
    </suppressions>

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,

    <suppressions>
        <suppress checks="[a-zA-Z0-9]*" files="[\\/]jaxb[\\/]" />
    </suppressions>
腻橙味 2024-08-01 02:17:20

如果您使用 Checkstyle 的 Checkclipse Eclipse 插件,则可以通过转到 Checkclipse > 来包含或排除文件模式(包括目录)。 项目属性下的文件过滤器选项卡。 例如,我的项目在顶层包含 srctest 目录。 我希望 Checkstyle 仅应用于 src 目录中的文件(省略 test),因此我添加了一个如下所示的包含模式:

src/.+java$

如您所见,它使用正则表达式模式规范的风格语法。

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:

src/.+java$

As you can see, it uses a regex-style syntax for pattern specification.

枫林﹌晚霞¤ 2024-08-01 02:17:20

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

并且在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>

到目前为止最高版本是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:

<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

<!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).

你对谁都笑 2024-08-01 02:17:20

有一个选项可以忽略写保护的文件。 或者是一个包中的文件。

There is an option to ignore write protected files. Or files in a package.

情仇皆在手 2024-08-01 02:17:20

如果您不希望检查项目中的一组文件,则可以通过创建文件过滤器过滤掉这些文件,这样它们就不会被检查。

文件过滤器使用正则表达式来确定要排除的文件。 正则表达式对完整文件名进行操作 - 因此,您还可以排除整个文件夹。 在这种情况下,如果您愿意,您可以排除整个包。

如果你用谷歌搜索一下 - 你可能会找到一些 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文