简体   繁体   English

如何从 jacoco 报告中正确排除和包含类、包和 jar 类、lib(仪器离线)

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

I am using JaCoCo code coverage, but the report is including classes from jar, lib.我正在使用 JaCoCo 代码覆盖率,但该报告包含来自 jar、lib 的类。 (Offline Instrumentation, Maven) (离线仪器,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 .我解决了脱机配置的问题,因为“aspectj-maven-plugin”正在更改 class 文件,而且现在我成功排除了target/classes -> src之外的包。 thanks to this answer in stackoverflow .感谢stackoverflow中的这个答案。 But now I am getting the classes from jar, lib inside the report and I have not idea how to exclude then.但现在我从报告中的 jar、lib 中获取类,我不知道如何排除。 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.我也试过这个解决方案从 jacoco 覆盖率报告中排除 jar 文件的类,但这对我不起作用。 <exclude>**/lib/*</exclude>

My jacoco offline configuration:我的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万能插件

            <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.以及我认为我从 de jacoco:report 中的 jar 获取课程的原因。 In my pom.xml I have the following dependencies:在我的 pom.xml 我有以下依赖项:

<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例如:这与 pom.xml 没有依赖关系,但在项目类之一中使用,jacoco 在报告中显示它

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我还有其他具有相同行为的案例导致相同的问题:Jacoco 在报告中显示来自 jar 的那些类,如图所示

在此处输入图像描述 在此处输入图像描述

Try includes instead of excludes .尝试includes而不是excludes Notice that you need.class at the end.请注意,最后需要.class。 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 .我不知道您如何生成lib目录,因为您没有提供完整的示例

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.xmlpom.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执行mvn clean verify产生

$ 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>添加以下<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执行相同的命令mvn clean verify产生以下报告

排除后报告

If the above doesn't help, then please provide absolutely complete example allowing everybody else to reproduce exactly the same what you do.如果上述方法没有帮助,那么请提供绝对完整的示例,让其他人能够完全复制您所做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM