java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Maven parent.relativePath说明

关于Maven parent.relativePath说明

作者:小时候的阳光

Maven中的relativePath用于指定父项目pom.xml的相对路径,默认值为../pom.xml,这个配置帮助Maven在构建时定位父模块的位置,确保模块间的依赖关系正确,relativePath可以指向本地或远程仓库中的父项目,如果不需要寻找父项目,可以将其设置为空

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>

网上大多数回到如下:

部分 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

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

这个还用说吗,上面都找不到了,最后只能从远程仓库找啦,再找不到就报错给你看 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文