"Project coverage is set to 0%" – JaCoCo and Sonar in Jenkins with Ant

14,417

I've recently setup and successfully got Sonar and Jacoco running together. Since I'm recent with the topic, I figured I'd check on stackoverflow for similar issues and help out. I am getting results from Jacoco, but found you had to explicitly set the following parameters in addition to the properties you've listed in your post:

sonar.core.codeCoveragePlugin=jacoco
sonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec
sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=tests/test-reports

You have to set sonar.core.codeCoveragePlugin=jacoco if you want to be able to use the sonar.jacoco.reportPath property. Otherwise, you will have to use the sonar.jacoco.itReportPath property. However, I recommend just setting the codeCoveragePlugin and reportPath properties. Otherwise, it won't display under the default coverage widget in sonar. Please make note, you cannot use the default coverage tool and jacoco together. It has to be one or the other. I decided to use Jacoco.

Your ant target must be configured to generate the jacoco.exec results prior to running the sonar tasks:

<jacoco:coverage enabled="${tests.code.coverage}" destfile="${jacoco.exec.dest}">
  <junit fork="yes" printsummary="withOutAndErr" dir="${tests.working.dir}">
  ...

Be sure to tell sonar to reuse reports and any sunfire reports if you're running junit before sonar, that is if you're running junit outside of sonar:

sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec
sonar.surefire.reportsPath=tests/test-reports

For whatever reason, if you need more verbose debugging, use the following property:

sonar.verbose=true

Share:
14,417
jdtangney
Author by

jdtangney

Bringing Lean and UX to Security and Privacy, and vice versa

Updated on June 09, 2022

Comments

  • jdtangney
    jdtangney almost 2 years

    I moved my job from a single Hudson machine to a multi-slave Jenkins environment, and now JaCoCo coverage no longer works.

    WORKING (old): Hudson 2.0.1, Jenkins Sonar Plugin 1.7.1, Sonar 2.1.2

    BROKEN (new): Jenkins 1.446, Jenkins Sonar Plugin 1.7.2, Sonar 2.1.2

    My Hudson job is called Pinnacle and it used to run on a single Hudson server with Sonar on the same machine. I set up my (NO MAVEN) build by doing the following.

    1) Added an Ant target to my build.xml called test-with-coverage

    2) Configured the Pinnacle job in Hudson to "invoke standalone Sonar Analysis" with these properties:

    sonar.projectKey=com.skyboximaging:pinnacle
    sonar.projectName="Pinnacle"
    sonar.projectVersion=1.0
    sources=Pinnacle/src/java
    tests=Pinnacle/test/java
    binaries=Pinnacle/classes
    sonar.jacoco.reportPath=Pinnacle/jacoco.exec
    sonar.jacoco.antTargets=test-with-coverage
    

    (Note that the code is checked out into Pinnacle directory in the Jenkins job workspace.)

    3) Configured Sonar "general settings" to use JaCoCo for code coverage

    Everything worked beautifully!

    But in the new Jenkins environment, I see this error in the Jenkins build output:

    23:15:17.863 INFO  Sensor JaCoCoSensor...
    23:15:17.868 INFO  Project coverage is set to 0% as no JaCoCo execution data has been dumped: /var/lib/jenkins/workspace/Pinnacle/Pinnacle/jacoco.exec
    

    That file does not exist on the slave where the build ran. (The directory /var/lib/jenkins/workspace/Pinnacle/Pinnacle does exist.)

    All other sensors (FindBugs, PMD, etc) appear to be working OK. Just JaCoCo is broken.

    Does Sonar/JaCoCo even work in a multi-slave Jenkins environment?

    I suspect that the Ant task test-with-coverage is not getting run. How does Sonar locate the build.xml? And what is different between old and new installations?

  • Dennis S.
    Dennis S. almost 12 years
    Thanks! The pointer to set sonar.jacoco.reportPath was the last bit I needed to get my multi-project ant driven sonar to load coverage data correctly.
  • vinod
    vinod almost 11 years
    @Jason: if i am using sahi instead of junit reports then can i use sahi reports(which are in html) in sonar.surefire.reportsPath