2
votes

sonar is not picking up the code coverage reports generated by IntellijIdea/Jacoco coverage reports.

Heres one sample of me trying to get sonar to pickup the jacoco report generated

Build Step:
Step 1
Ant
Build file path: junit-build.xml
Targets: default
JaCoCo coverage enabled
Execute: If all previous steps finished successfully
Step 2
SonarQube Runner Execute: If all previous steps finished successfully

Artifact:
.teamcity
    coverage_jacoco
        jacoco.exec

UT JaCoCo Report: .teamcity/coverage_jacoco/jacoco.exec

JaCoCoSensor: JaCoCo report not found : /export/.../BuildAgent2/work/.../.teamcity/coverage_jacoco/jacoco.exec

Sonar:
This component does not have coverage details.

1
I have the same issue. Jacoco runs via teamcity as part of the build step, and sonar runs by adding sonar:sonar to the maven goals in the same step. I think teamcity places the jacoco files outside of the build area and the publishing-artifacts to .teamcity/coverage_jacoco part happens after sonar has already run. Would be great to fix this.AutomatedMike

1 Answers

0
votes

The issue is that the TeamCity jacoco runner places the output files in a temporary file location that can't be found by the sonar plugin or by sonar when run as a part of the maven/ant build. ".teamcity" is in the published artifacts store not the build area and before publishing the jacoco output appears to be in a randomly named directory.

The alternative is to turn off the teamcity jacoco runner and trigger the coverage and sonar scan via the maven or ant build itself. Sonar can be configured to find the results by putting the correct location in the administration->java->jacoco settings (relative to the build or module root) This will get your coverage into sonar.

To add the coverage report back into teamcity you need to add an artifact in your build general settings which collects the report html files into a coverage.zip file. Then you'll get a Code Coverage tab in your build results. (If you get two then you're still running the teamcity coverage runner as well.)

in short for maven builds (ant will be logically the same):

  • turn off the coverage option in the teamcity build step
  • add the org.jacoco:jacoco-maven-plugin:prepare-agent goal to create a report consumable by sonar
  • In sonarqube set the jacoco UT report to "target/jacoco.exec"
  • add the org.jacoco:jacoco-maven-plugin:report goal to create an html report for use by teamcity
  • add this artifact "target/site/jacoco/**/. => coverage.zip" to make the html report appear as a tab in teamcity

For multi-module builds where you have more than one report I can't find a way to get these into teamcity - though they do look fine in sonar. There is a jacoco merge goal that may help but I haven't been successful with it - and I suspect it'll mess up the sonar view.