使用maven-shade-plugin解决jar包冲突问题:java.lang.NoSuchMethodError: com.google.common.collect.Sets.newConcurrentHashSet

jar包冲突

一个maven项目可能会依赖了不同版本的jar包而导致冲突,或者和执行环境依赖的jar包冲突
在笔者例子中,maven了com.google.common包,但是执行环境也有依赖的com.google.common包,且因为版本冲突的原因导致项目依赖的newConcurrentHashSet在环境依赖的老版本jar包中找不到因而报错。

使用maven-shade-plugin relocate解决冲突问题

参考:使用maven-shade-plugin插件解决spark依赖冲突问题

定位到冲突的包,本例为:com.google.common
使用maven-shade-plugin重命名,避免冲突:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                              
                                <relocation>
                                    <pattern>com.google.common</pattern>
                                    <shadedPattern>com.shaded.google.common</shadedPattern>
                                </relocation>
                               
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

更多推荐