Maven parent.relativePath 说明

这里日记一下 关于Maven非常基础的问题: Maven parent.relativePath 的意思,很多人网上搜索 类似下面pom.xml中的问题 ,文件中的

<relativePath /> <!-- lookup parent from repository  --> 

是什么意思?

    <parent>
        <groupId>com.middol</groupId>
        <artifactId>webbase-dependency-management</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

网上大多数回到如下:

设定一个空值将始终从仓库中获取,不从本地路径获取,如<relativePath />
Maven parent.relativePath
默认值为../pom.xml
查找顺序:relativePath元素中的地址–本地仓库–远程仓库

部分 Java 菜鸟或是一部分老鸟竟然也懵逼
答案第一行 :

设定一个空值将始终从仓库中获取,不从本地路径获取,如<relativePath />  

然后又开始问 : 仓库 是哪一个仓库 !?

我就纳闷了搜什么百度,自己测试一下不就好了吗,另外直接点击进去看原生说明啊,下面是 xml 原生英文解释:

      <xs:element name="relativePath" minOccurs="0" type="xs:string" default="../pom.xml">
        <xs:annotation>
          <xs:documentation source="version">4.0.0</xs:documentation>
          <xs:documentation source="description">
            The relative path of the parent &lt;code&gt;pom.xml&lt;/code&gt; file within the check out.
            The default value is &lt;code&gt;../pom.xml&lt;/code&gt;.
            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.
            &lt;code&gt;relativePath&lt;/code&gt; allows you to select a different location,
            for example when your structure is flat, or deeper without an intermediate parent pom.
            However, the group ID, artifact ID and version are still required,
            and must match the file in the location given or it will revert to the repository for the POM.
            This feature is only for enhancing the development in a local checkout of that project.
          </xs:documentation>
        </xs:annotation>
      </xs:element>

读一读英文,不行字典翻译翻译,然后自己做下实验 得出如下结论:

  1. relativePath 是Maven为了寻找父模块pom.xml所额外增加的一个寻找路径
  2. relativePath 默认值为 …/pom.xml
  3. Maven 寻找父模块pom.xml 的顺序如下:
    (1)  first in the reactor of currently building projects
          这里一个maven概念 反应堆(reactor ),
          意思就是先从工程里面有依赖相关的模块中找你引入的
          parent 的pom.xml,
        
    (2) then in this location on the filesystem
         然后从 你定义的  <relativePath > 路径中找,
         当然你如果只是 /  即空值,则跳过该步骤,  
         默认值 ../pom.xml 则是从上级目录中找啦。

    (3)  then the local repository
       这个就不说了,如果 (1) (2) 步骤没有则从 本地仓库找啦。

    (4) and lastly in the remote repo
     这个还用说吗,上面都找不到了,最后只能从远程仓库找啦,再找不到就报错给你看 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章