如何从 jacoco 报告中正确排除和包含 类、包和 jar 类、lib(Instrumentation offline)

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

我正在使用 JaCoCo 代码覆盖率,但报告包括来自 jar、lib 的 classes。 (离线检测,Maven)

我解决了离线配置的问题,因为“aspectj-maven-plugin”正在更改 class 文件,现在我也成功排除了 target/classes 之外的包 -> src。感谢 中的这个答案。 但是现在我正在从报告中的 jar、lib 中获取 classes,然后我不知道如何排除。我在下面显示我的配置和示例

我也试过这个解决方案但是它对我不起作用。 <exclude>**/lib/*</exclude>

我的jacoco离线配置:

<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>
                <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>

以及我认为我从 jacoco:report 中的 jar 中得到 classes 的原因。在我的 pom.xml 中,我有以下依赖项:

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.2.9</version>
</dependency>

我的 class 中也有一些像这样的导入

import org.hsqldb.lib.StringUtil;

例如: 这对 pom.xml 没有依赖性,但在其中一个项目 classes 中使用,并且 jacoco 在报告

中显示了它
import javax.mail.internet.AddressException;

我有其他案例具有相同的行为导致相同的问题:Jacoco 在报告中显示来自 jar 的 classes,如图所示

不知道你是怎么生成lib目录的,因为你没有提供complete example.

但是在下面的例子中

src/main/java/Example.java

class Example {
}

src/test/java/ExampleTest.java

public class ExampleTest {
    @org.junit.Test
    public void test() {
        new Example();
    }
}

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>

执行mvn clean verify产生

$ ls -R target/classes
target/classes:
Example.class  lib

target/classes/lib:
junit-4.12.jar

和后续报告

并添加以下内容后<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>

执行相同的命令mvn clean verify产生以下报告

如果以上方法没有帮助,请提供绝对完整示例,让其他人可以完全复制您所做的。

尝试 includes 而不是 excludes。请注意,最后需要 .class 。 尝试这样的事情:

<configuration>
    <includes>
        <include>com/company/package/**/*.class</include>
    </includes>
</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>
                        <!-- this configuration affects all goals -->
                        <includes>
                            <include>com/company/packageToInclude/**/*.class</include>                                               
                        </includes>
                    </configuration>
                    </execution>
                </executions>
            </plugin>