请问:如何优雅依赖多个版本的jar包?

问题现状

由于是一个迭代比较久的项目,项目中已经存在poi-tl 1.5.x的依赖,poi-tl v1.5.x是构建在Apache poi3.16上的版本,而我们现在要使用到easyexcel来处理导出,而easyexcel最低的Apache poi版本要求是4.1.2,将项目中已有poi的3.16升级到4.12时,旧代码出错,但是不升级就无法使用easyexcel。

解决思路

解决问题思路无非就是两种:

  1. 将项目中已有的poi3.16升级到4.12,解决升级后代码出错的地方;

    优点:jar包依赖清晰,在代码改动量可控的情况下,推荐使用该方式;

    缺点:代码改动量大,工作量大,而且容易对之前的功能带来不稳定因素;

  2. 使用maven-shade-plugin插件来解决,让项目依赖多个版本的poi版本;

    优点:对原来的功能无影响,代码改动量小;

    缺点:jar依赖变大,因为依赖了同一个依赖的2个版本;jar包依赖不那么清晰优雅;

今天文章主角就是第2种方式。

它的核心思路就是把easyexcel中的高版本poi包改个名字,同时easyexcel中引用的地方也改名(自动),并且代码中用高本版的地方也改个名(手动)。

解决问题

  1. 创建一个空maven项目,项目名称为jarjar,引入easyexcel的依赖;

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.1.1</version>
    </dependency>

    查看其依赖的poi版本

    请问:如何优雅依赖多个版本的jar包?
    easyexcel poi版本
  2. 引入插件并且配置好修改的方式;

    <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>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                            <relocations>
                                <relocation>
                                    <!-- <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>
                                <createDependencyReducedPom>true</createDependencyReducedPom>
                                <relocations>
                                    <relocation>
                                        <!-- 改名前 -->

                                        <pattern>org.apache.poi</pattern>
                                        <!-- 改名后 -->
                                        <shadedPattern>shaded.org.apache.poi</shadedPattern>
                                    </relocation>

                                    <!-- 可以配置多个 -->
                                    <relocation>
                                        <!-- 改名前 -->
                                        <pattern>com.deepoove.poi.XWPFTemplate</pattern>
                                        <!-- 改名后 -->
                                        <shadedPattern>shaded.com.deepoove.poi.XWPFTemplate</shadedPattern>
                                    </relocation>
                                </relocations>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>改名前 -->
                                    <pattern>org.apache.poi</pattern>
                                    <!-- 改名后 -->
                                    <shadedPattern>shaded.org.apache.poi</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    较为完整的pom.xml

    <?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.example</groupId>
        <artifactId>jarjar</artifactId>
        <version>1.0-SNAPSHOT</version>

        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>3.1.1</version>
            </dependency>
        </dependencies>

        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>

        <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>
                                <createDependencyReducedPom>true</createDependencyReducedPom>
                                <relocations>
                                    <relocation>
                                        <!-- 改名前 -->
                                        <pattern>org.apache.poi</pattern>
                                        <!-- 改名后 -->
                                        <shadedPattern>shaded.org.apache.poi</shadedPattern>
                                    </relocation>

                                    <!-- 可以配置多个 -->
                                    <relocation>
                                        <!-- 改名前 -->
                                        <pattern>com.deepoove.poi.XWPFTemplate</pattern>
                                        <!-- 改名后 -->
                                        <shadedPattern>shaded.com.deepoove.poi.XWPFTemplate</shadedPattern>
                                    </relocation>
                                </relocations>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    </project>
  3. 打出jar包;

    执行mvn package,如果是IDEA直接双击Lifecycle中的package就行了。

    请问:如何优雅依赖多个版本的jar包?
    打出jar包

    这时target目录中会有两个包,一个是original开头的原本包,因为我们没有新建类,所以这个包是空的。另一个是和artifactId-version.jar的包,artifactIdversion是本项目创建时填写的坐标。

    如图,我的这个maven项目叫jarjar,版本是1.0:

    请问:如何优雅依赖多个版本的jar包?
    找到指定的jar包
  4. 依赖本地jar,运行项目;

    <dependency>
                <groupId>org.example</groupId>
                <artifactId>jarjar</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>system</scope>
                <systemPath>${project.basedir}/src/main/java/lib/jarjar-1.0-SNAPSHOT.jar</systemPath>
    </dependency>
     <build>
       <resources>
        <resource>
          <directory>lib</directory>
          <targetPath>/BOOT-INF/lib/</targetPath>
          <includes>
            <include>**/*.jar</include>
          </includes>
        </resource>
       </resources>
     </build>
    1. 处理打包
    2. 自定义目录,这里在project的根目录新建lib文件夹,将jar放进去

    3. 引入jar包

      请问:如何优雅依赖多个版本的jar包?
      jarjar项目的pom信息

至此,已经无损的引入了easyexcel依赖,easyexcel中引用的地方也改名(自动),并且代码中用高本版的地方也改个名(手动)。

相关阅读

插件官网:https://maven.apache.org/plugins/maven-shade-plugin/index.html

maven-shade-plugin解决Maven同一依赖多版本共存:https://www.cnblogs.com/lixin-link/p/15507326.html

maven引入本地jar包的方法:https://cloud.tencent.com/developer/article/1510883


全文完。感谢大家阅读。如果喜欢连边的文章,请转发朋友圈,谢谢支持请问:如何优雅依赖多个版本的jar包?


– End – 

   连接角色边界,过好平凡生活。

请问:如何优雅依赖多个版本的jar包?

@关注和转发,就是最大的支持@

原文始发于微信公众号(连边):请问:如何优雅依赖多个版本的jar包?

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/57274.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!