跳至主要內容

使用maven-shade-plugin解决依赖冲突

postjava大约 1 分钟

使用maven-shade-plugin解决依赖冲突

一般遇到一些比较复杂恶心的依赖冲突,传统的通过dependencyManagementexclusion有时候是解不了的。这种问题最常见的是netty相关的。 这种情况下我们可以使用maven-shade-plugin插件。

比如我这边的一个case是我们依赖的redisson需要4.1.36.Final的netty,而我们公司其他的组件必须依赖4.0.46.Final的netty,而这2个版本的netty 是不兼容的,因此我当时就使用了maven-shade-plugin插件来解决这个问题。

因为我们公司内部的netty都是4.0.46.Final,因此我专门搞了一个redisson-shade的maven module,这个module只有pom.xml文件,在这个module中我们依赖 redisson,然后将redisson内部依赖的netty的package路径进行修改, 然后其他的module通过依赖redisson-shade的maven module就好了。

pom.xml文件内容为:

<dependencies>
		<dependency>
			<groupId>org.redisson</groupId>
			<artifactId>redisson</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<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>io.netty</pattern>
									<shadedPattern>com.xxx.io.netty.redisson</shadedPattern>
								</relocation>
							</relocations>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<finalName>redisson-shade</finalName>
	</build>

在上面的pom中,在package阶段,会把redisson依赖的netty的package路径从io.netty修改为com.xxx.io.netty.redisson。这样就解决了依赖冲突问题。

不过我是在同一个工程中搞多个maven module来弄的,因此本地测试时候,一般都需要专门搞一个测试工程,这个算是一个麻烦点。


版权申明

本站点所有内容,版权均归https://wenchao.renopen in new window所有,除非明确授权,否则禁止一切形式的转载协议

打赏

微信 支付宝

上次编辑于:
打赏
给作者赏一杯咖啡吧
您的支持将是我继续更新下去的动力
微信微信
支付宝支付宝