繁体   English   中英

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

[英]Jacoco code coverage for Sonarqube 8 using Gradle

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

在此处输入图像描述

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”

这个答案在这里解释了双 output 的:

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

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

有什么我想念的吗?

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

xml.enabled true

您的配置中的问题是属性名称的类型。 它是sonar.coverage.jacoco.xmlReportPaths而不是sonar.coverage.jacoco.xmlReportPath

我没有使用 gradle 声纳插件,而是使用 Jenkins Job 的 -> Execute SonarQube 扫描仪配置。

默认情况下,Jacoco 只生成 html 文件,对于 SonarQube,我们需要 xmlReportPath。

Below code in gradle will enable the xml reporting and will generate the file with default name as jacocoTestReport.xml jacocoTestReport { reports { xml.enabled true } }

This generates the following file in Jenkins workspace at location /ws/build/reports/jacoco/test/jacocoTestReport.xml along with /ws/build/reports/jacoco/html folder which contains all the html file for the coverage reports. 可以通过访问 index.html 文件访问此报告,该文件位于 /ws/build/reports/jacoco/html/index.xml

Jacoco xml 报告文件的路径将在以下属性中提供

sonar.coverage.jacoco.xmlReportPaths=<rootFolder>/build/reports/jacoco/test/jacocoTestReport.xml

这确实对我有用。

在此之前,在 SonarQube 中,我无法看到覆盖率,而在其他项目中,覆盖率显示为 0.0%。

因此,总而言之,SonarQube 无法看到您的 JaCoCo 报告文件。

为了扩展qasanov的答案,我必须将其添加到我的build.gradle文件中,以便 JaCoCo 生成 XML 报告,然后由 Z1D7B02DA4F367C28D1FB9DFC6F520 自动获取:

jacocoTestReport {
    reports {
        xml.required = true
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM