maven中dependencymanagement和parent的作用(转载)

转载自:
https://www.cnblogs.com/zhangmingcheng/p/10984036.html
https://www.cnblogs.com/loveer/p/11376531.html
(若有冒犯,评论立删)

一、dependencymanagement

1、在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器。
2、pom.xml文件中,jar的版本判断的两种途径
      2.1 如果dependencies里的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承它,如果没有就会报错,告诉你必须为dependency声明一个version。
      2.2 如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的version为准。
3、dependencies和dependencyManagement的区别
      3.1 dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
      3.2 dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
4、示例:

//只是对版本进行管理,不会实际引入jar 
<dependencyManagement> 
      <dependencies> 
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-core</artifactId> 
                <version>3.2.7</version> 
            </dependency> 
    </dependencies> 
</dependencyManagement> 
   
//会实际下载jar包 
<dependencies> 
   <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
   </dependency> 
</dependencies>

二、parent

parent用于引用父工程
1、统一管理jar包的版本,其依赖需要在子工程中定义才有效(比如此例)
2、统一的依赖管理(父工程的,子工程不必重新引入)
3、控制插件的版本
4、聚合工程

<parent>
    <!--这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了。
      spring-boot-starter-parent 是一个特殊的starter,它用来提供相关的Maven默认依赖。
      使用它之后,常用的包依赖可以省去version标签。-->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <!--查找顺序:relativePath元素中的地址–本地仓库–远程仓库,
          设定一个空值将始终从仓库中获取,不从本地路径获取-->
    <relativePath/>
</parent>
posted @ 2021-06-15 00:24  少爷晚安。  阅读(1315)  评论(0)    收藏  举报