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

1 Like

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,