【问题标题】:Adding SpotBugs to my project将 SpotBugs 添加到我的项目中
【发布时间】:2018-12-13 23:11:04
【问题描述】:

我一直在努力将SpotBugs 添加到我目前正在处理的android 项目中。我设法让它工作,但我对它的设置方式并不过分激动。目前,配置位于我的 app/build.gradle 文件中,这使得该文件不太易于管理。

我想知道是否有 SpotBugs/Gradle 专家知道如何将配置提取到单独的文件中。

这是我的 app/build.gradle(样板已删除):

buildscript {
    repositories {
        ...
    }

    dependencies {
        classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
        classpath 'io.fabric.tools:gradle:1.25.4'
        classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
    }
}

plugins {
    id 'com.gladed.androidgitversion' version '0.4.3'
    id "com.github.spotbugs" version "1.6.2"
}

...
apply plugin: 'com.github.spotbugs'
apply from: '../config/quality/quality.gradle'
apply from: '../app/jacoco.gradle'
apply from: '../app/ktlint.gradle'
apply from: '../app/androidgit.gradle'

...

spotbugs {
    toolVersion = '3.1.3'
    ignoreFailures = false

    effort = "min"
    // This selects what level of bugs to report: low means low priority issues will be reported
    // (in addition to medium+high), which corresponds to warning about everything.
    // TODO: boost this to low once low priority issues are fixed.
    reportLevel = "medium"

    excludeFilter = new File("$project.rootDir/config/quality/spotbugs/android-exclude-filter.xml")
}

task spotbugs(type: com.github.spotbugs.SpotBugsTask, dependsOn: 'assemble', group: 'verification') {
    classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

    source = fileTree('src/main/java')


    // Only one report format is supported. Html is easier to read, so let's use that
    // (xml is the one that's enabled by default).
    reports {
        xml.enabled = false
        html.enabled = true
    }

    classpath = files()
}

编辑

每当我尝试将 SpotBugs 从我的 app/build.gradle 中分离出来时,都会遇到以下错误:

Could not get unknown property 'SpotBugsTask' for project ':app' of type org.gradle.api.Project.

这是我的 gradle 文件:

apply plugin: 'com.github.spotbugs'

dependencies {
    checkstyle 'com.puppycrawl.tools:checkstyle:8.11'
    spotbugs "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.2"
//    spotbugs configurations.spotbugsPlugins.dependencies
//    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0'
}

def qualityConfigDir = "$project.rootDir/config/quality";
def reportsDir = "$project.buildDir/reports"

check.dependsOn 'checkstyle'

task checkstyle(type: Checkstyle, group: 'Verification', description: 'Runs code style checks') {
    configFile file("$qualityConfigDir/checkstyle/checkstyle-config.xml")
    source 'src/main/java'
    include '**/*.java'
    exclude '**/model/**'
    exclude '**/AppLogger.java'
    reports {
        xml.enabled = true
        xml {
            destination file("$reportsDir/checkstyle/checkstyle.xml")
        }
    }

    classpath = files()
}

spotbugs {
    toolVersion = '3.1.3'
    ignoreFailures = false

    effort = "min"
    // This selects what level of bugs to report: low means low priority issues will be reported
    // (in addition to medium+high), which corresponds to warning about everything.
    // TODO: boost this to low once low priority issues are fixed.
    reportLevel = "medium"

    excludeFilter = new File("$project.rootDir/config/quality/spotbugs/android-exclude-filter.xml")
}

task spotbugs(type: SpotBugsTask, dependsOn: 'assemble', group: 'verification') {
    classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

    source = fileTree('src/main/java')


    // Only one report format is supported. Html is easier to read, so let's use that
    // (xml is the one that's enabled by default).
    reports {
        xml.enabled = false
        html.enabled = true
    }

    classpath = files()
}

【问题讨论】:

  • “从我的 app/build.gradle 中分离 SpotBugs”是什么意思?
  • 我想把所有的 spotbugs 特定配置放在它自己的 gradle 文件中。
  • 我的意思是一个 spotbugs.gradle 文件。但是,当将其放入单独的 gradle 文件中时,它就无法识别 spotbugs 任务。

