Gradle 使用 JaCoCo 插件测试失败 Java 15?

Gradle test fails using the JaCoCo plugin with Java 15?

https://github.com/gradle/gradle/issues/15038 中似乎提到了这个问题,但从该线程中不清楚如何修复它,因此将其张贴在这里。我是 运行 Java 15:

> java --version
openjdk 15.0.2 2021-01-19

和Gradle 6.6.1:

> gradle --version

------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------

Build time:   2020-08-25 16:29:12 UTC
Revision:     f2d1fb54a951d8b11d25748e4711bec8d128d7e3

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          14.0.1 (Oracle Corporation 14.0.1+14)
OS:           Mac OS X 10.16 x86_64

我已经开始了一个测试项目,选择 Application 作为它的类型,并添加了 jacoco 插件:

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application.
    id 'application'

    id 'jacoco'
}

但是,如果我尝试 ./gradlew test 我会收到以下错误:

> ./gradlew test

> Task :test
java.lang.instrument.IllegalClassFormatException: Error while instrumenting sun/util/resources/cldr/provider/CLDRLocaleDataMetaInfo.
        at org.jacoco.agent.rt.internal_43f5073.CoverageTransformer.transform(CoverageTransformer.java:94)
        at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
        at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
        at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)
        at java.base/java.lang.ClassLoader.defineClass2(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1108)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:183)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:784)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(BuiltinClassLoader.java:705)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClass(BuiltinClassLoader.java:586)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:634)
        at java.base/java.lang.Class.forName(Class.java:546)
        at java.base/java.util.ServiceLoader.loadProvider(ServiceLoader.java:854)
        at java.base/java.util.ServiceLoader$ModuleServicesLookupIterator.hasNext(ServiceLoader.java:1078)
        at java.base/java.util.ServiceLoader.hasNext(ServiceLoader.java:1301)
        at java.base/java.util.ServiceLoader.hasNext(ServiceLoader.java:1386)
        at java.base/sun.util.cldr.CLDRLocaleProviderAdapter.run(CLDRLocaleProviderAdapter.java:89)
        at java.base/sun.util.cldr.CLDRLocaleProviderAdapter.run(CLDRLocaleProviderAdapter.java:86)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
        at java.base/sun.util.cldr.CLDRLocaleProviderAdapter.<init>(CLDRLocaleProviderAdapter.java:86)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
        at java.base/sun.util.locale.provider.LocaleProviderAdapter.forType(LocaleProviderAdapter.java:188)
        at java.base/sun.util.locale.provider.LocaleProviderAdapter.findAdapter(LocaleProviderAdapter.java:287)
        at java.base/sun.util.locale.provider.LocaleProviderAdapter.getAdapter(LocaleProviderAdapter.java:258)
        at java.base/java.util.Calendar.createCalendar(Calendar.java:1693)
        at java.base/java.util.Calendar.getInstance(Calendar.java:1661)
        at java.base/java.text.SimpleDateFormat.initializeCalendar(SimpleDateFormat.java:677)
        at java.base/java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:621)
        at java.base/java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:600)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.getLastResortErrorLogFile(SystemApplicationClassLoaderWorker.java:162)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:111)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
Caused by: java.io.IOException: Error while instrumenting sun/util/resources/cldr/provider/CLDRLocaleDataMetaInfo.
        at org.jacoco.agent.rt.internal_43f5073.core.instr.Instrumenter.instrumentError(Instrumenter.java:159)
        at org.jacoco.agent.rt.internal_43f5073.core.instr.Instrumenter.instrument(Instrumenter.java:109)
        at org.jacoco.agent.rt.internal_43f5073.CoverageTransformer.transform(CoverageTransformer.java:92)
        ... 37 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 59
        at org.jacoco.agent.rt.internal_43f5073.asm.ClassReader.<init>(ClassReader.java:195)
        at org.jacoco.agent.rt.internal_43f5073.asm.ClassReader.<init>(ClassReader.java:176)
        at org.jacoco.agent.rt.internal_43f5073.asm.ClassReader.<init>(ClassReader.java:162)
        at org.jacoco.agent.rt.internal_43f5073.core.internal.instr.InstrSupport.classReaderFor(InstrSupport.java:280)
        at org.jacoco.agent.rt.internal_43f5073.core.instr.Instrumenter.instrument(Instrumenter.java:75)
        at org.jacoco.agent.rt.internal_43f5073.core.instr.Instrumenter.instrument(Instrumenter.java:107)
        ... 38 more

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed

前面提到的 GitHub 问题页面似乎说可以通过指定 jacoco 项目扩展 (https://docs.gradle.org/6.7/userguide/jacoco_plugin.html#sec:configuring_the_jacoco_plugin) 的 toolVersion 来解决这个错误,但是我不清楚选择哪个工具版本;我试过了

jacoco {
    toolVersion "0.8.5"
}

但错误结果相同。知道如何修复此错误吗?

搜索“jacoco java 15”时第一个结果是 the changelog

Snapshot Build 0.8.7.202101150304 (2021/01/15) ...

  • JaCoCo now officially supports Java 15

为 JaCoCo 0.8.6 列出了对 Java 15 的实验性支持。