jacoco:不能排除类 [英] jacoco : Cannot exclude classes
问题描述
我有一个maven项目,我想使用 jacoco
进行代码覆盖。这是我的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>报告< / goal>
< / goals>
< configuration>
< dataFile> $ {project.build.directory} /jacoco-it.exec</dataFile>
< outputDirectory> $ {project.reporting.outputDirectory} / jacoco-it< / outputDirectory>
< / configuration>
< / execution>
< / executions>
< / plugin>
所以我能够很好地运行我的测试并且还可以很好地构建项目。然后我运行
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent
我一直收到错误,如
错误]无法执行目标org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:项目my-project上的
报告(post-my-test):
JaCoCo Test中发生错误报告生成。
创建报告时出错:
分析类时的错误/ path / tp / my / project / target / classes / META-INF /
bundled-dependencies / some-third-party-1- jar-with-dependencies.jar@org/slf4j/event/
EventConstants.class。无法添加具有相同名称的不同类:
org / slf4j / event / EventConstants - > [帮助1]
some-third-party-1-jar-with -dependencies.jar
是我拥有的外部依赖项。这是一个超级/阴影的罐子。所以我认为某些第三方-1-jar-with-dependencies.jar
也必须有 org.slf4j
,因此冲突。所以我对pom进行了更改
< plugin>
< groupId> org.jacoco< / groupId>
< artifactId> jacoco-maven-plugin< / artifactId>
< version> 0.7.5.201505241946< / version>
< configuration>
<排除>
< exclude> ** / org / slf4j / event / EventConstants。*< / exclude>
< /排除>
< / 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>报告< / goal>
< / goals>
< configuration>
< dataFile> $ {project.build.directory} /jacoco-it.exec</dataFile>
< outputDirectory> $ {project.reporting.outputDirectory} / jacoco-it< / outputDirectory>
< / configuration>
< / execution>
< / executions>
< / plugin>
但我仍然得到同样的错误。如何确保jacoco忽略所有重复的依赖项?我也试过了
< dependency>
< groupId> some.third.party< / groupId>
< artifactId>某些第三方< / 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>
但这不起作用。是否可以从阴影/超级罐中排除?
此外,为什么jacoco会关心?有没有办法修复或忽略这些错误?如何解决此问题?
物业不包括<$ code>
report
目标指定在生成报告期间应从分析中排除哪些文件。如果是 /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文件中的类。
因此作为正确配置的示例之一:
< configuration>
<排除>
< exclude> META-INF / **< / exclude>
< /排除>
< / configuration>
作为证明 pom.xml
<?xml version =1.0encoding =UTF-8?>
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>示例< / 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
公共类示例{
}
和 src / test / java / ExampleTest.java
public class ExampleTest {
@ org.junit.Test
public void test(){
}
}
执行
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:报告
将失败并显示消息
分析/private/tmp/j/target/classes/slf4j-api-1.4.2.jar@org/slf4j/helpers/BasicMarker.class时出错。无法添加具有相同名称的不同类:org / slf4j / helpers / BasicMarker
同时执行 pom.xml
包含 $ META-INF / **
<?xml version =1.0encoding =UTF-8?>
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>示例< / 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>
<排除>
< exclude> META-INF / **< / exclude>
< /排除>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>
将成功。
作为一方注意:财产的语义不包括
prepare-agent
目标不同 - 它指定在执行测试期间应从检测中排除的类名(无论其磁盘位置如何)。 / p>
I have a maven project and I want to use jacoco
for code coverage. Here is the relevant section of my 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. 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. So I made a change to the pom as
<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? 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? 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. 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.
Therefore as one of examples of correct configuration:
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
As a proof having 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
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/**
<?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.
这篇关于jacoco:不能排除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!