80

I am having issues with sonar picking up the jacoco analysis report. Jenkins however is able to pick up the report and display the results. My project is a maven build, built by Jenkins. The jacoco report is generated by maven (configured in the pom). Sonar is executed by using the Jenkins plugin.

This is what I see on SonarQube:

SonarQube Screen Capture

This is the report i can see of the project in jenkins.

Jacoco report in Jenkins

The maven plugin config:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.4.201312101107</version>
    <executions>
      <execution>
        <id>default-prepare-agent</id>
        <goals>
            <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>default-report</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>report</goal>
        </goals>
      </execution>
      <execution>
          <id>default-check</id>
          <goals>
              <goal>check</goal>
          </goals>
      </execution>
    </executions>
</plugin>

Jenkins Sonar Plugin config enter image description here

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
anton91
  • 993
  • 2
  • 10
  • 12
  • 1
    Somewhere in your console output should say, "Code coverage set to 0%...", That information would be useful to debug your issue. Two things come to mind without looking at it though: 1. You should be setting sonar.binaries property and point it to your compiled class files 2. You should set sonar.tests and point it to your uncompiled test files. 3. You should compile in debug mode (you might be already I can't tell) – Cole9350 Mar 04 '14 at 14:47
  • @Cole9350 so the error i see in the output is: _Project coverage is set to 0% since there is no directories with classes._ I have tried setting sonar.binaries to the root of the classes directory - /target/classes - but this resulted in an error saying they could not be found – anton91 Mar 05 '14 at 09:06
  • **correction** I tried again and set the fully qualified path to the classes and i am now able to see code coverage. However i know see the error when the jacoco sensor runs: No information about coverage per test. – anton91 Mar 05 '14 at 09:21
  • Good! You're almost there. The first error was indicating you hadn't set sonar.binaries. Now your error is telling you to set sonar.tests Like I mention in (2)... After that I'm pretty sure sonar will pick up the coverage – Cole9350 Mar 05 '14 at 14:40
  • @Cole9350 : I am also facing the same problem after i upgraded sonar from 4.0 to 4.5.1. Earlier it used to generate code coverage, but after upgrade there is no info on code coverage, When i have gone through the console output , i see thta jacoco.exec files are generated under target folder but Cobertura sensor was listed instead of jacocoSensor for analysing Codecoverage. – sai Jan 12 '16 at 07:19

10 Answers10

82

You were missing a few important sonar properties, Here is a sample from one of my builds:

sonar.jdbc.dialect=mssql
sonar.projectKey=projectname
sonar.projectName=Project Name
sonar.projectVersion=1.0
sonar.sources=src
sonar.language=java
sonar.binaries=build/classes
sonar.tests=junit
sonar.dynamicAnalysis=reuseReports
sonar.junit.reportsPath=build/test-reports
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=build/test-reports/jacoco.exec

The error in Jenkins console output can be pretty useful for getting code coverage to work.

Project coverage is set to 0% since there is no directories with classes.
Indicates that you have not set the Sonar.Binaries property correctly

No information about coverage per test
Indicates you have not set the Sonar.Tests property properly

Coverage information was not collected. Perhaps you forget to include debug information into compiled classes? Indicates that the sonar.binaries property was set correctly, but those files were not compiled in debug mode, and they need to be

Cole9350
  • 5,444
  • 2
  • 34
  • 50
  • 5
    The settings are not all required (for examle dynamicAnalysis is deprecated). I upvoted this for the binaries property, i've set this wrong. – user1226868 May 20 '14 at 07:57
  • Hi, I have same configuration for Android-Kotlin, build generating successfully and except code coverage everything is on place. Any suggestion what could be miss. FYI: I'm using exact same config you mentioned. – CoDe Feb 09 '18 at 09:20
  • @CoDe how did you solve it? I have similar problem but with Kotlin files - https://stackoverflow.com/q/70151110/8762338 – Astha Garg Nov 29 '21 at 08:15
  • It's been long and hard to recall. Sorry ! But may be if you have specific query I can try. – CoDe Nov 30 '21 at 04:30
24

Based on https://github.com/SonarSource/sonar-examples/blob/master/projects/tycho/pom.xml, the following POM works for me:

<properties>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.0.201403182114</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <destFile>${sonar.jacoco.reportPath}</destFile>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Setting the destination file to the report path ensures that Sonar reads exactly the file JaCoCo generates.
  • The report path should be outside the projects' directories to take cross-project coverage into account (e.g. in case of Tycho where the convention is to have separate projects for tests).
  • The reuseReports setting prevents the deletion of the JaCoCo report file before it is read! (Since 4.3, this is the default and is deprecated.)

Then I just run

mvn clean install
mvn sonar:sonar
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
thSoft
  • 21,755
  • 5
  • 88
  • 103
10

I had the similar issue, 0.0% coverage & no unit tests count on Sonar dashboard with SonarQube 6.7.2: Maven : 3.5.2, Java : 1.8, Jacoco : Worked with 7.0/7.9/8.0, OS : Windows

After a lot of struggle finding for correct solution on maven multi-module project,not like single module project here we need to say to pick jacoco reports from individual modules & merge to one report,So resolved issue with this configuration as my parent pom looks like:

 <properties>
            <!--Sonar -->
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
            <sonar.language>java</sonar.language>

        </properties>

        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <source>1.5</source>
                            <target>1.5</target>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.sonarsource.scanner.maven</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>3.4.0.905</version>
                    </plugin>

                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.9</version>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <append>true</append>
                        </configuration>
                        <executions>
                            <execution>
                                <id>agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                </plugins>
            </pluginManagement>
        </build>

I've tried few other options like jacoco-aggregate & even creating a sub-module by including that in parent pom but nothing really worked & this is simple. I see in logs <sonar.jacoco.reportPath> is deprecated,but still works as is and seems like auto replaced on execution or can be manually updated to <sonar.jacoco.reportPaths> or latest. Once after doing setup in cmd start with mvn clean install then mvn org.jacoco:jacoco-maven-plugin:prepare-agent install (Check on project's target folder whether jacoco.exec is created) & then do mvn sonar:sonar , this is what I've tried please let me know if some other best possible solution available.Hope this helps!! If not please post your question..

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
2

