8

Code Coverage is showing 0% on dashboard

enter image description here

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: Property 'sonar.jacoco.reportPath' is deprecated. Please use 'sonar.jacoco.reportPaths' instead

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?

bWilliamson
  • 174
  • 1
  • 9
  • are you aware of the missing s in `sonar.coverage.jacoco.xmlReportPaths` at the end? – Simon Schrottner Apr 10 '20 at 15:10
  • @SimonSchrottner in the docs both `Paths` and `Path` are accepted, either way I've tried both and they don't work – bWilliamson Apr 14 '20 at 09:00
  • you do not need sonar.jacoco.reportPaht(s) if you use `sonar.coverage.jacoco.xmlReportPaths` as long as you use `sonar.coverage.jacoco.xmlReportPaths` and not `sonar.coverage.jacoco.xmlReportPath` -https://docs.sonarqube.org/latest/analysis/coverage – Simon Schrottner Apr 14 '20 at 09:08

3 Answers3

7

You have to enable XML report property as true.

xml.enabled true

qasanov
  • 427
  • 1
  • 7
  • 20
4

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 /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. This report can be accessed by accessing index.html file located at /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=<rootFolder>/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.

Sanjay Bharwani
  • 3,317
  • 34
  • 31
4

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
    }
}
luthier
  • 2,674
  • 4
  • 32
  • 35