使用 Gradle 的 Sonarqube 8 的 Jacoco 代码覆盖率

bWi*_*son 8 java gradle jacoco docker sonarqube

仪表板上的代码覆盖率显示 0%

在此输入图像描述

构建.gradle 文件

plugins {
    id "org.sonarqube" version "2.8"
    id "java"
    id "idea"
    id "jacoco"
}

jacoco {
    toolVersion = "0.8.5"
}

jacocoTestReport {
    reports {
        html.enabled true
        xml.enabled true
        xml.destination file("${buildDir}/reports/jacoco.xml")
    }
}

plugins.withType(JacocoPlugin) {
    tasks["test"].finalizedBy 'jacocoTestReport'
}

sonarqube {
    properties {
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.host.url", "http://169.254.1.100:9000"
        property "sonar.coverage.jacoco.xmlReportPath", "${buildDir}/reports/jacoco.xml"
    }
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    // https://mvnrepository.com/artifact/junit/junit
    testCompile 'junit:junit:4.12'

}

check.dependsOn jacocoTestReport
Run Code Online (Sandbox Code Playgroud)

运行这个命令

./gradlew build jacocoTestReport sonarqube
Run Code Online (Sandbox Code Playgroud)

使用正确的代码覆盖率生成 JacocoTestReport

Sonarqube gradle 任务生成此日志

> Task :sonarqube
SonarScanner will require Java 11 to run starting in SonarQube 8.x
Property 'sonar.jacoco.reportPath' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.
Property 'sonar.jacoco.reportPaths' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.
Run Code Online (Sandbox Code Playgroud)

谷歌搜索了半天,这个问题的唯一真正的解决方案如下: 属性'sonar.jacoco.reportPath'已被弃用。请改用“sonar.jacoco.reportPaths”

这个答案解释了以下的双重输出:

Property 'sonar.jacoco.reportPaths' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.
Run Code Online (Sandbox Code Playgroud)

然而,这似乎尚未添加到 gradle 插件中,因为正在使用的插件是 2.8,这是发布时的最新版本。

我有什么遗漏的吗?

qas*_*nov 7

您必须将 XML 报告属性启用为 true。

xml.启用 true