【发布时间】:2018-05-03 20:32:23
【问题描述】:
我有一个 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>
但这没有用。是否可以从阴影/超级罐子中排除?
而且,jacoco 为何关心?有没有办法修复或忽略这些错误?我该如何解决这个问题?
【问题讨论】:
-
如果它在实际的 uber JAR 中,排除依赖项将不起作用。您是否尝试过此处描述的解决方案:stackoverflow.com/questions/9133116/…?
-
谢谢,我没有看过那个帖子。但是我应该把
<sourcefiles> <zipfileset> <fileset dir="foo.jar"> <exclude name="org/jboss/osgi/framework/main/**/AbstractPackageAttribute*.*"/> </fileset> </zipfileset> </sourcefiles>放在哪里? -
我的意思是在哪个标签下?
-
没关系,这只存在于 ant 版本中,并且您使用实际的 Maven 插件。查看错误消息,我不太确定它将如何处理您的路径,因为它在 JAR 中包含 @ 和路径。我会尝试更一般的排除,例如
**/event/EventConstants*或类似选项。 -
谢谢,我试过
**/event/EventConstants*但没用
标签: java maven code-coverage jacoco jacoco-maven-plugin