35

嗨,我在运行 JaCoCo 覆盖时遇到以下异常:

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.5.8.201207111220</version>
        <executions>
            <execution>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>report</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Error while creating report: Can't add different class with same name: org/hamcrest/BaseDescription
java.lang.IllegalStateException: Can't add different class with same name: org/hamcrest/BaseDescription
    at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:89)
    at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:79)
    at org.objectweb.asm.ClassAdapter.visitEnd(Unknown Source)
    at org.jacoco.core.internal.flow.ClassProbesAdapter.visitEnd(ClassProbesAdapter.java:128)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:94)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:115)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:155)
    at org.jacoco.core.analysis.Analyzer.analyzeArchive(Analyzer.java:135)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:158)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:183)
    at org.jacoco.maven.ReportMojo.createBundle(ReportMojo.java:280)
    at org.jacoco.maven.ReportMojo.createReport(ReportMojo.java:256)
    at org.jacoco.maven.ReportMojo.executeReport(ReportMojo.java:230)
    at org.jacoco.maven.ReportMojo.execute(ReportMojo.java:208)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
4

5 回答 5

12

有两种方法可以避免这种情况:

1)重命名其中一个重复项:当涉及到 maven 项目时,我经常看到这个问题。即使这两个类位于两个不同的模块中,两个不同的类具有相同的名称也不是一个好主意。

2)排除其中之一:有关更多信息,请参阅SO 线程。

于 2012-08-22T07:58:27.640 回答
2

- 文件夹中的某些 jar 或项目文件target通常会导致此问题。这就是我们的 maven 项目中解决问题的方法(请参阅Sanjay对 gradle 的回答):

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <configuration>
                <excludes>**/*.jar</excludes>
            </configuration>
        </plugin>

请注意,这是适用于我们特定项目的最简单的解决方案。如果您需要进行其他排除,请<exclude>按如下方式包装多个 -command:

            <configuration>
                <excludes>
                    <exclude>**/*.jar</exclude>
                    <exclude>naughty-dependency/**/*.class</exclude>
                </excludes>
            </configuration>

请注意,此配置部分不会故意将 JaCoCo 附加到任何<execution>-goals。这样,当 JaCoCo 跌倒时,默认打包过程不会中断,并且运行速度也稍快。报告只会在发布时生成mvn org.jacoco:jacoco-maven-plugin:prepare-agent test jacoco:report,例如我们在 gitlab CI-pipeline 中执行的操作。

于 2021-05-13T14:31:43.810 回答
2

我同意 rb512。另一个可能出现此错误的情况/区域是,在编译过程中,如果我们使用 JIBX/WST 等或类似的其他步骤/过程创建动态类文件(在编译时),即我们没有任何有效的对应工作区中的源文件(.java/.groovy 等)。

在这种情况下,您的工作区将有额外的类文件,当 jacoco 尝试生成报告时,它会因重复类的相同错误而出错。如果由于这种情况而发现此错误,则必须在运行 jacoco 报告之前删除所有类文件

如果您使用的是 Gradle(对于 Maven,请在 Maven pom .xml 文件中执行相同操作),请参阅doFirst下面的块。

  jacocoTestReport {
      //cleaning up the JiBx classes to jacoco will not cause problems for CareDiscovery
      doFirst {
             delete fileTree (dir: "${buildDir}/classes", include: "**/JiBX_*.class")
      }
      group = "Reporting"
      description = "Generate Jacoco coverage reports after running tests."

      executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

      reports {
             xml{
                 enabled true
                 //Following value is a file
                 destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
             }
             csv.enabled false
             html {
                 enabled true
                 //Following value is a folder
                 destination "${buildDir}/reports/jacoco/html"
             }
      }

      sourceDirectories = files(['src/java','src/main/java', 'src/main/groovy'])
      classDirectories =  files('build/classes/main')
  }
于 2015-11-13T21:59:00.840 回答
0

就我而言,问题是由于项目 Jar artefact 中的类而发生的。

    Caused by: java.io.IOException: Error while analyzing /opt/jenkins/workspace/MicroService-M_build/microservice/dev/microservice/build/gen/main/resources/uploadassets/myCustomProject.jar@org/apache/commons/beanutils/BasicDynaBean.class.
Caused by: java.lang.IllegalStateException: Can't add different class with same name: org/apache/commons/beanutils/BasicDynaBean
    at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:107)

以下是从 jacoco 测试报告中排除自定义 jar 的 gradle taks

jacocoTestReport {
        doFirst {
            delete fileTree (dir: "${buildDir}/gen/main/resources/uploadassets", include: "myCustomProject.jar")
        }
        reports {
            xml.enabled true
        }
    }

这解决了我的问题。

于 2021-01-25T13:12:44.580 回答
0

我遇到了和你一样的问题。事实证明,我的项目文件夹中有两个 jars 做了同样的事情,即使其中只有一个在构建路径中。检查您的代码中是否有多个 hamcrest jar。

于 2016-05-14T18:29:15.473 回答