Exclusions¶
Exclude files from check¶
All quality tasks are based on SourceTask which allows excluding sources using ant patterns.
To apply exclusion to all plugins at once use:
quality { exclude '**/sample/**' }
Note
This will not affect animalsniffer plugin, because it checks different thing (binary compatibility) and use it's own configuration to configure exclusions.
Info
Spotbugs task does not support exclusions on task level, so plugin manually resolve all excluded classes and add them to spotbugs excludes filter xml file (default or custom user file). As a result, exclusion works the same way for all plugins.
Note
Exclusion patterns are resolved on relative paths (relative to source dir), so absolute file path matching will not work. Your pattern must match just "package" and file name parts. If you need to exclude on full paths then use direct source exclusion.
Alternative configurations¶
All of the following configurations are allowed:
quality { exclude '**/Sample.java', 'com/foo/**' }
quality { exclude '**/Sample.java' exclude 'com/foo/**' }
quality { exclude = ['**/Sample.java', 'com/foo/**'] }
Direct source exclusions¶
When you need to exclude sources from check, you should consider:
- Extract such sources into it's own source set and exclude this set from check. (generated classes case)
- Use pattern excludes (see above) to exclude sources based on package and (or) file name
If non of the above works for you, then you did sources configuration not according to best practices. Anyway, there is last resort option for such cases (when it could not be done the right way).
Suppose we have generated sources, added to main source set:
sourceSets.main { java { srcDir 'build/generated/java' } }
Here we have two source dirs for java sources: src/main/java
and build/generated/java
.
We want to exclude them from quality check, so we try:
Fail
quality { exclude '**/generated/**' }
which WILL NOT WORK because gradle applies patterns relatively to build/generated/java
directory
and so our patter will never match.
Instead, specify ignored files directly, using rich gradle files api:
Success
quality { excludeSources = fileTree('build/generated') }
This will exclude all files in 'generated' directory from quality tools checks.
Note
As with patterns exclude, this will not affect animalsniffer. For spotbugs, plugin will add excluded classes to exclude filter.
Another example, just to show how flexible it could be configured:
quality { excludeSources = fileTree('build/generated').matching { include '**/sample/**/*.java' } }
Exclude all java sources in sample package (in generated directory).
Include pattern here will work relatively to build/generated
directory.
You can use even single files:
quality { excludeSources = files('build/generated/java/com/mypkg/Bad.java') }
Exclude options could be used together (exclude files and patterns).