Gradle spotbugs 插件

Gradle spotbugs plugin

我是 Gradle 的新手,正在尝试为我的 Spring 引导多模块项目配置 Spotbugs。

在我的 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 不会 运行 在 运行ning

./gradlew check

下面的工作(一些调整使其在本地工作)

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
        }
    }
}

您的构建配置的主要问题是您将 SpotBugs 插件应用于您的根项目。以下配置解决了这个问题(为简洁起见,省略了与 SpotBugs 插件无关的配置):

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
        }
    }
}

使用此配置,./gradlew check 还运行子项目的 SpotBugs 任务(使用 Gradle 6.8.3 测试)。

请注意,我还进行了一些其他更改:

  • 我使用的是最新版本的插件,因为您使用的插件 (1.6.8) 已经有好几年了,似乎不适用于 Gradle 的最新版本。
  • 我删除了 sourceSets 配置,它不需要也不管用。
  • 我已将任务类型的完全限定名称替换为最新版本。

希望对您有所帮助。如果您出于某种原因坚持使用旧的 SpotBugs 版本,请告诉我;在这种情况下,了解您使用的 Gradle 版本会有所帮助。