We have a multi module Maven project and using JaCoCo for code coverage analysis. I prepared the pom
file to run the test cases and pick up the code coverage.
Below is my pom.xml
with JaCoCo and other plugins added and in Azure Devops build am passing the command clean install sonar:sonar
, tried clean test sonar:sonar
in the Maven task with the argument -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-ut
defined in the pom.xml
.
I see below details in the logs.
Maven logs:
Analysis completion logs:
Test runs:
Generating Jacoco report:
I have looked into various blogs and posts, tried many ways but couldn't see code coverage in Sonar.
<plugins>
<!--Jacoco Maven Plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7-SNAPSHOT</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Maven ant plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- Maven Surefire Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<!-- Excludes integration tests when unit tests are run. -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- Sonar Maven Plugin -->
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
Sharing my Maven Task YAML configuration
task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
goals: 'clean verify sonar:sonar'
options: '-Dsonar.projectKey=** -Dsonar.organization=**
-Dsonar.host.url=https://sonarcloud.io -Dsonar.login=**
-Dsonar.java.binaries=$(build.sourcesdirectory)/**/src/test/java/
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate/jacoco.xml
-Dsonar.exclusions=/jaxb/**/*'
publishJUnitResults: false
codeCoverageToolOption: 'JaCoCo'
codeCoverageClassFilesDirectories: 'core/target/classes,/core/target/test-classes'
codeCoverageFailIfEmpty: true
javaHomeOption: 'Path'
jdkDirectory: '/opt/jdk-11.0.2'
mavenVersionOption: 'Path'
mavenDirectory: '/usr/local/apache-maven/apache-maven-3.6.3/'
mavenSetM2Home: false
mavenOptions: '-Xmx3072m'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: true
isJacocoCoverageReportXML: true
sqMavenPluginVersionChoice: 'pom'
After few changes i can see Jacoco report is picked up, but unable to see the Code Coverage
Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
Updating logs
main: [echo] Generating JaCoCo Reports
[report] Loading execution data file /home/AzureUser/adoagent/_work/2/s/CCReport43F6D5EF/jacoco.exec
[report] Writing bundle 'Jacoco report' with 338 classes
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.743 s