Incomplete Jacoco code Coverage in multimodule maven project

Must-share information (formatted with Markdown):

  • which versions are you using: Sonarqube 8.5.1, Azure DevOps Server 2020
  • what are you trying to achieve: Code coverage for multimodule JAVA/Maven project
  • what have you tried so far to achieve this: Tried using both classi and YAML pipelines but only the 2nd module’s coverage is being reported in SQ.

Hi Everyone.

We’ve had multiple issues getting Sonarqube to report Code Coverage from, our Azure DevOps build pipelines due to the MAven build step not being updated and discrepancies with the reportpaths/XMLReportPaths properties etc…

Having updated to Azure DevOps Server 2020 and compiling the latest version of the Maven Task (v3) (so we can select the XMLReportPath specifically) we’ve overcome these issues for the most part. Simple(single) module builds are working as expected.

However, multimodule builds only seem to be showing Code Coverage for the last module instead of overall.

My YAML pipeline looks like this…

> steps:
> 
> - task: SonarQubePrepare@4
> 
>   inputs:
> 
>     SonarQube: 'Sonar'
> 
>     scannerMode: 'Other'
> 
> - task: MavenEngie@3
> 
>   inputs:
> 
>     mavenPomFile: 'pom.xml'
> 
>     goals: 'clean package'
> 
>     options: '-Dmaven.test.failure.ignore=true -Drevision=$(Build.BuildNumber) -Dsonar.branch.name=$(Build.SourceBranchName) -Dsonar.projectKey=com.alm.demo.java -Dsonar.projectName="ALM Demo Java"  -Dsonar.links.scm=$(Build.Repository.Uri)  -X'
> 
>     publishJUnitResults: true
> 
>     testResultsFiles: '**/surefire-reports/TEST-*.xml'
> 
>     codeCoverageToolOption: 'JaCoCo'
> 
>     javaHomeOption: 'JDKVersion'
> 
>     jdkVersionOption: '1.11'
> 
>     mavenVersionOption: 'Default'
> 
>     mavenAuthenticateFeed: false
> 
>     effectivePomSkip: false
> 
>     sonarQubeRunAnalysis: true
> 
>     isJacocoCoverageReportXML: true
> 
>     sqMavenPluginVersionChoice: 'latest'
> 
> - task: SonarQubePublish@4
> 
>   inputs:
> 
>     pollingTimeoutSec: '300'
> 
>     
> 
> - task: PublishCodeCoverageResults@1
> 
>   displayName: 'Publish code coverage results'
> 
>   inputs: 
> 
>     codeCoverageTool: 'JaCoCo' 
> 
>     summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/jacoco.xml' 
> 
>     pathToSources: '$(build.sourcesdirectory)/'      
> 
>     additionalCodeCoverageFiles: '$(build.sourcesdirectory)/target/jacoco.exec'

Pom.xml (relevant sections)

> <build>
> 
>     <pluginManagement>
> 
>       <plugins>
> 
>         <plugin>
> 
>           <groupId>org.apache.maven.plugins</groupId>
> 
>           <artifactId>maven-compiler-plugin</artifactId>
> 
>           <version>3.8.1</version>
> 
>         </plugin>
> 
>         <plugin>
> 
>           <groupId>org.sonarsource.scanner.maven</groupId>
> 
>           <artifactId>sonar-maven-plugin</artifactId>
> 
>           <version>3.6.0.1398</version>
> 
>         </plugin>
> 
>         <plugin>
> 
>           <groupId>org.jacoco</groupId>
> 
>           <artifactId>jacoco-maven-plugin</artifactId>
> 
>           <version>0.8.6</version>
> 
>         </plugin>
> 
>       </plugins>
> 
>     </pluginManagement>
> 
>   </build>
> 
>  <profiles>
> 
>     <profile>
> 
>       <id>coverage</id>
> 
>       <activation>
> 
>         <activeByDefault>true</activeByDefault>
> 
>       </activation>
> 
>       <build>
> 
>          <plugins>
> 
>           <plugin>
> 
>           <groupId>org.jacoco</groupId>
> 
>           <artifactId>jacoco-maven-plugin</artifactId>
> 
>           <version>0.8.6</version>
> 
>           <executions>
> 
>             <execution>
> 
>             <id>prepare-agent</id>
> 
>             <goals>
> 
>               <goal>prepare-agent</goal>
> 
>             </goals>
> 
>             </execution>
> 
>             <execution>
> 
>             <id>report</id>
> 
>             <goals>
> 
>               <goal>report</goal>
> 
>               <goal>report-aggregate</goal>
> 
>           </goals>
> 
>           <phase>verify</phase>
> 
>           </execution>            
> 
>           </executions>
> 
>           </plugin>
> 
>         </plugins>
> 
>       </build>
> 
>     </profile>
> 
>   </profiles>

I can provide a build log in private if necessary (required due to company policy)…

Any assistance is appreciated.

Kind Regards,
DB

2 Likes

Hi @DevonBritton

Working with multi module is a bit special.

What we usually do in our sample is to provide, in each module’s pom, the sonar.coverage.jacoco.xmlReportPaths with the link of either the aggregated XML report file, or each one of the related module.

Also, if you are using your own jacoco plugin declaration in your parent pom or test one, please make sure to desactivate the Coverage tool set in the Maven task (set it to None, instead of JaCoCo) this will ensure that only the one you declare will be run and the maven task will not set its own coverage properties as well.

HTH,

1 Like

Hello everyone,

I hope you’re all having a good day.

I’m continuing my long struggle, which is very similar to the one reported by Devon. My team does not have admin access to Jenkins to change the coverage tool set in the Maven task to None instead of JaCoCo. The module where the integration tests run has the expected coverage (similar to example module2), but the coverage on other modules (module1) is 0%.

I’ve reached the point where I dump all existing jacoco.xml files (one of them being the report-aggregate for the test module) which contain classes with the expected coverage data. However, there is a jacoco.xml for each module, and the coverage in these files is 0%. All of them get uploaded to SonarQube, but the coverage still shows as 0%.

Could SonarQube attempt to merge the data from the multiple jacoco.xml files? At least in cases where one file shows 0% coverage and another shows 90%, it could use the higher coverage and display a warning.

(Our SonarQube version is Data Center Edition Version 9.9.3 (build 79811))

Thank you to anyone who can provide help or suggestions for improvement. I’m doing the best I can, and I challenge you to do the same.
:vulcan_salute: