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

44,044

Solution 1

It seems that your build is based on Gradle. It would be easier to use jacoco and sonarqube plugins in the gradle build

plugins {
    id "jacoco"
    id "org.sonarqube" version "2.8"
}

you don't need sonar-project.properties, analysis is configured from your build. You can customize default values in sonarqube configuration

// in build.gradle
sonarqube {
    properties {
        property "sonar.exclusions", "**/*Generated.java"
    }
}

To enable coverage you need to configure gradle to generate XML report

jacocoTestReport {
    reports {
        xml.enabled true
    }
}

And then run with gradle build jacocoTestReport sonarqube. More details can be found here and in SonarScanner for Gradle doc

Solution 2

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.

Share:
44,044
JJD
Author by

JJD

Android, Kotlin, Java, Git, Python, Ruby, Ruby on Rails, JavaScript, MacOS, Ubuntu #SOreadytohelp http://stackoverflow.com/10m

Updated on July 30, 2022

Comments

  • JJD
    JJD almost 2 years

    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

  • JJD
    JJD over 4 years
    The build is based on Gradle. I successfully generated the report file in build/reports/jacoco/test/jacocoTestReport.xml - I hope the file name fits for the SonarQube plugin - I read about report.xml before. - Please add in your answer whether I can leave out the sonar-project.properties configuration. How does SonarQube pick up variables and paths which I defined such as projectKey, ...?
  • JJD
    JJD over 4 years
    I checked the "Analysis property defaults". Some Gradle defaults match my current configuration in sonar-project.properties but not all. So I cannot get rid of it. How can I customize the value of individual sonar properties without using a sonar-project.properties? - BTW: Please fix this typo classeDir in the docs.
  • Tibor Blenessy
    Tibor Blenessy over 4 years
    I added the paragraph about customizing the default values. Thanks for noting the typo
  • JJD
    JJD over 4 years
    Thank you. I managed to migrate to the sonarqube plugin configuration block as well as the xmlReportPaths. The warnings disappeared in the SonarQube web interface. - I specified build/reports/jacoco/test/jacocoTestReport.xml manually, just to be sure the path fits ^^. I like to mention that in sonar.java.test.binaries=build/classes/test the value changed to property "sonar.java.test.binaries", "build/classes/java/test" once I used the sonarqube configuration block - mind the java.
  • Girish
    Girish about 3 years
    @TiborBlenessy I am getting the below message in Jenkins Console, I have sonar-project. properties file, but when I did run it in localhost path is correct No coverage report can be found with sonar.coverage.jacoco.xmlReportPaths='app/build/reports/jaco‌​coTestReport/jacocoT‌​estReport.xml'. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.x‌​ml,build/reports/jac‌​oco/test/jacocoTestR‌​eport.xml
  • Tibor Blenessy
    Tibor Blenessy about 3 years
    @Girish comment system on SO is not good enough to troubleshoot such issues, I would invite you to describe your problem on community.sonarsource.com
  • Girish
    Girish almost 3 years
    @TiborBlenessy Posted in Community...but I am not getting any help
  • Tibor Blenessy
    Tibor Blenessy almost 3 years
    @Girish can you share the link?
  • john ktejik
    john ktejik almost 3 years
    Could not get unknown property 'html' for task ':jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.
  • Prasanth Rajendran
    Prasanth Rajendran almost 3 years
    Have you tried html.required = true in report closure? Which version of Gradle you are using?