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.

- 10,475
- 7
- 58
- 103

- 7,739
- 12
- 44
- 55
6 Answers
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.

- 2,949
- 3
- 25
- 27
-
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 (https://issues.apache.org/jira/browse/MCHECKSTYLE-260) – Joachim Rohde Dec 15 '15 at 13:41
-
1As 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
(full example on https://git.opendaylight.org/gerrit/#/c/48715/) – vorburger Nov 25 '16 at 19:53 -
For me this configuration is working: ```
${project.build.sourceDirectory} -
4This works for me too (mvn 3.5.2, maven-checkstyle-plugin 2.17, checkstyle 8.7):
${project.build.sourceDirectory} ${project.build.testSourceDirectory} -
1The bug about `sourceDirectories` is corrected since 2017, and `sourceDirectory` is [deprecated](http://maven.apache.org/plugins/maven-checkstyle-plugin/check-mojo.html#sourceDirectory). Please consider updating your answer. – Olivier Cailloux Aug 08 '21 at 15:06
-
Can confirm `sourceDirectories` works now. ```
${project.build.sourceDirectories}
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).

- 474
- 4
- 18

- 562,542
- 136
- 1,062
- 1,124
-
-
4This doesn't work. `
` path pattern does not apply to the base directory, of which the `generated` directory is part. – drew May 22 '15 at 21:24 -
2Well, strike that. In general, the `generated` directory is part of the base directory, and as such, using `
` on it will not work. However, if you happen to have source that includes a `generated` package, then it will. – drew May 22 '15 at 21:33 -
9The 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
${project.build.sourceDirectory} is thus the only solution. To fully exclude resources, a SuppressionFilter with an XML containing – vorburger Nov 25 '16 at 19:50seems to be the only way (full example on https://git.opendaylight.org/gerrit/#/c/48715/) -
I arrived here exhausted, searching for a way to exclude an entire module of a multi-module build. Do not try
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: – Alexandros Oct 02 '21 at 11:12
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>

- 474
- 4
- 18

- 2,414
- 2
- 21
- 16
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>

- 3,593
- 30
- 34
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>

- 1,967
- 2
- 21
- 24
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>

- 100
- 2
- 14