Maven标签配置之parent和dependencyManagement

一、parent依赖管理

作用:定位父项目的坐标标签,子项目可以直接继承父项目的依赖包,实现所有子项目共用相同的依赖包。
举例:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>

二、dependencyManagement 依赖管理

作用:父子项目依赖的版本管理。

  • 通常会在一个项目的最顶层的父pom中使用dependencyManagement,使用dependencyManagement标签能让所有子项目在引用一个依赖时不必列出版本号。这样子项目中该依赖的版本号将使用父项目pom文件中该依赖的版本。
  • 子项目中的某依赖没有指定版本号时,maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本。
  • 这样的好处就是: 如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或者切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另一个版本,只需要自己pom中声明版本号即可。
    需要注意的是:
  • dependencyManagement只声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖,如果不在子项目中声明依赖,是不会从父项目中继承下来的。
  • 只有在子项目中写了该依赖,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom。
  • 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

三、实例分析

(一)单一模块情况
<!--只是对版本号进行管理,不会实际引入jar-->
<dependencyManagement>  
      <dependencies>  
            <dependency>
            		<!--jar包身份限定-->
                <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>
                <!--不声明version 标签,则会继承dependencyManagement-->
       </dependency>  
</dependencies>
(二)多模块情况

父模块 pom.xml

<!--parent-module父模块pom.xml-->
<properties>
    <!--统一管理jar包版本。集中在父模块properties标签中定义所有依赖的版本号。-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.eclipse.persistence.jpa.version>1.2.6</org.eclipse.persistence.jpa.version>
    <javaee-api.version>1.8</javaee-api.version>
</properties>
 
<dependencyManagement>  
    <!--定义公共依赖的版本号-->
    <dependencies> 
        <dependency>  
            <groupId>org.eclipse.persistence</groupId>  
            <artifactId>org.eclipse.persistence.jpa</artifactId>  
            <version>${org.eclipse.persistence.jpa.version}</version>  
            <scope>provided</scope>  
        </dependency>  
          
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>${javaee-api.version}</version>  
        </dependency>  
    </dependencies>  
</dependencyManagement> 

子模块 pom.xml

<!--son-module子模块pom.xml-->
<!--继承父类-->  
<parent>
    <!--声明父类的身份信息-->
    <artifactId>parent-module</artifactId>
    <groupId>com.ppd</groupId>  
    <version>0.0.1-SNAPSHOT</version> 
    <!--声明父类的pom文件路径-->
    <relativePath>../parent-module/pom.xml</relativePath>
</parent>  
 
<modelVersion>4.0.0</modelVersion>  
<artifactId>son-module</artifactId>  
<packaging>ejb</packaging>  
  
<!--依赖关系-->  
<dependencies>  
    <dependency>  
        <groupId>javax</groupId>  
        <artifactId>javaee-api</artifactId>
        <!--未声明则继承父类version、scope-->
    </dependency>  
      
    <dependency>  
        <groupId>com.fasterxml.jackson.core</groupId>  
        <artifactId>jackson-annotations</artifactId>
        <!--声明则不继承父类version-->
        <version>1.8<version/>
        <!--继承父类scope-->
    </dependency>  
      
    <dependency>  
        <groupId>org.eclipse.persistence</groupId>  
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <!--未声明则继承父类version-->
        <scope>provided</scope>
    </dependency>  
</dependencies>

四、properties标签

作用:在pom.xml中的properties标签下声明相应的版本信息,然后在dependency下引用的时候用${spring-version}就可以引入该版本jar包了。
举例:

   <properties>
        <spring-version>4.3.7.RELEASE</spring-version>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-version}</version>
        </dependency>
 
    </dependencies>
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值