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

问题描述 投票:1回答:1

代码覆盖率在仪表板上显示为0%。

enter image description here

build.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

运行此命令

./gradlew build jacocoTestReport sonarqube

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.

上网查了半天,真正能解决这个问题的只有以下几点。"sonar.jacoco.reportPath "这个属性已经废弃了。请使用'sonar.jacoco.reportPaths'代替。

这个答案 此处 解释了为什么会有双重输出。

Property 'sonar.jacoco.reportPaths' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.

但是这个似乎并没有被添加到gradle插件中去 因为现在使用的插件是2. 8,也就是截止到发帖时的最新版本。

我是不是遗漏了什么?

java docker gradle sonarqube jacoco
1个回答
0
投票

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

xml.enabled true

© www.soinside.com 2019 - 2024. All rights reserved.