spotbugs-gradle-plugin icon indicating copy to clipboard operation
spotbugs-gradle-plugin copied to clipboard

Is there a recommended way to disable spotbugsTest? Could there be a setting?

Open booniepepper opened this issue 4 years ago • 9 comments

I own a Gradle plugin at my company (Amazon) that's kind of an "aggregate" plugin for quality defaults. It gets used by a lot of engineering teams.

I think it makes sense as a default to run spotbugs against source code and test code (and really any code) but sometimes developers don't want to run spotbugs against their test code. Especially if they're vending libraries that do arcane and fiddly things where they will purposefully raise "bugs" to test against.

In older versions of this plugin, it was possible to do something like this:

spotbugs {
    sourceSets = [sourceSets.main]
    ignoreFailures = false
    // etc
}

Note: The above more-or-less still works with some built-in plugins (e.g. checkstyle)

In the current version of the plugin my cleanest way to do configure it is something like this:

spotbugs {
    ignoreFailures.set(false)
    // etc
}
spotbugsTest {
    ignoreFailures = true
    // etc
}

This will still run spotbugs against the test suite though, and for the dev teams that would rather disable it, it's unnecessary build delays.

Of course I know I can wrap the whole thing in a plugin and fiddle with anything I want. The work done to improve this plugin over previous versions is awesome, especially breaking the reliance/exposure of gradle's ever-changing internal apis. But, I'd rather not add unique behavior just for us.

So tl;dr: I want a simple way to disable spotbugs against tests. One way would be to expose configurations of the source sets. If the latter is ok, I'd be interested in contributing.

booniepepper avatar Nov 27 '20 18:11 booniepepper

spotbugsTest.enabled = false is what I use

jscancella avatar Nov 27 '20 21:11 jscancella

Unfortunately, that doesn't prevent the task from being created and the check task depending on it. So even if you use spotbugsTest.enabled = false, it will run compileTestGroovy for example.

jochenberger avatar May 27 '21 14:05 jochenberger

Unfortunately, that doesn't prevent the task from being created and the check task depending on it. So even if you use spotbugsTest.enabled = false, it will run compileTestGroovy for example.

Seems like a bug in gradle then, because if a task is disabled it shouldn't run tasks that it depends on.

jscancella avatar May 27 '21 14:05 jscancella

Sounds reasonable.

jochenberger avatar May 27 '21 15:05 jochenberger

See https://github.com/gradle/gradle/issues/17306

jochenberger avatar May 28 '21 06:05 jochenberger

So, it seems that, as long as we cannot choose the source sets for which the tasks are created, the best way to prevent the spotbugsTest (and its dependencies) from running is to do exactly that by specifying -x spotbugsTest on the command line.

jochenberger avatar Jun 05 '21 08:06 jochenberger

This works for me to disable spotbugsTest in build.gradle:

// kotlin dsl
tasks {
    spotbugsTest {
        onlyIf { false }
    }
}

gradle reports it as

> Task :spotbugsTest SKIPPED

hauner avatar Nov 20 '21 15:11 hauner

A little late to the party, but

spotbugs<SourceSetName> {
    enabled = false;
} 

works for any source set.

Example:


sourceSets {
   benchmarks {
     java {}
   }
}
spotbugsBenchmarks {
   enabled = false
}

josiahhaswell avatar Apr 12 '22 20:04 josiahhaswell

My preferred solution (from this StackOverflow answer) is:

project.gradle.startParameter.excludedTaskNames.add(":spotbugsTest")

I have this configuration in tasks { }, directly adjacent to my spotbugsMain task configuration. E.g.:

tasks {
    // ...
    
    // spotbugs-gradle-plugin creates a :spotbugsTest task by default, but we don't want it
    // see: https://github.com/spotbugs/spotbugs-gradle-plugin/issues/391
    project.gradle.startParameter.excludedTaskNames.add(":spotbugsTest")

    spotbugsMain {
        baselineFile.set(file("$rootDir/config/spotbugs/baseline.xml"))
        reports.create("html") {
            required.set(true)
        }
    }
    // ...
}

I prefer this to skipping the task, as now :spotbugsTest no longer shows up when I'm doing task listing or analysis.

jobarr-amzn avatar Feb 02 '23 03:02 jobarr-amzn