6

I have a multimodule Maven project where the coverage reports are located in another module than the covered Java classes. An import of a not empty xml coverage report (with coverage information) into Sonarqube is successful but shows a coverage of 0.

Steps to reproduce:

  1. Checkout following github project and build it with mvn clean verify. After that there exist an aggregated xml report located in coverage/target/site/jacoco-aggregate-all/jacoco.xml. You can see coverage data in there and also in the corresponding html-Report.

coverage report

  1. Start sonarqube (current version 8.4.1) with following command and wait a little bit.
docker run -d -p 9000:9000 sonarqube

edit: Plugin "JaCoco xml report importer" is already installed in this image.

JaCoco xml report importer is installed

  1. Publish coverage data with following (verbose) command. Importing of report was successful (see log).
mvn sonar:sonar -X -Dsonar.projectKey=example -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate-all/jacoco.xml

## Log output contains
...
 10:54:28.519 Reading report '<project-path>\maven-multimodule-coverage\coverage\target\site\jacoco-aggregate-all\jacoco.xml'
...
  1. Browse to http://localhost:9000/dashboard?id=example. You see coverage of 0.

What am I doing wrong?

Sonar report

Rokko_11
  • 837
  • 2
  • 10
  • 24

2 Answers2

4

It turned out that it works if I use an absolute path for the xml report file. So I added

<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../coverage/target/site/jacoco-aggregate-all/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

to the top maven pom so each module points to the same report file. For a deeper nesting of module directories you have to introduce a property main.basedir or something.

The main understanding is that you do not provide sonar a coverage report which get mapped to the module classes, but you provide module classes which get mapped to a coverage report.

Rokko_11
  • 837
  • 2
  • 10
  • 24
0

Please install JaCoco xml report importer plugin on sonarqube instance (you will find it under marketplace just search for jacoco there). This plugin will pick up your code coverage and import it to sonarqube. so after installing plugin please use the same command to see result on sonarqube which is

mvn sonar:sonar -X -Dsonar.projectKey=example -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate-all/jacoco.xml

Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
  • Thank you for your answer. In the given docker image this plugin is already installed. See attached screenshot on the main topic. – Rokko_11 Jul 17 '20 at 10:11
  • @Rokko_11 could you please share full log of step 3 which you added above to the description? – Dashrath Mundkar Jul 17 '20 at 10:17
  • I use dockerized maven command to produce [following log](https://pastebin.com/9zKfrdnU). `docker run -it --rm --name example -v "%CD%":/usr/src/mymaven -w /usr/src/mymaven maven:3.6-jdk-8 mvn sonar:sonar -X -Dsonar.projectKey=example -Dsonar.host.url=http://kubernetes.docker.internal:9000 -Dsonar.login=admin -Dsonar.password=admin -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate-all/jacoco.xml | grep -i "jacoco\|sonar|squid" > out-stackoverflow2.log`. Report gets read on line 640 – Rokko_11 Jul 17 '20 at 12:07
  • sonar log says, coverage was successful imported: https://pastebin.com/CgdQg7TH – Rokko_11 Jul 17 '20 at 15:10