本文用于记录今天在公司遇到的一个情况
场景为公司准备开发一个底座作为其他项目的公共依赖,如用户、权限、文件操作等公共模块,采用编译成jar包的方式供其他项目使用。
我这次对接登录和用户信息相关的jar包(用一个老系统进行的改造紧急开发的)。引入后导致底座和当前项目的版本不兼容,如底座Spring boot为2.2.9版本,本系统为2.7.1,还有mybatis等一系列的依赖都用版本都不一致,出现冲突。
以下是今天我们今天的解决方法。
1.在pom文件中修改底座的打包方式,去除间接引用,这样可以避免到部分间接应用其他模块的冲突依赖
<build>
<plugins>
<!-- 打JAR包,不包含依赖文件;显式剔除配置文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--${lib.dir.name}为项目配置的环境变量,下同-->
<outputDirectory>${lib.dir.name}/${output.jar.file.path}</outputDirectory>
<!-- 将配置文件排除在jar包 -->
<excludes>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*.xml</exclude>
<exclude>*.txt</exclude>
</excludes>
<archive>
<!-- 生成的jar中,包含pom.xml和pom.properties这两个文件 -->
<addMavenDescriptor>true</addMavenDescriptor>
<!-- 生成MANIFEST.MF的设置 -->
<manifest>
<!--这个属性特别关键,如果没有这个属性,有时候我们引用的包maven库 下面可能会有多个包,并且只有一个是正确的,
其余的可能是带时间戳的,此时会在classpath下面把那个带时间戳的给添加上去,然后我们 在依赖打包的时候,
打的是正确的,所以两头会对不上,报错。 -->
<useUniqueVersions>false</useUniqueVersions>
<!-- 为依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path下 -->
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 各个依赖加入前缀 -->
<!--这个jar所依赖的jar包添加classPath的时候的前缀,需要 下面maven-dependency-plugin插件补充-->
<!--一定要找对目录,否则jar找不到依赖lib,前边加../是因为jar在bin下,而bin与lib是平级目录-->
<classpathPrefix>${output.dependence.file.path}</classpathPrefix>
</manifest>
<manifestEntries>
<!-- 假如这个项目可能要引入一些外部资源,但是你打包的时候并不想把 这些资源文件打进包里面,这个时候你必须在
这边额外指定一些这些资源文件的路径,假如你的pom文件里面配置了 <scope>system</scope>,就是你依赖是你本地的
资源,这个时候使用这个插件,classPath里面是不会添加,所以你得手动把这个依赖添加进这个地方 -->
<!--MANIFEST.MF 中 Class-Path 加入自定义路径,多个路径用空格隔开 -->
<!--此处resources文件夹的内容,需要maven-resources-plugin插件补充上-->
<Class-Path>${output.resource.file.path}</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- 复制依赖的jar包到指定的文件夹里 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 拷贝项目依赖包到指定目录下 -->
<outputDirectory>${lib.dir.name}/${output.dependence.file.path}</outputDirectory>
<!-- 是否排除间接依赖,间接依赖也要拷贝 -->
<excludeTransitive>false</excludeTransitive>
<!-- 是否带上版本号 -->
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<!-- 用于复制指定的文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<!-- 复制配置文件 -->
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<!--将如下格式配置文件拷贝-->
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*.xml</exclude>
<exclude>*.txt</exclude>
</includes>
</resource>
</resources>
<!--输出路径-->
<outputDirectory>${lib.dir.name}/${output.resource.file.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.在本系统中剔除冲突的依赖
<dependency>
<groupId>xxx.xxx</groupId>
<artifactId>xxx-usercenter-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 写入需要从xxx-usercenter-service中剔除的依赖项 -->
<exclusions>
<exclusion>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</exclusion>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
3.有部分和Spring版本没关联、但和本身的版本更替有关联导致冲突的依赖基本需要重新引用命名空间(注意引用是的依赖版本)
这就是这次的解决办法,只是一个思路,各个项目都不一致,有需要的小伙伴可以借鉴。