Search code examples
javagradlespotbugs

Spotbugs configuration for multi project setup


I have a multi-project Gradle setup like so:

RootProject
    |
    ---- ProjectA
    |
    ---- ProjectB
    |
    ---- ProjectC

I want to apply SpotBugs to all my projects.

Doing the following in every project explicitly works.

For example, the following in build.gradle of ProjectA:

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

spotbugs {
    ignoreFailures = true
    effort = 'min'
    showProgress = true
    reportLevel = 'medium'
    // Only run spotbugs directly as too slow to run as CI.  
    // Will still run spotbugs when called like "gradlew spotbugsMain"
    sourceSets = []
}

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

However, I do not want to duplicate this code in build.gradle files of all the projects.

How do I pull this out into the RootProject's build file.

Example, inside the following block:

configure(subprojects.findAll {it.name != 'SomeProjectToIgnore'}) {
   // how to configure spotbugs here ?
}

Moving the block as is doesn't work.


Solution

  • You can configure spotbugs in root project's build.gradle the following way:

    buildscript {
        repositories {
            maven { url 'maven repository url' }
        }
    
        dependencies {
            classpath group: 'gradle.plugin.com.github.spotbugs', name: 'spotbugs-gradle-plugin', version: '1.6.5'
        }
    }
    
    allprojects {
    
        apply plugin: 'com.github.spotbugs'
    
        dependencies {
            compileOnly group: 'com.github.spotbugs', name: 'spotbugs-annotations', version: '3.1.8'
            spotbugsPlugins group: 'com.h3xstream.findsecbugs', name: 'findsecbugs-plugin', version: '1.8.0'
        }
    
        spotbugs {
            toolVersion = '3.1.8'
            sourceSets = [ sourceSets.main ]
        }
    
    }