dependencyManagement和dependencies的区别1.0

102 阅读2分钟
ok那么好各位,今天的自我修炼继续开始!🧐

一、什么是dependencies?

直接上代码:

<dependencies>
    <!-- JWT依赖-->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
    </dependency>

    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
    </dependency>
    <!-- no more than 2.3.3-->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
    </dependency>
</dependencies>

看到这,想必大家就对dependency不怎么陌生了,在maven项目中的pom文件中随地大小见,但是为了早日成为大佬,依旧巩固一遍:
dependencies:(这里的解释来自小豆同学)
小张的拙见就是:dependencies就是在maven项目中,用来存放各种各样的jar包,各种各样的依赖配置的tool,在dependencies中,存放着多个dependency,代表着多个家jar包

二、什么是dependencyManagement?

依旧先上代码:

</dependencyManagement>
    </dependencies>
        <dependency>
            <groupId>com.san.trip</groupId>
            <artifactId>message</artifactId>
            <version>1.4.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

看着和dependencies的结构非常相像,but其中的difference还是很大的,前面认真看了小豆同学的讲解(什么是dependencies?)的大佬,应该会注意到末尾的这段话: image.png 也就证明它还是存在不足的地方:在依赖继承中,父pom中定义的依赖,可以直接被子pom继承,子pom不需要再次声明,感觉还蛮OK的,BUT!!!,还是有很多可以更好的地方,比如:

  • 父pom中定义的依赖,会被所有模块继承,但是有些子pom模块或许不需要这个依赖,可能就会导致后期代码臃肿,产生jar冲突的问题
  • 子模块的pom可以覆盖继承父pom的依赖,可能会导致依赖版本出现混乱

而使用dependencyManagement,可以较好的解决

  • dependencyManagement 中声明的依赖,仅仅是声明,并没有真正的引入依赖。这样子模块可以从声明的依赖中,选择自己需要的,而不会继承所有父 pom 中的依赖
  • dependencyManagement 中除了声明依赖,还指定了依赖的版本。这样在子模块的依赖中,就无须指定版本,而是使用 dependencyManagement 指定的版本

(“这里又借助了CSDN中一位大佬的博客中的图案“)
如图所示: image.png