嗨我在运行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)
答案 0 :(得分:10)
有两种方法可以避免这种情况:
1)重命名其中一个副本:我在maven项目中经常看到这个问题。即使这两个类在两个不同的模块中,但是对于两个不同的类具有相同的名称并不是一个好主意。
2)排除其中一个:有关详细信息,请参阅this SO主题。
答案 1 :(得分:1)
我同意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')
}
答案 2 :(得分:0)
我遇到了和你一样的问题。事实证明我在我的项目文件夹中有两个罐子做同样的事情,即使其中只有一个在构建路径中。检查代码中是否有多个hamcrest jar。
答案 3 :(得分: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 任务
jacocoTestReport {
doFirst {
delete fileTree (dir: "${buildDir}/gen/main/resources/uploadassets", include: "myCustomProject.jar")
}
reports {
xml.enabled true
}
}
这解决了我的问题。
答案 4 :(得分:0)
target
文件夹中的某些 jar 或项目文件通常会导致此问题。这就是解决我们 maven 项目中的问题的原因(参见 Sanjay's answer 以获得 gradle):
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<configuration>
<excludes>**/*.jar</excludes>
</configuration>
</plugin>
请注意,这是适用于我们特定项目的最简单的解决方案。如果您需要进行其他排除,请按如下方式包装多个 <exclude>
-命令:
<configuration>
<excludes>
<exclude>**/*.jar</exclude>
<exclude>naughty-dependency/**/*.class</exclude>
</excludes>
</configuration>
注意此配置部分不会故意将 JaCoCo 附加到任何 <execution>
目标。这样默认打包过程不会在 JaCoCo 倒下时中断,并且运行速度也稍快一些。报告只会在发出 mvn org.jacoco:jacoco-maven-plugin:prepare-agent test jacoco:report
时生成,例如我们在 gitlab CI-pipeline 中所做的。