Excluding classes in Maven Checkstyle plugin reports

41,552

Solution 1

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

Solution 2

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.

Solution 3

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>

Solution 4

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>
Share:
41,552

Related videos on Youtube

Andrew Harmel-Law
Author by

Andrew Harmel-Law

I'm a developer (Java, with a bit of Ruby, JS and some C#, currently learning Scala in an attempt to grok functional programming) and Scrum Master. I work in the UK for a large consultancy. I've been described as being "passionate about software" which I like.

Updated on July 09, 2022

Comments

  • Andrew Harmel-Law
    Andrew Harmel-Law almost 2 years

    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.

  • djjeck
    djjeck over 10 years
    You might want to disable the plugin, maybe?
  • drew
    drew almost 9 years
    This doesn't work. <excludes> path pattern does not apply to the base directory, of which the generated directory is part.
  • drew
    drew almost 9 years
    Well, strike that. In general, the generated directory is part of the base directory, and as such, using <excludes> on it will not work. However, if you happen to have source that includes a generated package, then it will.
  • Joachim Rohde
    Joachim Rohde over 8 years
    Thanks a lot. Excluding generated sources was giving me really a hard time and I thought I used a wrong pattern. That sourceDirectories is not working is a known bug since 2.13 (issues.apache.org/jira/browse/MCHECKSTYLE-260)
  • vorburger
    vorburger over 7 years
    The trouble with this is that it won't work e.g. for /target/ - I think because the exclusion is checked on the relative not absolute path of the source or resource. To fully exclude any generated code, without relying on any package convention, the <sourceDirectory>${project.build.sourceDirectory} is thus the only solution. To fully exclude resources, a SuppressionFilter with an XML containing <suppress files="[/\]target[/\]" checks=".*"/> seems to be the only way (full example on git.opendaylight.org/gerrit/#/c/48715)
  • vorburger
    vorburger over 7 years
    As noted in the comment on the other answer above, this works for generated sources, but not for generates resources. The maven-checkstyle-plugin has no resourceDirectory (resourceDirectories), so if you have other Maven code gen. or plugins, or build-helper-maven-plugin, then to just fully completely exclude /target/ the best seems to be a SuppressionFilter with an XML containing <suppress files="[/\]target[/\]" checks=".*"/> (full example on git.opendaylight.org/gerrit/#/c/48715)
  • Cyrusmith
    Cyrusmith over 6 years
    For me this configuration is working: ``` <configuration> <sourceDirectories> <sourceDirectory>${project.build.sourceDirectory}</sourceDir‌​ectory> </sourceDirectories> </configuration>```
  • Davids037
    Davids037 over 6 years
    This works for me too (mvn 3.5.2, maven-checkstyle-plugin 2.17, checkstyle 8.7): <sourceDirectories> <sourceDirectory>${project.build.sourceDirectory}</sourceDir‌​ectory> <sourceDirectory>${project.build.testSourceDirectory}</sourc‌​eDirectory> </sourceDirectories>
  • Olivier Cailloux
    Olivier Cailloux almost 3 years
    The bug about sourceDirectories is corrected since 2017, and sourceDirectory is deprecated. Please consider updating your answer.
  • Alexandros
    Alexandros over 2 years
    I arrived here exhausted, searching for a way to exclude an entire module of a multi-module build. Do not try <excludes> for that, as it seems to apply to the relative path of each file from its module (and hence does not contain the module name). Fortunately, the suppression filter approach works: <suppress files="[\\/]module-name[\\/]src[\\/]" checks=".*"/>
  • faruk13
    faruk13 over 2 years
    Can confirm sourceDirectories works now. <configuration> <sourceDirectories>${project.build.sourceDirectories}</sourc‌​eDirectories> </configuration>