Using "excludes" config in Findbugs and Checkstyle plugin in Gradle
Asked Answered
S

2

19

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
}
Scraperboard answered 26/2, 2014 at 9:46 Comment(3)
I have a feeling this is a bug in the Findbugs plugin. The excludes work fine for both Checkstyle and PMD, and according to documentation, they should have the same format.Scraperboard
I got it to work with an external exclude file (see github.com/markuswustenberg/jsense/blob/…), so I think it's indeed a bug in the Findbugs gradle plugin. I'll file a bug when I find out how.Scraperboard
Right, reported here: forums.gradle.org/gradle/topics/…Scraperboard
A
16

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

Aubade answered 26/2, 2014 at 11:6 Comment(4)
Cool, thanks, that works. Which means I perhaps haven't actually found a bug. However, I think it's rather counter-intuitive that the "exclude" works only on sources. That's not what I would expect when using the plugin after reading the documentation, especially when it works as expected with both Checkstyle and PMD. Thoughts?Scraperboard
Checkstyle and PMD primarily operate on source files, so SourceTask#exclude is good enough for them. It would be nice to have a simpler way to filter FindBugs classes, but it's not immediately clear how to do this, especially in a backwards compatible way.Aubade
Regarding your PS: Filtering the source makes absolutely no difference. I guess that method is quite useless, then? Or is there a use case where FindBugs actually checks the source?Scraperboard
It does use the sources (at least in some cases), but my guess is that the class files determine what's being analyzed.Aubade
P
2

If someone looking for a modern day solution:
For checkstyle, you can use something like this in build.gradle:

checkstyleMain.exclude '**/org/jsense/serialize/protobuf/gen/**'

If you want to exclude more than one path
solution 1:

checkstyleMain.exclude '**/org/jsense/serialize/protobuf/gen/**'
checkstyleMain.exclude '**/org/example/some/random/path/**'

solution 2:

checkstyleMain {
    setExcludes(new HashSet(['**/org/jsense/serialize/protobuf/gen/**', '**/org/example/some/random/path/**']))
}
Patsypatt answered 29/5, 2019 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.