Excluding classes in Maven Checkstyle plugin reports

后端 未结 4 1823
故里飘歌
故里飘歌 2020-12-09 07:24

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<

相关标签:
4条回答
  • 2020-12-09 07:52

    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>
    
    0 讨论(0)
  • 2020-12-09 07:59

    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>
    
    0 讨论(0)
  • 2020-12-09 08:10

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

    0 讨论(0)
  • 2020-12-09 08:15

    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.

    0 讨论(0)
提交回复
热议问题