Jacoco:无法排除类

8

我有一个Maven项目,我想使用 jacoco 来进行代码覆盖。这是我的pom文件中相关部分:

<code>          <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>
</code>

我可以成功地运行我的测试,也可以顺利地构建项目。然后我运行

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是我使用的一个外部依赖项,它是一个超级/阴影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>

但是那并没有起作用。是否可以从阴影/uber jar中排除某些内容?

此外,Jacoco为什么会关注这个问题?有没有办法修复或忽略这些错误?如何解决这个问题?


1
如果实际的Uber JAR中包含依赖关系,则排除该依赖关系是行不通的。您是否尝试过这里描述的解决方案:https://dev59.com/dWox5IYBdhLWcg3wjE5i? - Dennis Hunziker
谢谢,我之前没有看过那篇帖子。但我应该把 <sourcefiles> <zipfileset> <fileset dir="foo.jar"> <exclude name="org/jboss/osgi/framework/main/**/AbstractPackageAttribute*.*"/> </fileset> </zipfileset> </sourcefiles> 放在哪里? - AbtPst
我是指在哪个标签下? - AbtPst
1
没关系,那只存在于Ant版本中,而你使用的是实际的Maven插件。看着错误信息,我不太确定它会如何处理你的路径,因为它包含一个@符号和JAR内的路径。我建议尝试更一般的排除,例如 **/event/EventConstants* 或类似的选项。 - Dennis Hunziker
谢谢,我尝试过 **/event/EventConstants* 但是没有成功。 - AbtPst
看看这里是否有任何提示:https://dev59.com/eWgt5IYBdhLWcg3w-SP1 - AKS
3个回答

12

report目标的excludes属性指定在生成报告期间应该从分析中排除哪些文件。在这种情况下,/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 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

将会失败并返回错误信息

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

当使用包含META-INF/**排除项的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>
        <configuration>
          <excludes>
            <exclude>META-INF/**</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

会成功。

另外需要注意的是:prepare-agent目标中,excludes属性的语义是不同的 - 它指定了应该在测试执行期间从插桩排除的类名(与它们在磁盘上的位置无关)。


1

我通过在报告执行任务中添加<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>

你能否在解决方法中加入一些根本原因的描述,以便理解为什么这样解决问题? - undefined

0

/** 用于遍历底层目录,与名称的字符通配符“*”不同。避免使用 ** 可能会导致不良影响。


似乎这并不回答原问题,而是一般性的建议。 - RubioRic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接