user1578872
user1578872

Reputation: 9048

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

Upvotes: 2

Views: 6347

Answers (2)

Chriki
Chriki

Reputation: 16358

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’m using a recent version of the plugin as the one that you’ve used (1.6.8) is several years old and doesn’t seem to work with recent versions of Gradle.
  • I’ve removed the sourceSets configuration which is not needed and doesn’t work anyway.
  • I’ve replaced the fully qualified name of the task type with an up-to-date version.

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.

Upvotes: 6

PrasadU
PrasadU

Reputation: 2438

The below works (some adjustments to make it work locally)

gradle - 6.5.1

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

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

import com.github.spotbugs.snom.SpotBugsTask

allprojects {
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'

    repositories {
        mavenCentral()
    }

    dependencyManagement {
        imports {
            
        }
    }

    configurations{
    }

    sourceCompatibility = '15'
    targetCompatibility = '15'

    dependencies {
    }

    spotbugs {
        toolVersion = '4.2.1'
    }
    

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

Upvotes: 1

Related Questions