如何通过Maven Shade插件详细解决项目版本冲突问题?

2026-05-24 01:031阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1217个文字,预计阅读时间需要5分钟。

如何通过Maven Shade插件详细解决项目版本冲突问题?

目录+背景+maven-shade-plugin介绍+解决问题

1.环境准备

2.解决方案

3.引入依赖+注意事项

maven-shade-plugin的其他使用背景+当我们在maven项目中引入第三方组件时,其依赖可能存在冲突+如何处理这些冲突

目录
  • 背景
  • maven-shade-plugin介绍
  • 解决问题
    • 1.环境准备
    • 2.解决方案
    • 3.引入依赖
  • 一些需要注意的坑
    • maven-shade-plugins的其他使用

      背景

      当我们在maven项目中引入第三方组件时,三方组件中的依赖可能会与项目已有组件发生冲突。

      比如三方组件中依赖maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.sk</groupId> <artifactId>rename-dependencies</artifactId> <version>1.0-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <relocations> <relocation> <pattern>com.alibaba</pattern> <shadedPattern>shade.com.alibaba</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

      从配置文件中可以看到,由于maven-shade-plugin插件在解决这个问题上其实是通过对依赖进行重命名而达到隔离的目的,所以配置主要是集中在relocations中。

      这里将以com.alibaba开头的包全部重命名为以shade.com.alibaba开头。

      3.引入依赖

      将rename-dependencies进行打包,打包好之后在原项目中引入rename-dependencies的依赖。

      此时在引入rename-dependencies之后,可以在项目下看到该依赖中的fastjson包名发生了变化

      此时在代码中调用fastjson相关方法,会提示选择所需要包,如下图,此时问题解决,两个版本的fastjson可同时使用已经兼容。

      一些需要注意的坑

      • 描述: 引入依赖找不到重命名的shade包
      • 原因:重命名的模块和需要引入依赖的模块在一个项目中,idea优先找本项目,所以没有走仓库
      • 解决方案:
      • 将模块从项目maven中移除,右键项目-maven-unlink maven projects
      • 新建一个项目专门来做依赖

      maven-shade-plugins的其他使用

      • 打入和排除指定jar包。maven-shade-plugins还有个功能就是打入和排除指定的jar包,通过和指定。

      官方配置示例

      <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> <excludes> <exclude>classworlds:classworlds</exclude> <exclude>junit:junit</exclude> <exclude>jmock:*</exclude> <exclude>*:xml-apis</exclude> <exclude>org.apache.maven:lib:tests</exclude> <exclude>log4j:log4j:jar:</exclude> </excludes> </artifactSet> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>

      官方配置示例

      <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>junit:junit</artifact> <includes> <include>junit/framework/**</include> <include>org/junit/**</include> </includes> <excludes> <exclude>org/junit/experimental/**</exclude> <exclude>org/junit/runners/**</exclude> </excludes> </filter> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>

      • 排除包内资源。在上面的pom中使用maven-shade-plugin时,使用来对包内META-INF下的一些资源进行排除。如上面的配置中排除META-INF下的资源文件

      <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters>

      以上就是使用maven shade插件解决项目版本冲突详解的详细内容,更多关于maven shade解决项目版本冲突的资料请关注自由互联其它相关文章!

      如何通过Maven Shade插件详细解决项目版本冲突问题?

      本文共计1217个文字,预计阅读时间需要5分钟。

      如何通过Maven Shade插件详细解决项目版本冲突问题?

      目录+背景+maven-shade-plugin介绍+解决问题

      1.环境准备

      2.解决方案

      3.引入依赖+注意事项

      maven-shade-plugin的其他使用背景+当我们在maven项目中引入第三方组件时,其依赖可能存在冲突+如何处理这些冲突

      目录
      • 背景
      • maven-shade-plugin介绍
      • 解决问题
        • 1.环境准备
        • 2.解决方案
        • 3.引入依赖
      • 一些需要注意的坑
        • maven-shade-plugins的其他使用

          背景

          当我们在maven项目中引入第三方组件时,三方组件中的依赖可能会与项目已有组件发生冲突。

          比如三方组件中依赖maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.sk</groupId> <artifactId>rename-dependencies</artifactId> <version>1.0-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <relocations> <relocation> <pattern>com.alibaba</pattern> <shadedPattern>shade.com.alibaba</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

          从配置文件中可以看到,由于maven-shade-plugin插件在解决这个问题上其实是通过对依赖进行重命名而达到隔离的目的,所以配置主要是集中在relocations中。

          这里将以com.alibaba开头的包全部重命名为以shade.com.alibaba开头。

          3.引入依赖

          将rename-dependencies进行打包,打包好之后在原项目中引入rename-dependencies的依赖。

          此时在引入rename-dependencies之后,可以在项目下看到该依赖中的fastjson包名发生了变化

          此时在代码中调用fastjson相关方法,会提示选择所需要包,如下图,此时问题解决,两个版本的fastjson可同时使用已经兼容。

          一些需要注意的坑

          • 描述: 引入依赖找不到重命名的shade包
          • 原因:重命名的模块和需要引入依赖的模块在一个项目中,idea优先找本项目,所以没有走仓库
          • 解决方案:
          • 将模块从项目maven中移除,右键项目-maven-unlink maven projects
          • 新建一个项目专门来做依赖

          maven-shade-plugins的其他使用

          • 打入和排除指定jar包。maven-shade-plugins还有个功能就是打入和排除指定的jar包,通过和指定。

          官方配置示例

          <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> <excludes> <exclude>classworlds:classworlds</exclude> <exclude>junit:junit</exclude> <exclude>jmock:*</exclude> <exclude>*:xml-apis</exclude> <exclude>org.apache.maven:lib:tests</exclude> <exclude>log4j:log4j:jar:</exclude> </excludes> </artifactSet> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>

          官方配置示例

          <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>junit:junit</artifact> <includes> <include>junit/framework/**</include> <include>org/junit/**</include> </includes> <excludes> <exclude>org/junit/experimental/**</exclude> <exclude>org/junit/runners/**</exclude> </excludes> </filter> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>

          • 排除包内资源。在上面的pom中使用maven-shade-plugin时,使用来对包内META-INF下的一些资源进行排除。如上面的配置中排除META-INF下的资源文件

          <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters>

          以上就是使用maven shade插件解决项目版本冲突详解的详细内容,更多关于maven shade解决项目版本冲突的资料请关注自由互联其它相关文章!

          如何通过Maven Shade插件详细解决项目版本冲突问题?