spotbugs / spotbugs-gradle-plugin

https://plugins.gradle.org/plugin/com.github.spotbugs
Apache License 2.0
180 stars 68 forks source link

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

Open booniepepper opened 4 years ago

booniepepper commented 4 years ago

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.

jscancella commented 4 years ago

spotbugsTest.enabled = false is what I use

jochenberger commented 3 years ago

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.

jscancella commented 3 years ago

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.

jochenberger commented 3 years ago

Sounds reasonable.

jochenberger commented 3 years ago

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

jochenberger commented 3 years ago

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.

hauner commented 3 years ago

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

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

gradle reports it as

> Task :spotbugsTest SKIPPED
josiahhaswell commented 2 years ago

A little late to the party, but

spotbugs<SourceSetName> {
    enabled = false;
} 

works for any source set.

Example:


sourceSets {
   benchmarks {
     java {}
   }
}
spotbugsBenchmarks {
   enabled = false
}
jobarr-amzn commented 1 year ago

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.