Sonarqube: Import Jacoco xml report for multimodule - sonarqube

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:
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.
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.
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'
...
Browse to http://localhost:9000/dashboard?id=example. You see coverage of 0.
What am I doing wrong?

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.

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

Related

SonarQube Jacoco JUnit : SonarScanner get many unexpected problems

I have a simple project that need to Run JUnit test and let SonarQube to scan / run the Test of JUnit result with SonarScanner. I put my project at Github here
There are some problems:
WARN: Class "XXX" is not accessible through the classloader sonar (Reference)
No jacoco-it.exec and jacoco-ut.exec, only run jacoco.exec at SonarScanner
Cannot exclude some files when running Test JUnit, because I only want to Run Test in my Service Folder (Reference)
This is my sonar-project.properties :
# Required metadata
sonar.projectKey=com.example:Sample
sonar.projectName=Java :: Example :: SonarQube Scanner
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.sources=src
sonar.exclusions=src/main/resources/**
#sonar.test.inclusions=src/main/java/com/example/service/**, src/test/java/**
#sonar.test.exclusions=src/main/java/com/example/controller/**
# Encoding of the source files
sonar.sourceEncoding=UTF-8
#Jacoco
sonar.junit.reportsPath=target/surefire-reports
sonar.jacoco.reportPath =target/jacoco.exec
sonar.jacoco.reportMissing.force.zero=true
sonar.java.binaries=target/classes/**
#sonar.java.libraries=libs/**
When I Run Maven Test until build success, It create some files and folders in folder Target.
After that I run my SonarScanner to Scan my project, but I got those problems.
My question:
How can I run my SonarQube Scanner without any WARN ?
How to exclude files properly when scanning JUnit result? Because I use the reference settings, and Coverage still scan the files
How to create jacoco-it.exec and jacoco-ut.exec and run it on SonarScanner?
My project is based on this Reference
Use SonarQube Scanner for Maven instead of just SonarQube Scanner. Otherwise you fall into troubles with configuration, into which you fall, because need to do everything manually:
sonar.sources most likely is incorrect - should be src/main/java and will be properly configured by Scanner for Maven, and that's why you're getting undesired test files analysed as main files
as well as sonar.tests that normally for Maven based projects points to src/test/java
sonar.java.libraries is empty and that's why you're getting Class "XXX" is not accessible through the classloader sonar - it might be quite hard to list all required JAR files of dependencies of your Maven project, while again Scanner for Maven will do this automatically and most importantly will do this correctly
sonar.projectKey with Scanner for Maven will be set automatically to Maven groupId:artifactId
sonar.projectName and sonar.version with Scanner for Maven will be set correctly to Maven project name and version respectively, so that you don't need to change version, when it will change in Maven project
others such as sonar.sourceEncoding, sonar.java.binaries, sonar.java.test.binaries and sonar.java.test.libraries with Scanner for Maven will also be set automatically
If needed you'll still be able to provide other additional properties
via command line such as mvn ... sonar:sonar -Dsonar.something=something
or in SonarQube UI
or specify them directly in properties section of pom.xml
How to create jacoco-it.exec and jacoco-ut.exec and run it on SonarScanner?
Usually jacoco-ut.exec refers to coverage data about unit tests and jacoco-it.exec refers to coverage data about integration tests. They are created by jacoco-maven-plugin, in your pom.xml you configured jacoco-maven-plugin for creation of jacoco.exec.

Feed sonarqube jacoco widget with csv/html reports (instead of *.exec)

I got a root maven project, under which there are many independent modules (e.g. module_A, module_B, etc.).
One of these module is my integration-tests module, and it uses all the above external modules.
In order to have code coverage for all modules used by integration-tests, I use a workaround based on maven-ant (see this blog post).
Problem is:
The above generates csv/html report, yet sonarqube jacoco widget parses only jacoco*.exec files - which results in 0% code coverage.
Question is:
EDIT
here's an example project for the problem above.
You don't need to use that workaround. You can provide Sonar with integration tests coverage file with following property (you shall use it while executing sonar:sonar goal):
-Dsonar.jacoco.itReportPath=<coverage_file>
Here is detailed documentation:
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Integration+Tests+for+Java+Project
To sum up:
Compile all your modules.
Execute integration tests with jacoco enabled.
Execute Maven Sonar build adding mentioned property in command line.
I have prepared example project generating both unit and integration coverage results, you can check it here:
https://github.com/octosan/unit-and-integration-jacoco-coverage-with-sonar
You have to:
download newest Sonarqube version and start it
execute command:
mvn clean install sonar:sonar -Dsonar.jacoco.itReportPath=<absolute_path>/itest/target/jacoco-it.exec
add integration coverage widged in project dashboard in Sonar

How can i give extra parameters in Jacoco, Maven?

I have a project with multi modules and i have coverage report. I am running with this command:
mvn org.jacoco:jacoco-maven-plugin:prepare-agent clean install -Pcoverage -DfailIfNoTests=false -Dmaven.test.failurignore=true
But due to multi module, i guess i have to give extra arguments like destfile.
How can i do this? For now, some reports are not shown in sonar.
Why not include the jacoco plugin details in your pom file.
See This question for a link to all configuration options for Jacoco.
How to add JaCoCo in maven

SONAR importing cobertura.ser coverage reports to sonar server using mvn sonar:sonar?

I have a cobertura.ser file, that got generated while integration-test and system test. Now I want to import my coverage this to sonar server.
How can I achieve this, so while executing mvn sonar:sonar the coverage should consider external coberture.ser file?
Can I do this using sonar, where i can see overall coverage obtain during all test run?
You need to set the following paramater to tell Sonar to use a pre-generated report:
sonar.dynamicAnalysis=reuseReports
Secondly Sonar doesn't read the "cobertura.ser" file. It can be configured as follows to read the generated XML report (See cobertura docs):
sonar.cobertura.reportPath=target/reports/coverage.xml
Finally, my reference for all this stuff comes from the from the Sonar wiki:
http://docs.codehaus.org/display/SONAR/Advanced+Parameters
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Integration+Tests
The Sonarsource tutorials are increasingly pushing JaCoCo. This appears to be an emerging standard (replacing the older defunct Emma project)

Sonar not picking up clover test results created by running maven in jenkins

I've been working on this all day and haven't been able to find a solution, so thought i'd turn to the stackoverflow community.
I have a server setup with Jenkins (version 1.454). It's configured to perform the build with Maven (version 3.0.4). The build command is as follows:
clean clover2:setup verify clover2:aggregate clover2:clover
Jenkins has the sonar plugin installed and configured. My sonar install (version 3) is on a different server to jenkins. After the build is completed, i can see the results on sonar (the basics of the build).
I have installed to sonar the clover plugin (sonar-clover-plugin-2.8.jar) manually by putting it into the 'extentions/plugins' folder. It appeared in sonar and I can configure it. I put in the license, version (3.1.4) and sonar.clover.reportPath as "target\site\clover\clover.xml".
When I run the build, it is successful as before, and I can see the basic results as before in Sonar, however, I cannot see any test code coverage results.
When I look at the maven build output I notice the following:
[INFO] Writing report to '/opt/jenkins/jobs/foo/workspace/target/site/clover/clover.xml'
and then near the bottom of the output:
[INFO] [15:08:36.586] Clover XML report not found
The following are also in my pom:
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.clover.reportPath>target\site\clover\clover.xml</sonar.clover.reportPath>
<sonar.surefire.reportsPath>target\surefire-reports</sonar.surefire.reportsPath>
<sonar.core.codeCoveragePlugin>clover</sonar.core.codeCoveragePlugin>
<sonar.clover.version>${clover.version}</sonar.clover.version>
When I look on the jenkins box, I can see that it has generated the clover.xml file (infact in my fiddling I have the clover plugin working on jenkins, so i can see the results there!).
I'm guessing at this stage that there is some issue with jenkins and sonar being on seperate boxes.
Any help would be appreciated!
Cheers.
After a lot more fiddling around, it appeared that I needed to change the following:
<sonar.clover.reportPath>${project.build.directory}/site/clover/clover.xml</sonar.clover.reportPath>
<sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
A good reference was the following:
all about pom variables

Resources