首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从jacoco报告中正确地排除和包含类、包和jar类、库(测试脱机)

如何从jacoco报告中正确地排除和包含类、包和jar类、库(测试脱机)
EN

Stack Overflow用户
提问于 2019-10-03 18:29:01
回答 2查看 4K关注 0票数 8

我使用的是JaCoCo代码覆盖率,但是报告包含了jar,lib中的类。(脱机仪器,Maven)

我解决了离线配置的问题,因为“aspectj plugin”正在更改类文件,而且现在我成功地将包排除在target/classes -> src之外。感谢stackoverflow中的这个答案。但是现在我从报告中的jar,lib中获得了类,我不知道如何排除该类。我在下面展示了我的配置和示例

我也尝试过这个解决方案,Exclude classes of jar files from jacoco coverage report,但它对我不起作用。<exclude>**/lib/*</exclude>

我的jacoco离线配置:

代码语言:javascript
运行
复制
<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>

万岁-插件

代码语言:javascript
运行
复制
            <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内部的jar获得类的原因是:报告。在我的pom.xml中,我有以下依赖项:

代码语言:javascript
运行
复制
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.2.9</version>
</dependency>

另外,我的类中有几个导入,如下所示

代码语言:javascript
运行
复制
import org.hsqldb.lib.StringUtil;

例如:它不依赖于pom.xml,而是在其中一个项目类中使用,并在报告中显示它。

代码语言:javascript
运行
复制
import javax.mail.internet.AddressException;

我还有其他同样行为导致相同问题的情况: Jacoco在报告中显示jar中的那些类,如图所示

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-04 14:14:29

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

代码语言:javascript
运行
复制
<configuration>
    <includes>
        <include>com/company/package/**/*.class</include>
    </includes>
</configuration>

根据您的示例:

代码语言:javascript
运行
复制
           <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>
票数 8
EN

Stack Overflow用户

发布于 2019-10-03 21:42:23

我不知道如何生成lib目录,因为您没有提供 example

但是,在下面的示例中

src/main/java/Example.java

代码语言:javascript
运行
复制
class Example {
}

src/test/java/ExampleTest.java

代码语言:javascript
运行
复制
public class ExampleTest {
    @org.junit.Test
    public void test() {
        new Example();
    }
}

pom.xml

代码语言:javascript
运行
复制
<?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的执行产生

代码语言:javascript
运行
复制
$ ls -R target/classes
target/classes:
Example.class  lib

target/classes/lib:
junit-4.12.jar

及后续报告

在添加以下<configuration>之后

代码语言:javascript
运行
复制
      <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将生成以下报告

如果上面没有帮助,那么请提供绝对完整的示例,让其他人都能完全复制您所做的事情。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58224968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档