最近,我升级到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 升级到新版本之外,还有什么方法可以改变这一点吗? (假设新版本的 byte-buddy 可与当前版本的 Mockito 配合使用。)
您只需在
mvn clean install
中添加mockito依赖项中的排除后执行pom.xml
即可。
我尝试了一下,成功了:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.5.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.12</version>
<scope>test</scope>
</dependency>
全新安装后的依赖树:
只需依赖管理您想要在项目中使用的内容。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>dummy</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<byte-buddy.version>1.14.9</byte-buddy.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${byte-buddy.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
更新:为了展示我的方法与rekha sharma建议的方法之间的区别,让我们检查一下依赖关系树是什么样的:
org.acme:dummy:jar:1.0
\- org.mockito:mockito-core:jar:4.11.0:test
+- net.bytebuddy:byte-buddy:jar:1.14.9:test
+- net.bytebuddy:byte-buddy-agent:jar:1.12.19:test
\- org.objenesis:objenesis:jar:3.3:test
看到了吗?我们没有像他的回答那样改变依赖树。树保持不变,仅修复了传递依赖版本。仅当测试代码直接从那里访问类时,添加 Byte Buddy (BB) 库才有意义。否则,
dependency:analyze
会正确地将 BB 库报告为未使用的、已声明的依赖项。