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

huangapple go评论88阅读模式
英文:

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 需要 Java 11 或更高版本才能在 SonarQube 8.x 中运行
不再支持属性 'sonar.jacoco.reportPath'。请使用 JaCoCo 的 xml 报告和 sonar-jacoco 插件。
不再支持属性 'sonar.jacoco.reportPaths'。请使用 JaCoCo 的 xml 报告和 sonar-jacoco 插件。

经过半天的谷歌搜索,唯一真正的解决方案是以下内容:
https://stackoverflow.com/questions/44892248/property-sonar-jacoco-reportpath-is-deprecated-please-use-sonar-jacoco-repor

此答案此处解释了以下内容的双重输出:

不再支持属性 'sonar.jacoco.reportPaths'。请使用 JaCoCo 的 xml 报告和 sonar-jacoco 插件。

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

我是否漏掉了什么?

英文:

Code Coverage is showing 0% on dashboard

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

build.gradle file

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

Running this command

./gradlew build jacocoTestReport sonarqube

The JacocoTestReport gets generated with the correct code coverage

Sonarqube gradle task produces this log

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

Been Googling for half a day, and the only real solutions to this problem is the following:
https://stackoverflow.com/questions/44892248/property-sonar-jacoco-reportpath-is-deprecated-please-use-sonar-jacoco-repor

This answer here explains the double output of:

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

However this seems to not have been added to the gradle plugin as the plugin being used is 2.8, the lastest as of posting.

Is there something I'm missing?

答案1

得分: 7

你必须将XML报告属性设置为true。

xml.enabled true

英文:

You have to enable XML report property as true.

xml.enabled true

答案2

得分: 4

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

我并没有使用 Gradle Sonar 插件,而是使用 Jenkins 作业的 -> 执行 SonarQube 扫描配置。

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

以下 Gradle 代码将启用 XML 报告,并将生成默认名称为 jacocoTestReport.xml 的文件。

jacocoTestReport {
    reports {
        xml.enabled true
    }
}

这将在 Jenkins 工作区的位置 <JenkinsJobFullPath>/ws/build/reports/jacoco/test/jacocoTestReport.xml 生成以下文件,同时还会生成包含所有覆盖率报告的 HTML 文件的文件夹 <JenkinsJobFullPath>/ws/build/reports/jacoco/html。可以通过访问位于 <JenkinsJobFullPath>/ws/build/reports/jacoco/html/index.xml 的 index.html 文件来访问此报告。

并且 Jacoco XML 报告文件的路径需要提供在下面的属性中:

sonar.coverage.jacoco.xmlReportPaths=&lt;rootFolder&gt;/build/reports/jacoco/test/jacocoTestReport.xml

这对我有效。

在此之前,我在 SonarQube 中无法看到覆盖率,在另一个项目中,覆盖率显示为 0.0%。

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

英文:

The issue in your configuration is type of the property name. It is sonar.coverage.jacoco.xmlReportPaths and not sonar.coverage.jacoco.xmlReportPath

I am not using the gradle sonar plugin, but using Jenkin Job's -> Execute SonarQube Scanner configuration.

By default Jacoco generates only html files, for SonarQube we need 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 <JenkinsJobFullPath>/ws/build/reports/jacoco/test/jacocoTestReport.xml along with
<JenkinsJobFullPath>/ws/build/reports/jacoco/html folder which contains all the html file for the coverage reports. This report can be accessed by accessing index.html file located at <JenkinsJobFullPath>/ws/build/reports/jacoco/html/index.xml

And path to the Jacoco xml report file to be provided in the below property

sonar.coverage.jacoco.xmlReportPaths=&lt;rootFolder&gt;/build/reports/jacoco/test/jacocoTestReport.xml

This did work for me.

Before this in SonarQube I was not able to see the Coverage and in other project Coverage was shown as 0.0%.

So, in summary SonarQube is not able to see your JaCoCo report file.

答案3

得分: 4

根据qasanov的回答,为了使JaCoCo生成XML报告,并且使SonarQube能够自动识别,我不得不将以下内容添加到我的build.gradle文件中:

jacocoTestReport {
    reports {
        xml.required = true
    }
}
英文:

To expand on qasanov's answer, I had to add this to my build.gradle file in order for JaCoCo to generate the XML report, which was then picked up automatically by SonarQube:

jacocoTestReport {
    reports {
        xml.required = true
    }
}

huangapple
  • 本文由 发表于 2020年4月7日 00:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定