maven父工程relativepath标签详解

1、概述

我们在使用maven构建父子工程时,通常会在父工程中将pom的打包方式设置为pom,然后在pom文件中使用modules标签来声明子工程,我们在子工程的pom文件中使用parent标签来声明自己属于哪个父工程。这样的好处是可以方便打包和管理依赖。

2、问题

如果我们的子工程是一个springBoot项目,那么我们可以在pom文件中使用parent标签去继承spring-boot的praent工程,这样的好处是我们项目中使用有关spring-boot的依赖可以全由父工程来管理,方便版本统一。但现在我们的praent标签已经指向了我们自己创建的父工程。在pom中只允许使用一个praent标签,那还有什么办法呢?

3、答案(方式一)

这是一个父工程的pom文件,该工程下管理了一个model1子工程

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mavenParent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>model1</module>
    </modules>

</project>

子工程pom文件:我们可以看到我们的父工程是声明的spring-boot-starter-parent,而不是我们自己的父工程,其实这样也可以被我们自己的父工程所管理,因为我们在父工程中使用了modules标签。这里有一个关键点:我将relativePath标签关闭了,也就是置空.(该标签必须置空,不写会使用默认值也会报错)。如果你是使用idea创建的maven工程,手动勾选了springboot,那么在relativePath标签旁边会注释一段话:在本地仓库查找依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>model1</artifactId>
    <packaging>jar</packaging>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <!-- 这里不需要指定版本号,版本由上面的父工程管理了 -->
        </dependency>
    </dependencies>

</project>

4、详解

relativePath标签含义:指定父工程的pom文件路径

如果你不使用这个标签,那他的默认值是<relativePath>../pom<relativePath>。也就是指向当前模块的上一级目录中的pom文件,也就是我们自己声明的父工程。

他的查找顺序为:relativePath元素中的地址–本地仓库–远程仓库。如果标签指定的地址找不到他就会去本地的maven仓库中找,如果本地仓库没有那就去远程仓库找。

使用<relativePath/>指定一个空地址,他就会去本地仓库中找。

5、番外:方式二

还有一种方式那就是不使用继承spring-boot-starter-parent的方式来管理springboot的依赖,我们可以点进这个spring-boot-starter-parent的pom文件中查看,我们发现它也有一个父工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.2</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>spring-boot-starter-parent</name>
  <description>Parent pom providing dependency and plugin management for applications built with Maven</description>
  

我们再点进这个父工程spring-boot-dependencies看,(这是一部分),我们可以看到其实帮我们管理版本的是这个父工程,它的打包方式也是pom,在properties标签中管理了很多依赖的版本。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.7.2</version>
  <packaging>pom</packaging>
  <name>spring-boot-dependencies</name>
  <description>Spring Boot Dependencies</description>
  <url>https://spring.io/projects/spring-boot</url>
  
  <properties>
    <activemq.version>5.16.5</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.98</appengine-sdk.version>
    <artemis.version>2.19.1</artemis.version>
    <aspectj.version>1.9.7</aspectj.version>
    <assertj.version>3.22.0</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
  </properties>

</project>

既然是这样,还记得dependencyManagement标签吗,这个标签可不是只能在父工程中才可以使用,我们在这里使用该标签来导入spring-boot-dependencies为我们整理的依赖,这样项目中国使用到的有关springboot的依赖就又可以不写版本号了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>model1</artifactId>
    <packaging>jar</packaging>
    <!--自己的父工程-->
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mavenParent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <!-- 这里不需要指定版本号,版本由下面的dependencyManagement管理了 -->
        </dependency>
    </dependencies>

    <!-- 这里dependencyManagement管理版本,只是管理版本,不导入依赖哦! -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.7.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code小鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值