目录
  • 一、parent依赖管理
  • 二、dependencyManagement 依赖管理
  • 三、实例分析
    • 单一模块情况
    • 多模块情况
      • 父模块 pom.xml
      • 子模块 pom.xml
  • 四、properties标签

    一、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>
    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。