maven中<scope>import</scope>标签<type>pom</type>标签的含义

今天在学习到SpringCloud时看了看pom文件中发现了导入的项目中SpringCloud依赖中有<scope>import</scope>,当时不太清楚这和个标签的作用就去网上搜了一下,我自己的理解如下。

要说清楚<scope>import</scope>这个标签作用我们先来说一下<type>这个标签

type标签:

type:指明依赖需要引入的类型(jar、war、pom等),默认jar。为什么要说这个标签呢因为要用到<scope>import</scope>这个必须要声明<type>pom</type>。

我们来看下我项项目中的例子:

    <modules>
        <module>user-service</module>
        <module>order-service</module>
        <module>eureka-server</module>
        <module>gateway</module>
    </modules>
 
    <packaging>pom</packaging>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/>
    </parent>

  • 首先,我这个pom文件是作为其他子模块pom文件的父类,在该模块下我使用<parent>标签继承了SpringBoot的所有依赖(但不是引入jar包!),这样方便于版本的管理。
  • 其次,我还要在项目中用到SpringCloud的相关依赖,但是<parent>标签已经被占用了,怎么办?
    这时候我们就需要使用<dependencyManagement> + <dependencies>来引入SpringCloud的相关依赖,也就是我们常说的需要“解决Maven依赖单继承问题”。
    也就是说,这时由于maven的继承模式是单继承模式,我们就不能直接使用<parent>标签来继承Spring Cloud的依赖,那要怎么办呢?我们看下面代码:
        <dependencyManagement>
            <dependencies>
                <!-- springCloud -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>    
        </dependencyManagement>

一个一个导入会很麻烦而且会是pom文件过大,所以官方提供了一个依赖合集——直接引入spring-cloud-dependencies,即整个cloud全套的依赖!!

这样就解决了单继承的问题。要注意的是<scope>import</scope>必须在<dependencyManagement>下使用并且必须声明类型为<type>pom</type>。

当一个父pom中的dependencyManagement标签中需要导入另一个pom中的dependencyManagement的时候(此处是想引入spring-cloud-dependencies工程中的<dependencyManagement>),则必须同时使用<scope>import</scope> 和 <type>pom</type>

注意:dependencyManagement只在父工程(即pom类型的maven工程)中声明,在子工程中定义无需声明版本从而生效。如果在jar类型的maven工程中添加了dependencyManagement,是没有意义的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值