场景说明
- 在实际工作过程中可能出现这种情况,就是你基于开源框架开发了一些工具供其他人使用,比如说基于Hadoop3.0开发了一个hdfs小工具
然后使用你小工具的人自己项目里也依赖了Hadoop但是版本是2.x的,按照依赖调节规则,肯定就是用2.x版本的Hadoop了,这就可能造成依赖冲突
现在要做的就是如果你要依赖我写的包,那么版本号就由我来规定,你按照我的版本号来就OK ,不然就用不了!
配置
比如现在项目里依赖了一个1.2.58版本的fastjson
<?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>com.wisdom.oa</groupId>
<artifactId>restraint-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
</project>
针对restraint-test,开发一个pom包(restraint-test-bom),该pom包专门用dependencyManagement来约束依赖方的各个版本号 ```xml
com.wisdom.oa restraint-test-bom 1.0-SNAPSHOT pom
3. 依赖方(oa-order)不直接依赖restraint-test,而是依赖于restraint-test-bom,此时oa-order在dependency声明fastjson依赖时,在对fastjson自定义依赖版本时,pom就会报错
xml
<?xml version=”1.0” encoding=”UTF-8”?>
<!-- 此时如果声明junit的依赖版本号,pom文件报错-->
<dependencies>
<dependency>
<groupId>com.wisdom.oa</groupId>
<artifactId>restraint-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.42</version>
</dependency>
</dependencies>
```