理解maven的parent标签里面的relativePath

经常看到用Spring Initializr建立spring boot项目,生成的pom.xml包含如下parent:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

也会看到一些maven module项目包含下面relativePath形式:

    <parent>
        <artifactId>admin</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

也有不写relativePath的,直接如下:

    <parent>
        <artifactId>admin</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

参考官方文档发现,relativePath是与从哪里继承相关联的,默认③什么都不写等同于②,像①那种是不依赖本地parent pom,直接从reposity拉取,

Maven looks for the parent pom first in the reactor of currently building projects, then in this location on the filesystem, then the local repository, and lastly in the remote repo.

寻找parent pom优先级:本地项目>文件系统>本地库>远程库,而springboot默认都是直接从远程库拉取的。

参考文档:

Understanding Maven’s “relativePath” Tag for a Parent POM:https://www.baeldung.com/maven-relativepath

 

posted @ 2022-01-26 20:07  feibazhf  阅读(3998)  评论(0编辑  收藏  举报