[英]jacoco : Cannot exclude classes
I have a maven project and I want to use jacoco
for code coverage.我有一个 Maven 项目,我想使用
jacoco
进行代码覆盖。 Here is the relevant section of my pom这是我的 pom 的相关部分
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-my-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-my-test</id>
<phase>post-my-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
So I am able to run my tests just fine and also build the project just fine.所以我能够很好地运行我的测试,也可以很好地构建项目。 Then I run
然后我跑
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent
I keep getting errors like我不断收到错误,例如
ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:
report (post-my-test) on project my-project:
An error has occurred in JaCoCo Test report generation.
Error while creating report:
Error while analyzing class /path/tp/my/project/target/classes/META-INF/
bundled-dependencies/some-third-party-1-jar-with-dependencies.jar@org/slf4j/event/
EventConstants.class. Can't add different class with same name:
org/slf4j/event/EventConstants -> [Help 1]
some-third-party-1-jar-with-dependencies.jar
is an external dependency that I have. some-third-party-1-jar-with-dependencies.jar
是我拥有的外部依赖项。 This is an uber/shaded jar.这是一个超级/阴影罐。 So I thought that
some-third-party-1-jar-with-dependencies.jar
must also have org.slf4j
, hence the conflict.所以我认为
some-third-party-1-jar-with-dependencies.jar
也必须有org.slf4j
,因此冲突。 So I made a change to the pom as所以我对 pom 进行了更改
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<excludes>
<exclude>**/org/slf4j/event/EventConstants.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
but I still get the same error.但我仍然遇到同样的错误。 How can I make sure that jacoco ignores all duplicate dependencies?
如何确保 jacoco 忽略所有重复的依赖项? I also tried
我也试过
<dependency>
<groupId>some.third.party</groupId>
<artifactId>some-third-party</artifactId>
<version>1</version>
<classifier>jar-with-dependencies</classifier>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
but that did not work.但这没有用。 Is it possible to have exclusions from shaded/uber jars?
是否可以从阴影/超级罐子中排除?
Moreover, why does jacoco care?而且,为什么jacoco会在意? Is there any way to fix or ignore these errors?
有没有办法修复或忽略这些错误? How can I resolve this issue?
我该如何解决这个问题?
Property excludes
of report
goal specifies which files should be excluded from analysis during generation of report. report
目标的属性excludes
指定在生成报告期间应从分析中排除哪些文件。 In case of /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar@org/slf4j/event/EventConstants.class
file is /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar
, and the rest is about class in JAR file.如果是
/path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar@org/slf4j/event/EventConstants.class
文件是/path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar
,其余的是关于 JAR 文件中的类。
Therefore as one of examples of correct configuration:因此,作为正确配置的示例之一:
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
As a proof having pom.xml
作为拥有
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>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
</plugin>
</plugins>
</build>
</project>
src/main/java/Example.java
public class Example {
}
and src/test/java/ExampleTest.java
和
src/test/java/ExampleTest.java
public class ExampleTest {
@org.junit.Test
public void test() {
}
}
Execution of执行
mvn clean jacoco:prepare-agent package
mkdir target/classes/META-INF/
cp ~/.m2/repository/org/slf4j/slf4j-api/1.4.2/slf4j-api-1.4.2.jar target/classes
cp ~/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar target/classes/META-INF
mvn jacoco:report
will fail with message将失败并显示消息
Error while analyzing /private/tmp/j/target/classes/slf4j-api-1.4.2.jar@org/slf4j/helpers/BasicMarker.class. Can't add different class with same name: org/slf4j/helpers/BasicMarker
while same execution with pom.xml
containing exclusion of META-INF/**
同时与
pom.xml
执行相同的包含META-INF/**
排除
<?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>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
will succeed.将会成功。
As a side note: semantic of property excludes
of prepare-agent
goal is different - it specifies class names (regardless of their on-disk location) that should be excluded from instrumentation during execution of tests.作为旁注:
prepare-agent
目标的属性excludes
的语义是不同的- 它指定了在测试执行期间应该从检测中排除的类名(无论它们在磁盘上的位置如何)。
I solved the duplication issue by adding <phase>prepare-package</phase>
to the report execution task:我通过将
<phase>prepare-package</phase>
到报告执行任务中解决了重复问题:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
/** is for traversing into underlying directories and different than * which is a character wild card for names. /** 用于遍历底层目录,不同于 * 是名称的字符通配符。 Avoiding ** can result in undesirable effect.
避免 ** 会导致不良影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.