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

要说清楚<scope>import</scope>这个标签作用我们先来说一下<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文件的父类,在该模块下我继承了SpringBoot的所有依赖,这样方便于版本的管理。其次我还要在项目中用到SpringCloud的相关依赖,一个一个导入会很麻烦而且会是pom文件过大,这时由于maven中继承模式是单继承模式,我们就不能直接继承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>

我的理解就是将SpringCloud的依赖拷贝引用过来了这样就解决了单继承的问题。要注意的是  <scope>import</scope>必须在<dependencyManagement>下使用并且必须声<type>pom</type>,那么<scope>import</scope>的作用我就可以理解为当前依赖是引用拷贝来的不受单继承影响。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