Maven依赖冲突:com.google.common版本不一致引发IllegalAccessError求助
问题背景
我在Maven项目中引入两个关键依赖:
org.dreambot:client:3.0.0(依赖A):自身包含旧版本com.google.common模块com.google.guava:guava:31.1-jre:最新版,同样包含com.google.common模块
使用maven-shade-plugin打包uber jar后运行,出现以下错误:
java.lang.IllegalAccessError: class com.google.common.collect.AbstractListMultimap cannot access its abstract superclass com.google.common.collect.AbstractMapBasedMultimap (com.google.common.collect.AbstractListMultimap is in unnamed module of loader java.net.URLClassLoader @6368386a; com.google.common.collect.AbstractMapBasedMultimap is in unnamed module of loader 'app')
问题原因
编译器尝试用依赖A中的旧版本父类匹配Guava依赖中的AbstractListMultimap,但二者来自不同类加载器的模块,且依赖A中并不存在AbstractListMultimap类,导致类访问权限异常。
解决方案
在maven-shade-plugin的配置中添加针对依赖A的类过滤规则,排除其中所有com.google.common相关的类,确保打包时仅保留新版Guava中的com.google.common模块。修改后的插件配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>dep-included</shadedClassifierName> <outputDirectory>${dreambot.path}/Scripts</outputDirectory> <filters> <!-- 全局过滤签名文件 --> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> <!-- 排除依赖A中的com.google.common类 --> <filter> <artifact>org.dreambot:client</artifact> <excludes> <exclude>com/google/common/**</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin>
配置说明
- 新增的过滤器针对
org.dreambot:client,明确排除其下所有com.google.common路径的类文件 - 保留原有全局过滤规则,避免签名文件冲突
- 打包时只会将新版Guava中的
com.google.common模块打入uber jar,彻底解决类冲突问题
内容的提问来源于stack exchange,提问作者kevkev
相关产品推荐
相关产品推荐

