I am using JaCoCo code coverage, but the report is including classes from jar, lib. (Offline Instrumentation, Maven)
I solved the problem with the offline configuration since "aspectj-maven-plugin" was changing the class files, and also now I successfully exclude the packages outside of target/classes
-> src
. thanks to this answer in stackoverflow.
But now I am getting the classes from jar, lib inside the report and I have not idea how to exclude then. I Show my configuration and examples below
I also tried this solution Exclude classes of jar files from jacoco coverage report But it doesn't work for me.
<exclude>**/lib/*</exclude>
My jacoco offline configuration:
<properties>
<jacoco.version>0.8.4</jacoco.version>
<argLine></argLine>
</properties>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
</dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<!-- this configuration affects all goals -->
<excludes>
<exclude>*</exclude>
<exclude>com/company/rrPackage/**/*.class</exclude>
<exclude>org/**/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<testNGArtifactName>...</testNGArtifactName>
<suiteXmlFiles>
<suiteXmlFile>...</suiteXmlFile>
</suiteXmlFiles>
<skip>${skip.test}</skip>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<properties>
...
</properties>
</configuration>
</plugin>
And the reason what I think that I am getting classes from jar inside de jacoco:report. In my pom.xml I have the following dependencies:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
Also I have a couple of import in my classes like this
import org.hsqldb.lib.StringUtil;
for example: This has no dependency on the pom.xml but is used in one of the project classes, and jacoco shows it in the report
import javax.mail.internet.AddressException;
I have other cases with the same behavior that result in the same problem: Jacoco show those classes from jar in the report, as shown in the images
Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.
One of the main benefits of JaCoCo is the Java agent, which instruments classes on-the-fly. This simplifies code coverage analysis a lot as no pre-instrumentation and classpath tweaking is required.
Try includes
instead of excludes
. Notice that you need .class at the end.
try something like that:
<configuration>
<includes>
<include>com/company/package/**/*.class</include>
</includes>
</configuration>
Base on your example:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<!-- this configuration affects all goals -->
<includes>
<include>com/company/packageToInclude/**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I don't know how you generate lib
directory, because you don't provide complete example.
However in case of the following example
src/main/java/Example.java
class Example {
}
src/test/java/ExampleTest.java
public class ExampleTest {
@org.junit.Test
public void test() {
new Example();
}
}
and pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<jacoco.version>0.8.4</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>junit</includeArtifactIds>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
execution of mvn clean verify
produces
$ ls -R target/classes
target/classes:
Example.class lib
target/classes/lib:
junit-4.12.jar
and following report
And after addition of following <configuration>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>lib/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
execution of the same command mvn clean verify
produces following report
If the above doesn't help, then please provide absolutely complete example allowing everybody else to reproduce exactly the same what you do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With