Gradle Spotbug 插件

use*_*872 2 gradle spotbugs

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

在我的父母 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Spotbugs 不会在运行时运行

./gradlew 检查

Chr*_*iki 5

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

\n
plugins {\n    // we don\xe2\x80\x99t need to *apply* the plugin to the root project, do we?\n    id \'com.github.spotbugs\' version \'4.7.0\' apply false\n}\n\nsubprojects {\n    apply plugin: \'java\'\n    // this is the most important part, applying the plugin to the subprojects,\n    // too:\n    apply plugin: \'com.github.spotbugs\'\n\n    spotbugs {\n        toolVersion = \'4.2.2\'\n    }\n\n    tasks.withType(com.github.spotbugs.snom.SpotBugsTask) {\n        reports {\n            xml.enabled = false\n            html.enabled = true\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

请注意,我\xe2\x80\x99ve 还做了一些其他更改:

\n
    \n
  • 我\xe2\x80\x99m 使用最新版本的插件,因为您\xe2\x80\x99 使用的插件(1.6.8)已经有好几年了,并且\xe2\x80\x99t 似乎无法与最新版本的插件一起使用摇篮。
  • \n
  • 我\xe2\x80\x99已经删除了sourceSets不需要的配置,并且无论如何都不起作用。
  • \n
  • I\xe2\x80\x99 已将任务类型的完全限定名称替换为最新版本。
  • \n
\n

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

\n