English 中文(简体)
Excluding classes in Maven Checkstyle plugin reports
原标题:

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.

最佳回答

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

问题回答

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.

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>

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>

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>

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>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签