Using "excludes" config in Findbugs and Checkstyle plugin in Gradle

Markus picture Markus · Feb 26, 2014 · Viewed 15.3k times · Source

I have the following Gradle build file: https://github.com/markuswustenberg/jsense/blob/a796055f984ec309db3cc0f3e8340cbccac36e4e/jsense-protobuf/build.gradle which includes:

checkstyle {
  // TODO The excludes are not working, ignore failures for now
  //excludes '**/org/jsense/serialize/protobuf/gen/**'
  ignoreFailures = true
  showViolations = false
}

findbugs {
  // TODO The excludes are not working, ignore failures for now
  //excludes '**/org/jsense/serialize/protobuf/gen/**'
  ignoreFailures = true
}

As you can see, I'm trying to exclude auto-generated code in the package org.jsense.serialize.protobuf.gen. I cannot figure out the format of the strings given to the excludes parameter, and the documentation isn't of much help: http://www.gradle.org/docs/1.10/dsl/org.gradle.api.plugins.quality.FindBugs.html#org.gradle.api.plugins.quality.FindBugs:excludes (it just says "The set of exclude patterns.").

So my question is: How should the excludes pattern strings be formatted for both the Findbugs and Checkstyle plugins?

I'm running Gradle 1.10.

Thanks!

EDIT 1: I got the Checkstyle exclude working with the following:

tasks.withType(Checkstyle) {
  exclude '**/org/jsense/serialize/protobuf/gen/**'
}

However, using the exact same exclude on the Findbugs plugin doesn't work:

tasks.withType(FindBugs) {
  exclude '**/org/jsense/serialize/protobuf/gen/*'
}

EDIT 2: The accepted answer works, and so does using an XML file and filtering on that, like so:

findbugs {
  excludeFilter = file("$projectDir/config/findbugs/excludeFilter.xml")
}

and

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
  <Match>
    <Package name="org.jsense.serialize.protobuf.gen"/>
  </Match>
</FindBugsFilter>

EDIT 3: This works great, and no XML file is needed:

def excludePattern = 'org/jsense/serialize/protobuf/gen/'
def excludePatternAntStyle = '**/' + excludePattern + '*'
tasks.withType(FindBugs) {
    classes = classes.filter {
        !it.path.contains(excludePattern)
    }
}
tasks.withType(Checkstyle) {
    exclude excludePatternAntStyle
}
tasks.withType(Pmd) {
    exclude excludePatternAntStyle
}

Answer

Peter Niederwieser picture Peter Niederwieser · Feb 26, 2014

SourceTask#exclude filters source files. However, FindBugs primarily operates on class files, which you'll have to filter as well. Try something like:

tasks.withType(FindBugs) {
    exclude '**/org/jsense/serialize/protobuf/gen/*'
    classes = classes.filter { 
        !it.path.contains(new File("org/jsense/serialize/protobuf/gen/").path) 
    }
}

PS: It could be that filtering source files makes no difference (and therefore isn't necessary) in case of FindBugs. (I haven't tried though.)