Jenkins does not show coverage results as it is a problem of version compatibilities between jenkins jacoco plugin and maven jacoco plugin. On my side I have fixed it by using a more recent version of maven jacoco plugin

<build>
   <pluginManagement>
     <plugins>
       <plugin>
         <groupId>org.jacoco</groupId>
         <artifactId>jacoco-maven-plugin</artifactId>
         <version>0.7.9</version>
       </plugin>
     <plugins>
   <pluginManagement>
<build>
2

I was facing the same problem and the challenge in my case was to configure Jacoco correctly and to configure the right parameters for Sonar. I will briefly explain, how I finally got SonarQube to display the test results and test coverage correctly.

In your project you need the Jacoco plugin in your pom or parent pom (you already got this). Moreover, you need the maven-surefire-plugin, which is used to display test results. All test reports are automatically generated when you run the maven build. The tricky part is to find the right parameters for Sonar. Not all parameters seem to work with regular expressions and you have to use a comma separated list for those (documentation is not really good in my opinion). Here is the list of parameters I have used (I used them from Bamboo, you might omit the "-D" if you use a sonar.properties file):

-Dsonar.branch.target=master (in newer version of SQ I had to remove this, so that master branch is analyzed correctly; I used auto branch checkbox in bamboo instead)
-Dsonar.working.directory=./target/sonar
-Dsonar.java.binaries=**/target/classes
-Dsonar.sources=./service-a/src,./service-b/src,./service-c/src,[..]
-Dsonar.exclusions=**/data/dto/**
-Dsonar.tests=.
-Dsonar.test.inclusions=**/*Test.java [-> all your tests have to end with "Test"]
-Dsonar.junit.reportPaths=./service-a/target/surefire-reports,./service-b/target/surefire-reports,
./service-c/target/surefire-reports,[..]
-Dsonar.jacoco.reportPaths=./service-a/target/jacoco.exec,./service-b/target/jacoco.exec,
./service-c/target/jacoco.exec,[..]
-Dsonar.projectVersion=${bamboo.buildNumber}
-Dsonar.coverage.exclusions=**/src/test/**,**/common/**
-Dsonar.cpd.exclusions=**/*Dto.java,**/*Entity.java,**/common/**

If you are using Lombok in your project, than you also need a lombok.config file to get the correct code coverage. The lombok.config file is located in the root directory of your project with the following content:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
maxeh
  • 1,373
  • 1
  • 15
  • 24
0

Include the sunfire and jacoco plugins in the pom.xml and Run the maven command as given below.

mvn jacoco:prepare-agent jacoco:report sonar:sonar

<properties>
    <surefire.version>2.17</surefire.version>
    <jacoco.version>0.7.2.201409121644</jacoco.version>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.version}</version>
        </plugin> 

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>

            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals><goal>prepare-agent</goal></goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals><goal>report</goal></goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
Raghav Mo
  • 1
  • 1
0

The presence of argLine configurations in either of surefire and jacoco plugins stops the jacoco report generation. The argLine should be defined in properties

<properties>
  <argLine>your jvm options here</argLine>
</properties>
Harish Kumar
  • 316
  • 3
  • 11
0

For me percentage coverage was not appearing in the sonarqube dashboard. I have added below property in our pom.xml to work.

<sonar.binaries>${project.basedir}/../target/classes</sonar.binaries>
SudhirKumar
  • 366
  • 2
  • 4
  • 16
0

Make sure that Sonarqube can find your test coverage file. As somebody already mentioned the Sonarqube output should really be helpful here. e.g.

WARN: No coverage information will be saved because LCOV file cannot be found.
jahller
  • 2,705
  • 1
  • 28
  • 30
0

I will describe my experience in solving problem with test-coverage in SonarQube. First of all, Sonar analyzes your test-coverage when you use JaCoCo plugin. without it, you will not get coverage-info in sonar dashboard. So, first configure it, after that add to you sonar config these params:

sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=./project_root_folder/module1/target/site/jacoco/jacoco.xml, /project_root_folder/module2/target/site/jacoco/jacoco.xml
sonar.coverage.jacoco.xmlReportPaths=./project_root_folder/module1/target/site/jacoco/jacoco.xml, ./project_root_folder/module2/target/site/jacoco/jacoco.xml,

sonar.tests=./project_root_folder/module1/src/test, ./project_root_folder/module2/src/test

pay ATTENTION to that path to jacoco report in your target branch!(for multimodule projects use coma to divide paths) also keep in mind one moment - maybe you will need to make mavn clean install before sonar scanning in order give sonar latest jacoco reports.

Sam Fisher
  • 746
  • 2
  • 10
  • 27