最近我升级到Java 21并在构建过程中注意到这个错误
Failed to enhance class ...: Java 21 (65) is not supported by the current version of Byte Buddy which officially supports Java 20 (64) - update Byte Buddy or set net.bytebuddy.experimental as a VM property
问题是我在依赖项中使用 Mockito 4,它依赖于不支持 Java 21 的早期版本的 byte-buddy。
我尝试覆盖对较新版本的 byte-buddy 的依赖,如我的 POM 中所示
...
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.core.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.9</version>
<scope>test</scope>
</dependency>
...
但是当我运行 mvn dependency:tree 时我看到了这个
[INFO] +- org.mockito:mockito-core:jar:4.0.0:test
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.11.19:test
[INFO] | \- org.objenesis:objenesis:jar:3.2:test
[INFO] +- net.bytebuddy:byte-buddy:jar:1.14.11:test
所以覆盖不会影响mockito的依赖。 除了将 Mockito 升级到新版本之外,还有其他方法可以改变这一点吗? (假设较新版本的 btye-buddy 可与当前版本的 Mockito 配合使用)
只需依赖管理您想要在项目中使用的内容。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>