How to exclude and include properly classes, packages and jar classes, lib from the jacoco report (Instrumentation offline)

Didier Roque Ginebra :

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

enter image description here enter image description here

Lisandra Gonzalez :

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305670&siteId=1