Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure sonar.coverage.jacoco.xmlReportPaths for JaCoCo/SonarQube?

SonarQube 7.7 shows the following warning for a Java project analysis:

Property 'sonar.jacoco.reportPath' is deprecated (JaCoCo binary format). 'sonar.coverage.jacoco.xmlReportPaths' should be used instead (JaCoCo XML format).

The Gradle based project is configured via sonar-project.properties as follows:

sonar.projectKey=MyExampleLib
sonar.projectName=MyExample Library
sonar.sources=src/main/java
sonar.jacoco.reportPath=build/jacoco/test.exec
sonar.junit.reportsPath=build/test-results/test
sonar.java.test.binaries=build/classes/test
sonar.java.binaries=build/classes/java/main
sonar.binaries=build/classes
sonar.projectVersion=$libVersion

The SonarQube server URL is injected via (otherwise you end up with a "localhost:9000" error):

Prepare SonarQube Scanner environment

The SonarQube analysis is triggered via Jenkins and the JaCoCo plugin v.3.0.4 with the following Job configuration:

JaCoCo configuration

I read that a report.xml is picked up by xmlReportPaths. How can I generate it?

Related

  • https://github.com/jacoco/jacoco/issues/919
  • https://github.com/jacoco/jacoco/commit/6babdb5233217b0812a85f6b1673aabe7f0fd47e
like image 338
JJD Avatar asked Nov 13 '19 14:11

JJD


People also ask

Does SonarQube use JaCoCo?

For Java projects, SonarQube directly supports the JaCoCo coverage tool (see Generic Test Data for information on integrating other coverage tools).

How do I show code coverage in SonarQube?

SonarQube itself does not calculate coverage. To include coverage results in your analysis, you must set up a third-party coverage tool and configure SonarQube to import the results produced by that tool. Below, you'll find guidelines and resources, as well as language- and tool-specific analysis parameters.

What is difference between JaCoCo and SonarQube?

JaCoCo vs SonarQube: What are the differences? JaCoCo: A code coverage library for Java. It is a free code coverage library for Java, which has been created based on the lessons learned from using and integration existing libraries for many years; SonarQube: Continuous Code Quality.


1 Answers

We can generate Jacoco reports in XML format by specifying xml.enabled value to true and providing destination path in the reports section.

plugins {
      id "org.sonarqube" version "2.8"
}
    jacocoTestReport {
        group = "Reporting"
        reports {
            xml.enabled true
            csv.enabled false
            //to create coverage report in html
            html.destination file("${buildDir}/reports/coverage")
            //for XML
            xml.destination file("${buildDir}/reports/jacoco.xml")
        }
    }

The SonarQube properties can be also configured through the build.gradle file. As sonar.jacoco.reportPath and sonar.jacoco.reportPaths are deprecated properties from the sonar version of 7.7 which should be replaced with sonar.coverage.jacoco.xmlReportPaths.

  • Configuring the Sonarqube properties through the build.gradle
sonarqube {
    properties {
        property 'sonar.projectName', 'MyExample Library'
        property 'sonar.projectKey', 'MyExampleLib'
        property 'sonar.core.codeCoveragePlugin', 'jacoco'
        property 'sonar.coverage.jacoco.xmlReportPaths', '${project.buildDir}/reports/jacoco.xml'
    }
}

  • If you wish to do that through sonar-project.properties then update the deprecated properties mentioned below to the suggested one.
sonar.jacoco.reportPath=build/reports/jacoco.xml

Finally, by executing gradle jacocoTestReport sonarqube command, the jacoco test report files such as ${project.buildDir}/reports/jacoco.xml and ${project.buildDir}/jacoco/test.exec will be generated for SonarQube.

like image 140
Prasanth Rajendran Avatar answered Sep 21 '22 05:09

Prasanth Rajendran