gradlespotbugs

Gradle spotbugs plugin


I am new to Gradle and trying to configure Spotbugs for my Spring Boot multi module project.

In my parent, build.gradle,

buildscript {
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${versionSpringBoot}"
    }
}

plugins {
  id 'com.github.spotbugs' version '1.6.8'
}

allprojects {
    apply plugin: 'eclipse'
    apply plugin: 'idea'
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'pmd'
    apply plugin: 'jacoco'

    dependencyManagement {
        imports {
            
        }
    }

    configurations{
    }

    sourceCompatibility = '15'
    targetCompatibility = '15'

    dependencies {
    }

    pmd {
        consoleOutput = true
        toolVersion = "${versionPmd}"
        sourceSets = [sourceSets.main]
        ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
    }

    spotbugs {
        toolVersion = "${versionSpotBugs}"
        sourceSets = [sourceSets.main]
    }
    
    jacoco {
        toolVersion = "${versionJacoco}"
    }

    jacocoTestReport {
        reports {
            xml.enabled = true
        }
    }

    tasks.withType(com.github.spotbugs.SpotBugsTask) {
        reports {
            xml.enabled = false
            html.enabled = true
        }
    }
}

Spotbugs doesn't run on running

./gradlew check


Solution

  • The main issue with your build configuration is that you apply the SpotBugs plugin only to your root project. The following configuration solves that (leaving out configurations that are unrelated to the SpotBugs plugin for brevity):

    plugins {
        // we don’t need to *apply* the plugin to the root project, do we?
        id 'com.github.spotbugs' version '4.7.0' apply false
    }
    
    subprojects {
        apply plugin: 'java'
        // this is the most important part, applying the plugin to the subprojects,
        // too:
        apply plugin: 'com.github.spotbugs'
    
        spotbugs {
            toolVersion = '4.2.2'
        }
    
        tasks.withType(com.github.spotbugs.snom.SpotBugsTask) {
            reports {
                xml.enabled = false
                html.enabled = true
            }
        }
    }
    

    With this configuration, ./gradlew check also runs the SpotBugs tasks of subprojects (tested with Gradle 6.8.3).

    Please note that I’ve also made a few other changes:

    I hope this helps. Please let me know if you’re stuck with the old SpotBugs version for some reason; knowing the Gradle version that you use would help in that case.