场景说明

  1. 在实际工作过程中可能出现这种情况,就是你基于开源框架开发了一些工具供其他人使用,比如说基于Hadoop3.0开发了一个hdfs小工具

image.png

  1. 然后使用你小工具的人自己项目里也依赖了Hadoop但是版本是2.x的,按照依赖调节规则,肯定就是用2.x版本的Hadoop了,这就可能造成依赖冲突

  2. 现在要做的就是如果你要依赖我写的包,那么版本号就由我来规定,你按照我的版本号来就OK ,不然就用不了!

配置

  1. 比如现在项目里依赖了一个1.2.58版本的fastjson

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.wisdom.oa</groupId>
    7. <artifactId>restraint-test</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <dependencies>
    10. <dependency>
    11. <groupId>com.alibaba</groupId>
    12. <artifactId>fastjson</artifactId>
    13. <version>1.2.58</version>
    14. </dependency>
    15. </dependencies>
    16. </project>
  2. 针对restraint-test,开发一个pom包(restraint-test-bom),该pom包专门用dependencyManagement来约束依赖方的各个版本号 ```xml com.wisdom.oa restraint-test-bom 1.0-SNAPSHOT

    pom
com.wisdom.oa restraint-test 1.0-SNAPSHOT com.alibaba fastjson 1.2.58 3. 依赖方(oa-order)不直接依赖restraint-test,而是依赖于restraint-test-bom,此时oa-order在dependency声明fastjson依赖时,在对fastjson自定义依赖版本时,pom就会报错xml <?xml version=”1.0” encoding=”UTF-8”?> 4.0.0 com.wisdom.oa order 1.0-SNAPSHOT com.wisdom.oa restraint-test-bom 1.0-SNAPSHOT pom import
<!-- 此时如果声明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>

```