jacoco:不能排除类
我有一个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>report</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
我不断收到类似的错误
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
是我的外部依赖。这是一个超大/阴影罐。所以我认为也some-
third-party-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>
<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>
但我仍然遇到相同的错误。我如何确保jacoco忽略所有重复的依赖项?我也试过
<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>
但这没有用。阴影/超级罐子中可能有排除物吗?
而且,雅各布为什么要照顾?有什么办法可以解决或忽略这些错误?我该如何解决这个问题?
回答:
物业excludes
的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> <excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
作为证明 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 {}
和 src/test/java/ExampleTest.java
public class ExampleTest { @org.junit.Test
public void test() {
}
}
执行
mvn clean jacoco:prepare-agent packagemkdir 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
将失败并显示消息
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
同时执行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>
将会成功。
附带说明:目标属性excludes
的语义prepare-
agent是不同的
-它指定了类名(无论其在磁盘上的位置如何),在执行测试期间应从工具中排除该类名。
以上是 jacoco:不能排除类 的全部内容, 来源链接: utcz.com/qa/412986.html