排除 Maven Checkstyle 插件报告中的类

发布于 2024-08-23 10:13:25 字数 240 浏览 8 评论 0原文

我有一个 Maven 2 项目,我想配置我的 Checkstyle 报告插件,以便只分析我的一些类。我找到了 maven.checkstyle.excludes 属性,但尽管将其作为命令行参数传递(使用 -D=maven.checkstyle.excludes=... )我无法让它工作。我在插件文档页面上找不到任何内容。理想情况下,我希望能够在 POM 的 部分中进行设置。

I have a Maven 2 project and I want to configure my Checkstyle report plugin so that only some of my classes are analysed. I have found the maven.checkstyle.excludes property, but despite passing this as a command line parameter (using -D=maven.checkstyle.excludes=...) I can't get it to work. I can't find anything on the Plugin documentation page. Ideally I want to be able to set this in the <configuration> section of my POM.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

勿挽旧人 2024-08-30 10:13:25

如果像我一样,您来到这里寻找一种从 checkstyle 中排除生成的源的方法,请执行以下操作:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>2.15</version>
  <configuration>
    <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
  </configuration>
</plugin>

默认情况下,checkstyle:checkstyle checkstyle 插件的目标使用 ${project.compileSourceRoots},显然包括生成的源目录。

如果将其更改为 ${project.build.sourceDirectory},它将仅使用源目录,而不使用任何生成的源目录。

请注意,虽然 已弃用,但替代方案 似乎不起作用。

编辑:在评论中,@Cyrusmith、@Ben 和 @Olivier Callioux 报告说,截至 2017 年, 有效;我无法确认。

If, like me, you arrived here searching for a way to exclude generated sources from checkstyle, do this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>2.15</version>
  <configuration>
    <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
  </configuration>
</plugin>

By default, the checkstyle:checkstyle goal of the checkstyle plugin uses ${project.compileSourceRoots}, which apparently includes generated source directories.

If you change it to ${project.build.sourceDirectory}, it will use only the source directory, not any generated source directories.

Note that while <sourceDirectory> is deprecated, the alternative, <sourceDirectories>, does not appear to work.

Edit: In comments, @Cyrusmith, @Ben, and @Olivier Callioux report that as of 2017, <sourceDirectories> works; I cannot confirm.

琉璃梦幻 2024-08-30 10:13:25

如果这个问题是关于 Maven 2,那么属性是 < code>excludes 并采用逗号分隔的 Ant 模式列表。因此,要么在命令行上传递它:

-Dexcludes=**/generated/**/*

要么在插件配置中设置它:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <configuration>
       <excludes>**/generated/**/*</excludes>
   </configuration>
</plugin>

另一个选择是使用 抑制过滤器

例如,您可以使用 SuppressionCommentFilter 来抑制包含 CHECKSTYLE:OFF 的注释和包含 CHECKSTYLE:ON 的注释之间的审核事件 /em> (然后只需将这些注释添加到您不想检查的类或代码部分)。

If this question is about Maven 2, then the property is excludes and takes a comma-separated list of Ant patterns. So either pass this on the command line:

-Dexcludes=**/generated/**/*

Or set it up in the plugin configuration:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <configuration>
       <excludes>**/generated/**/*</excludes>
   </configuration>
</plugin>

Another option would be to use a suppression filter.

For example you could use the SuppressionCommentFilter to suppress audit events between a comment containing CHECKSTYLE:OFF and a comment containing CHECKSTYLE:ON (then just add these comments to the classes or parts of the code you don't want to check).

梦一生花开无言 2024-08-30 10:13:25

此外,如果您想排除多个独立文件夹,您可以添加多个独立路径,以逗号分隔,如下所示

<excludes>org/log4j/*,com/acme/**/*,com/companyb/*</excludes>

Additionally, if you want to exclude multiple independent folders, you can add multiple independent paths comma separated like this

<excludes>org/log4j/*,com/acme/**/*,com/companyb/*</excludes>
毁虫ゝ 2024-08-30 10:13:25

上面的答案对我不起作用,因为我在 Maven 中运行代码生成,它还将目标/生成添加为源目录。

以下解决方案有效:
您必须使用显式的 checkstyle-suppressions.xml 配置文件并从配置中激活它:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
      <configLocation>checkstyle.xml</configLocation>
      <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
   [...]

用于排除目标文件夹的抑制文件如下所示:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
  "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
  "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
  <suppress files="[/\\]target[/\\]" checks=".*" />
</suppressions>

The answers above didn't work for me as I'm running code generation in maven which also adds the target/generated as a source dir.

The following solution works:
You have to use an explicit checkstyle-suppressions.xml config file and activate it from your configuration:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
      <configLocation>checkstyle.xml</configLocation>
      <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
   [...]

The suppressions file for excluding the target folder looks like this:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
  "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
  "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
  <suppress files="[/\\]target[/\\]" checks=".*" />
</suppressions>
流绪微梦 2024-08-30 10:13:25

我正在使用 maven 3 并且排除似乎不起作用。有效的是 sourceDirectories 技巧:

            <configuration>
                <sourceDirectories>
                    <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
                </sourceDirectories>
            </configuration>

I'm using maven 3 and excludes does not seem to work. What worked was the sourceDirectories trick:

            <configuration>
                <sourceDirectories>
                    <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
                </sourceDirectories>
            </configuration>
春花秋月 2024-08-30 10:13:25

我正在使用 maven 3,但由于某种原因,排除和 sourceDirectory 都不起作用。为面临同样问题的任何人添加替代方案。

设置自定义检查样式抑制,如下所示。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle.maven.plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>${com.puppycrawl.tools.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>
                                src/main/resources/customized_google_checks.xml
                            </configLocation>
                            <suppressionsLocation>
                                src/main/resources/customized_suppressions.xml
                            </suppressionsLocation>
                            <consoleOutput>true</consoleOutput>
                            <failsOnError>true</failsOnError>
                            <failOnViolation>false</failOnViolation>
                            <violationSeverity>warning</violationSeverity>
                            <includeTestSourceDirectory>false</includeTestSourceDirectory>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

在抑制文件中使用以下内容来排除特定文件。


<!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.1//EN"
        "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<!--<!DOCTYPE module PUBLIC-->
<!--        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"-->
<!--        "https://checkstyle.org/dtds/configuration_1_3.dtd">-->

<suppressions>
    <suppress checks=".*" files=".ESAPI.properties" />

</suppressions>

I'm using maven 3 and both excludes and sourceDirectory did not work for some reason. Adding an alternative for anybody who is facing the same issue.

Setup a custom checkstyle suppression as shown below.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle.maven.plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>${com.puppycrawl.tools.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>
                                src/main/resources/customized_google_checks.xml
                            </configLocation>
                            <suppressionsLocation>
                                src/main/resources/customized_suppressions.xml
                            </suppressionsLocation>
                            <consoleOutput>true</consoleOutput>
                            <failsOnError>true</failsOnError>
                            <failOnViolation>false</failOnViolation>
                            <violationSeverity>warning</violationSeverity>
                            <includeTestSourceDirectory>false</includeTestSourceDirectory>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Use the following in the suppression file to exclude specific files.


<!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.1//EN"
        "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<!--<!DOCTYPE module PUBLIC-->
<!--        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"-->
<!--        "https://checkstyle.org/dtds/configuration_1_3.dtd">-->

<suppressions>
    <suppress checks=".*" files=".ESAPI.properties" />

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