JaCoCo和MR Jars [英] JaCoCo and MR Jars
问题描述
JaCoCo和 MultiRelease JAR文件存在问题.由于两个地方都存在相同的类名称,因此JaCoCo抱怨:
Caused by: java.lang.IllegalStateException: Can't add different class with same name: jodd/core/JavaBridge
at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:107)
at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:96)
我们如何告诉JaCoCo(在Gradle中)从META-INF路径中跳过类?还是根据JVM版本,表现出应有的表现(使用正确的类并忽略其他版本)?
如@nullpointer所述,JaCoCo不支持多版本JAR文件.
我的解决方法是忽略版本类.我无法通过显式设置其名称来仅忽略该类,看起来JaCoCo正在扫描所有它们,然后仅在以后应用过滤器进行排除(但也许我错了).
因此,删除版本类的唯一方法是排除所有资源-因为无论如何都不会使用它们.像这样:
task codeCoverage(type: JacocoReport) {
executionData fileTree("${buildDir}/jacoco/").include("*.exec")
//sourceSets it.sourceSets.main <--- REPLACED WITH FOLLOWING LINES!!!
sourceDirectories = it.sourceSets.main.java
classDirectories = it.sourceSets.main.output.classesDirs
reports {
xml.enabled true
html.enabled true
}
}
所以我改变了这个:
sourceSets it.sourceSets.main
对此:
sourceDirectories = it.sourceSets.main.java
classDirectories = it.sourceSets.main.output.classesDirs
这里我们明确指出的区别是:sourceSets.main.output.classesDirs
排除资源.
There is an issue with JaCoCo and the MultiRelease JAR files. Since the same class name exist on two places, JaCoCo complains:
Caused by: java.lang.IllegalStateException: Can't add different class with same name: jodd/core/JavaBridge
at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:107)
at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:96)
How we can tell JaCoCo (in Gradle) to skip the classes from META-INF path? OR to behave like it should (use correct class and ignoring other versions), depending on JVM version?
As explained by @nullpointer, JaCoCo doesn't support Multi-Release JAR Files.
My workaround is to ignore the versions classes. I was not able to ignore just the class by explicitly set its name, it looks like JaCoCo is scanning all of them and then only later applies the filters for exclusion (but maybe I am wrong).
Therefore, the only way to remove versions classes was to exclude all resources - since they are not used anyway. Like this:
task codeCoverage(type: JacocoReport) {
executionData fileTree("${buildDir}/jacoco/").include("*.exec")
//sourceSets it.sourceSets.main <--- REPLACED WITH FOLLOWING LINES!!!
sourceDirectories = it.sourceSets.main.java
classDirectories = it.sourceSets.main.output.classesDirs
reports {
xml.enabled true
html.enabled true
}
}
So I changed this:
sourceSets it.sourceSets.main
to this:
sourceDirectories = it.sourceSets.main.java
classDirectories = it.sourceSets.main.output.classesDirs
The difference here that we explicitly state: sourceSets.main.output.classesDirs
which excludes resources.
这篇关于JaCoCo和MR Jars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!