标签: android gradle spotbugs


【解决方案1】:

终于找到了解决办法。

我必须将以下内容添加到我在 app/build.gradle 文件中应用所有插件的部分:

project.extensions.extraProperties.set('SpotBugsTask', com.github.spotbugs.SpotBugsTask)

所以它最终看起来像这样:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
        classpath 'io.fabric.tools:gradle:1.25.4'
        classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
    }
}

plugins {
    id 'com.gladed.androidgitversion' version '0.4.3'
    id "com.github.spotbugs" version "1.6.2"
}

// Workaround to be able to access SpotBugsTask from external gradle script.
// More info: https://discuss.gradle.org/t/buildscript-dependencies-in-external-script/23243
project.extensions.extraProperties.set('SpotBugsTask', com.github.spotbugs.SpotBugsTask)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'org.jetbrains.dokka-android'
apply plugin: 'io.fabric'
apply plugin: 'spoon'
apply from: '../app/checkstyle.gradle'
apply from: '../app/jacoco.gradle'
apply from: '../app/ktlint.gradle'
apply from: '../app/androidgit.gradle'
apply from: '../app/spotbugs.gradle'

android {
...

我的 spotbugs.gradle 文件:

dependencies {
    spotbugs configurations.spotbugsPlugins.dependencies
    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0'
}

def qualityConfigDir = "$project.rootDir/config/quality"
def reportsDir = "$project.buildDir/reports"

spotbugs {
    toolVersion = "$spotbugs_version"
    ignoreFailures = false

    effort = "min"
    // This selects what level of bugs to report: low means low priority issues will be reported
    // (in addition to medium+high), which corresponds to warning about everything.
    // TODO: boost this to low once low priority issues are fixed.
    reportLevel = "medium"

    excludeFilter = new File("$qualityConfigDir/config/quality/spotbugs/android-exclude-filter.xml")
}

tasks.register("spotbugs", SpotBugsTask) {
    dependsOn 'assemble'
    group = "verification"
    classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

    source = fileTree('src/main/java')


    // Only one report format is supported. Html is easier to read, so let's use that
    // (xml is the one that's enabled by default).
    reports {
        xml.enabled = true
        xml {
            destination file("$reportsDir/spotbugs/spotbugs.xml")
        }
        html.enabled = true
    }

    classpath = files()
}

【讨论】:

  • 您知道如何指定要使用的 SpotBugs 版本吗? toolVersion 是插件的版本,而不是 SpotBugs 依赖项的版本。借助 FindBugs,它引入了一个依赖项 findbugs,可以在其中指定 FindBugs 依赖项版本。
  • 我在我的 spotbugs.gradle 中指定了工具版本。我已经编辑了答案以包括我的 spotbugs.gradle。工具版本在我的项目构建脚本的依赖项部分中指定。
  • spotbugsspotbugsPlugins 的依赖关系是什么?
  • 这是为了加载findsecbugs-plugin 一个spotbugs 插件。它能够发现安全漏洞。
  • 我明白了;最后一件事。如果您显示 spotbugs 依赖项,它将很有用。也许您应该编辑您的答案以添加这些信息,而不是将它们作为 cmets。
【解决方案2】:

对于任何偶然发现此线程并且对上述答案不满意的人(您应该在任何时候看到“这有效”而没有“因为...”),请注意,如果您使用的是外部 buildscript 文件,例如OP,并尝试配置任务,真正的问题是脚本插件 ClassLoader 与项目 buildscript ClassLoader 隔离,代表com.github.spotbugs.SpotBugsTask 类型的java.lang.Class 实例不同,因此withType 调用不匹配任何东西。

请参阅gradle-native#742gradle#1262 了解详细信息,以及一些使其正常工作的解决方案。

【讨论】:

  • 你应该在这里真正指定解决方案,而不是提供一个将来可能会死的链接
  • @bharal 如果 github 死了,stackoverflow 也会死。此外,解决方案取决于用例;没有万能的解决方案。
猜你喜欢
  • 2023-03-17
  • 2014-12-19
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2014-03-07
相关资源
最近更新 更多