This project imports both:
- JaCoCo's aggregate XML report to enable code coverage reporting across modules
- Maven Surefire XML test reports to include unit test results for each module
For a basic example, see basic Maven project.
Build the project, execute all the tests, and analyze it with SonarScanner for Maven:
mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:5.6.0.6792:sonar \
-Dsonar.coverage.jacoco.xmlReportPaths=$(pwd)/tests/target/site/jacoco-aggregate/jacoco.xml \
-Dsonar.junit.reportPaths=target/surefire-reportsThis project consists of 3 modules:
-
module1andmodule2contain "business logic" and related unit tests. -
testsmodule contains integration tests which test functionality using both modules.testsmodule is also the one which creates the aggregate coverage report imported into SonarQube.
To collect code coverage across all modules:
In the top-level pom.xml, the JaCoCo plugin is configured under so it applies to all submodules. It is wrapped in the coverage profile to make it activatable (e.g. in CI):
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
In the tests module, the report-aggregate goal is configured to run in the verify phase to generate a coverage report combining all modules:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report</id>
<goals>
<goal>report-aggregate</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
This creates the report at:
tests/target/site/jacoco-aggregate/jacoco.xml
To import it into SonarQube, set the path in the top-level pom.xml:
<properties>
<sonar.coverage.jacoco.xmlReportPaths>${maven.multiModuleProjectDirectory}/tests/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
Or pass it directly via the command line:
-Dsonar..coverage.jacoco.xmlReportPaths=absolute/path/to/jacoco.xml
To include unit test results (e.g. passed/failed/skipped test count, execution time) in SonarQube:
The maven-surefire-report-plugin (version 3.5.3) is included in the root pom.xml under , ensuring all modules inherit it:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.5.3</version>
</plugin>
</plugins>
</pluginManagement>
During the verify phase, this generates XML reports in each module's target/surefire-reports/ directory:
module1/target/surefire-reports/TEST-com.example.FooTest.xml
module2/target/surefire-reports/TEST-com.example.BarTest.xml
To import these into SonarQube, set:
-Dsonar.junit.reportPaths=target/surefire-reports
After analysis, SonarQube will display:
- Code coverage metrics from jacoco.xml
- Test execution stats from Surefire XML files:
- Total tests
- Passed/failed/skipped
- Execution duration
- Trends and drill-down under the "Tests" and "Coverage" tabs