Sonarqube not generating CodeCoverage report for both Jacoco or Cobertura

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • what are you trying to achieve
  • what have you tried so far to achieve this

Hi
We are using Sonarqube 8.3.1 community edition in Azure Devops
We have integrated Sonarqube with Azure devops with sonar extension SonarQube - Visual Studio Marketplace
Java 11 for Sonar 8.3.1
Here we are running sonarqube for Maven project. We got the report but codecoverage is zero. SO for that we have enabled for Jacoco and also Cobertura coverage report. But not reflecting in Sonar server. We have individually tested for both plugins.

We have enabled Jacoco plugin Azure Pipeline Yaml code as below

Jacoco Code PIpeline yaml:
- task: Maven@3
            timeoutInMinutes: 100
            inputs:
              mavenPomFile: 'cq-webapp\pom.xml'
              goals: '-T 4 clean install -Puber-package -f "cq-webapp" -s "cq-webapp/settings.xml"'
              publishJUnitResults: true
              testResultsFiles: '**/surefire-reports/TEST-*.xml'
              codeCoverageToolOption: 'JaCoCo'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: '1.8'
              mavenVersionOption: 'Path'
              mavenDirectory: 'C:/Softwares/apache-maven-3.6.3-bin/apache-maven-3.6.3/'
              mavenSetM2Home: true
              mavenOptions: '-Xmx3072m'
              mavenAuthenticateFeed: false
              effectivePomSkip: false
              sonarQubeRunAnalysis: true
              isJacocoCoverageReportXML: true
              sqMavenPluginVersionChoice: 'latest'

also find the pom.xml for Jacoco is

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format>
        <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>
		...... <!-- Some other steps --->
		
	</properties>	
### Jacoco Plugin
	In Build added this
            <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.3</version>
    <configuration>
        
        <dataFile>target/jacoco.exec</dataFile>
        <outputDirectory>target/myreport</outputDirectory>
        <excludes>
            <exclude>*MethodAccess</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
		<execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

### ANTRUN plugin
            <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        </plugin>

For Jacoco we are getting one some code coverage like 2%. and pipeline is failing with error like
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) on project VstsReport: An Ant BuildException has occured: Unable to read execution data file C:\azagent\A2_work\1\s\cq-webapp\CCReport43F6D5EF\jacoco.exec

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) on project VstsReport: An Ant BuildException has occured: Unable to read execution data file C:\azagent\A2_work\1\s\cq-webapp\CCReport43F6D5EF\jacoco.exec
[ERROR] around Ant part …… @ 8:11 in C:\azagent\A2_work\1\s\cq-webapp\target\antrun\build-main.xml: C:\azagent\A2_work\1\s\cq-webapp\CCReport43F6D5EF\jacoco.exec (The system cannot find the path specified)

###Cobertura code in pipeline
          - task: Maven@3
            timeoutInMinutes: 100
            inputs:
              mavenPomFile: 'cq-webapp\pom.xml'
              goals: '-T 4 clean install -Puber-package -f "cq-webapp" -s "cq-webapp/settings.xml"'
              publishJUnitResults: true
              testResultsFiles: '**/surefire-reports/TEST-*.xml'
              codeCoverageToolOption: 'Cobertura'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: '1.8'
              mavenVersionOption: 'Default'
              mavenOptions: '-Xmx3072m'
              mavenAuthenticateFeed: false
              effectivePomSkip: false
              sonarQubeRunAnalysis: true
              sqMavenPluginVersionChoice: 'latest'

Issue is in Cobertura we are getting the report in pipeline. But not reflecting in Sonarqube server.

Cobertura Pom.xml

Properties:

<sonar.core.codeCoveragePlugin>cobertura</sonar.core.codeCoveragePlugin>
    <sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin>
    <sonar.junit.reportsPath>${project.basedir}/target/surefire-reports</sonar.junit.reportsPath>
    <sonar.surefire.reportsPath>${project.basedir}/target/surefire-reports</sonar.surefire.reportsPath>
   <sonar.cobertura.reportPath>${project.basedir}/target/cobertura/cobertura.ser</sonar.cobertura.reportPath>
<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <instrumentation>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </instrumentation>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <id>clean</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>instrument</id>
                    <phase>site</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>cobertura</goal>
                    </goals>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Please tell how to get codecoverage in sonarqube server with any of the method.

Hi @Raja_Shekar ,

Welcome to SonarSource Community! :sonarsource:

Cobertura is not an accepted report for Java coverage based on our Test Coverage and Execution documentation. Please see [Coverage & Test Data] Importing JaCoCo coverage report in XML format for how to get Jacoco XML reports to work.

That means we cant use Cobertura report in sonarqube for Maven only Jacoco is supported. In that case I have used all the Jacoco method but not working. Getting error as mentioned above.
If possible can you help in steps to add in Maven azure task for not getting the error

Hi @Raja_Shekar ,

You need to use .xml format, not .exec format. See my earlier link: [Coverage & Test Data] Importing JaCoCo coverage report in XML format

Here’s an example of a simple maven single-module project with successful code coverage (sample projects here): https://dev.azure.com/joetingsanchali/_git/azo-maven-basic?path=%2Fazure-pipelines.yml

I give a simplified pipeline yaml here:

trigger:
- master

pool:
  vmImage: ubuntu-latest

steps:
- task: SonarQubePrepare@4
  inputs:
    SonarQube: 'ee89'
    scannerMode: 'Other'
    extraProperties: |
      sonar.coverage.jacoco.xmlReportPaths=**/jacoco.xml
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'clean install sonar:sonar'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    codeCoverageToolOption: 'JaCoCo'
    javaHomeOption: 'JDKVersion'
    mavenVersionOption: 'Default'
    mavenAuthenticateFeed: false
    effectivePomSkip: false
    sonarQubeRunAnalysis: true
    sqMavenPluginVersionChoice: 'latest'
- task: SonarQubePublish@4
  inputs:
    pollingTimeoutSec: '300'

Hi @Joe
Still facing the issue with Jacoco plugin. Maven Build is getting failed. Error is
Failed to execute goal org.apache.maven.plugins: maven-antrun-plugin:1.8:run (default) on project VstsReport: An Ant BuildException has occured: Error while creating report
**[ERROR] around Ant part …… @ 8:11 in C:\azagent\A2_work\1\s\cq-webapp\target\antrun\build-main.xml: **
Error while analyzing test-webapp-services\target\test-webapp-services-3.0.1.jar@OSGI-INF/lib/commons-logging-1.1.jar@org/apache/commons/logging/Log.class. Can’t add different class with same name: org/apache/commons/logging/Log

If it fails also getting some part of the coverage in sonar qube. Actually 18%. But here it is showing 2.7%. How to get complete coverage.

Hi @Raja_Shekar ,

That error seems to be something wrong with your Maven project, see this SO answer: JaCoCo Can't add different class with same name: org/hamcrest/BaseDescription - Stack Overflow

Please post your console logs when running the maven command as an attachment in a .txt file if you still encounter a failure. Please append -X to mvn command to get DEBUG logs.

Joe

My maven logs. If i uses -X the file size becomes too large of 60mb
aemmaven logs.zip (580.8 KB)

Can you split your logs with split tool? Split Command in Linux with Examples - GeeksforGeeks

I just need to see the part that starts the Sonar scanner analysis.

Sorry that file log file has been deleted. Also we kept the process on hold for this pipeline. Once we get approval for rerun then i will reshare the logs. Thanks

1 Like