如何从jacoco报告中排除并正确包含类,程序包和jar类lib(离线工具)

时间:2019-10-03 18:29:01

标签: java maven jacoco jacoco-maven-plugin

我正在使用JaCoCo代码覆盖率,但是该报告包括jar,lib中的类。 (离线仪表,Maven)

由于“ aspectj-maven-plugin”正在更改类文件,因此我解决了脱机配置的问题,现在我也成功排除了target/classes-> src之外的软件包。感谢stackoverflow中的回答。 但是现在我从报表中的jar和lib中获取类,并且我不知道该如何排除。我在下面显示我的配置和示例

我也尝试过此解决方案Exclude classes of jar files from jacoco coverage report,但对我而言不起作用。 <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>

这就是我认为我从de jacoco内的jar中获取类的原因:报告。在我的pom.xml中,我具有以下依赖性:

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

在这样的课程中我也有几个导入

import org.hsqldb.lib.StringUtil;

例如: 它不依赖于pom.xml,而是在项目类之一中使用的,jacoco在报告中将其显示

import javax.mail.internet.AddressException;

我还有其他情况下具有相同行为的情况,也会导致相同的问题:如图像所示,Jacoco在报表中显示了jar中的那些类

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

我不知道您如何生成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

及后续报告

report before exclusions

在添加以下<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会产生以下报告

report after exclusions

如果上述方法无济于事,请提供绝对完整的示例,让其他人重现与您所做的完全相同的事情。

答案 1 :(得分:1)

尝试使用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>