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

2022-09-04 01:05:00

尝试从 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 wrapper(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?


答案 1

Java 11 添加了基于嵌套的访问,因此必须更新任何字节代码编写 API(如 ASM)以支持类文件更改。

查看 6.2.1 版本的 ASM 中该方法的源代码(gradle 似乎正在使用该方法):

  @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的发布版本,然后再更新依赖项。


答案 2

ASM v7 已经发布

Gradle 问题已关闭,修复程序可在 Gradle 5.0 RC1 或更高版本上使用。

二进制文件应该很快就会可供下载


推荐