We have a multi-module multi-language maven java project with coverage analysis with jacoco. The main part of the modules is backend (Java code) with a REST API and our webapp m
I had the same experience when I was configuring JaCoCo for Jersey. I ended up applying the bisection method in order to find out which modules are causing the code coverage for the whole project to drop to 0%.
If I remember it correctly, the biggest surprise was maven-shade-plugin
which somehow corrupted the association between the collected coverage and the location of classes which caused Sonar to show 0% code coverage. I ended up disabling its execution by binding it to phase none
in order to fix it (see this commit).
As an example, this is how to extend a pom.xml
of a sub-module in order to have working sonar (assuming, sonar is executed with sonar
maven profile).
<profile>
<id>sonar</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- disable parallel execution so that JaCoCo listener can properly work -->
<parallel>none</parallel>
<perCoreThreadCount>false</perCoreThreadCount>
<forkCount>1</forkCount>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade-archive</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-debug-all</artifactId>
<optional>false</optional>
</dependency>
</dependencies>
</profile>
I use JUnit and in my case the issue was because of having TestNG dependency in my pom.xml in addition to JUnit. After removing this unnecessary dependency, everything started to work as expected.
Considering other answers I want do add one thing, which is important to show test coverage on Sonar:
<includes>
or similar configurations.After fighting very long time with Jacoco and Maven only this way it is working for me:
Create profile in pom.xml
<profile>
<id>coverage-per-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- disable parallel execution so that JaCoCo listener can properly work -->
<parallel>none</parallel>
<perCoreThreadCount>false</perCoreThreadCount>
<forkCount>1</forkCount>
<properties>
<property>
<name>listener</name>
<value>org.sonar.java.jacoco.JUnitListener</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.sonarsource.java</groupId>
<artifactId>sonar-jacoco-listeners</artifactId>
<version>3.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
Update maven surefire plugin to latest version
Execute commands:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test -Dmaven.test.failure.ignore=true
mvn sonar:sonar
Similar as describe in github sonar example
Im using a similar setup and everything works perfectly with the following configuration:
However the maven-surefire-plugin
was causing troubles initially. Thats why I had to add the @{argLine}
in the argLine tag, since the jacoco-maven-plugin:prepare-agent
goal is executed before the maven-surefire-plugin
.
(see https://stackoverflow.com/a/25774988)
Hope I could help
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
</execution>
</executions>
<configuration>
<!-- the @{argLine} is needed for proper jacoco execution -->
<argLine>@{argLine} -Xmx256m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- generate fancy html report -->
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>