Java 排除 Maven Checkstyle 插件报告中的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2362652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:34:54  来源:igfitidea点击:

Excluding classes in Maven Checkstyle plugin reports

javamaven-2checkstyle

提问by Andrew Harmel-Law

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.excludesproperty, 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.

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

采纳答案by Pascal Thivent

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

如果此问题与 Maven 2 有关,则该属性是excludes并且采用逗号分隔的 Ant 模式列表。所以要么在命令行上传递这个:

-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 SuppressionCommentFilterto suppress audit events between a comment containing CHECKSTYLE:OFFand a comment containing CHECKSTYLE:ON(then just add these comments to the classes or parts of the code you don't want to check).

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

回答by Christer Fahlgren

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

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

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

回答by drew

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

如果像我一样,您来到这里寻找一种从 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>

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

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

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

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

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

请注意,虽然<sourceDirectory>已弃用,但替代方法 ,<sourceDirectories>似乎不起作用。