使用 gradle 迁移到 Java 11;不支持的操作异常

Migrate to Java 11 with gradle; UnsupportedOperationException

正在尝试从 jdk 10 切换到 11,但无法确定哪个库调用此异常:

Caused by: org.gradle.api.GradleException: failed to read class file ../../SomeTestFile.class
...
...
Caused by: java.lang.UnsupportedOperationException
        at org.objectweb.asm.ClassVisitor.visitNestMemberExperimental(ClassVisitor.java:248)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:651)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:391)
        at org.gradle.api.internal.tasks.testing.detection.AbstractTestFrameworkDetector.classVisitor(AbstractTestFrameworkDetector.java:124)

我将 gradle 包装器 (v4.10.2) 与以下内容一起使用 build.gradle:

buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
        schemaDownloadVersion = '1.6'
        generateAvroVersion = '0.14.2'
    }
    repositories {
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "http://packages.confluent.io/maven/" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("gradle.plugin.com.amit.plugin.download-registry:registry-schema-download-plugin:${schemaDownloadVersion}")
        classpath("com.commercehub.gradle.plugin:gradle-avro-plugin:${generateAvroVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.amit.plugin.download-registry'
apply plugin: 'com.commercehub.gradle.plugin.avro'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11

repositories {
    mavenCentral()
}

ext {
    springCloudVersion = 'Finchley.RELEASE'
    jaxbVersion = '2.3.0'
    activationVersion = '1.1.1'
    jmockitVersion = '1.43'
    lombokVersion = '1.18.2'
}

jacoco {
    toolVersion = '0.8.2'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-oauth2')
    compile("javax.xml.bind:jaxb-api:${jaxbVersion}")
    compile("com.sun.xml.bind:jaxb-core:${jaxbVersion}")
    compile("com.sun.xml.bind:jaxb-impl:${jaxbVersion}")
    compile("javax.activation:activation:${activationVersion}")
    compileOnly("org.projectlombok:lombok:${lombokVersion}")
    testCompile("org.jmockit:jmockit:${jmockitVersion}")
    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude(group: 'org.mockito')
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

...

调查这个问题我只发现 gradle 本身,lombok 和 jmockit 使用 asm 库可能会导致这个异常。有人可以回答我是哪个库调用了这个异常吗?

更新

我找到了关于同样问题的那篇文章: https://matsumana.info/blog/2018/09/25/gradle-with-jdk11/ 这指向 gradle 问题 https://github.com/gradle/gradle/issues/5120 所以这意味着 gradle 仍然不支持 Java 11 了吗?

Java 11 添加 nest based access, so any byte code writing APIs like ASM 必须更新以支持 class 文件更改。

查看 ASM 6.2.1 版本中该方法的源代码 (which is the one that gradle seems to be using):

  @Deprecated
  public void visitNestMemberExperimental(final String nestMember) {
    if (api < Opcodes.ASM7_EXPERIMENTAL) {
      throw new UnsupportedOperationException();
    }
    if (cv != null) {
      cv.visitNestMemberExperimental(nestMember);
    }
  }

所需的 API 等级为 7,目前处于测试阶段。我猜他们在更新依赖项之前正在等待 ASM 7 的发布版本。

ASM v7 已经 released

Gradle issue 已关闭,修复可在 Gradle 5.0 RC1或以后。

二进制文件应该很快就可以用于 download

Gradle4.10.3版本好像也解决了

我遇到了这个错误。因为我的 gradle 文件配置了 1.7 jdk,而在项目设置中我指的是 1.8 jdk。 所以我在 gradle 文件中更改为 来源兼容性 = 1.8 目标兼容性 = 1.8