Maven学习(十二)依赖范围补充

本文详细介绍了Maven的依赖范围,包括import范围如何用于解决多继承问题,例如在引入springboot、SpringCloud时的应用;system范围的使用及其不可移植性的风险;runtime范围适用于编译时非必需但运行时必要的依赖。此外,还讨论了可选依赖(optional)的概念和版本冲突的解决原则,即最短路径优先和先声明者优先的规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.依赖范围

①import

管理依赖最基本的方法就是继承父工程,但是和Java一样,Maven也是单继承的。如果不同体系的依赖信息封装在不同的POM里了,没办法继承多个父工程怎么办?这个时候就可以使用import依赖范围。
典型案例当然是在项目中引入springboot、SpringCloud依赖

<dependencyManagement>
        <dependencies>
            <!--springcloud依赖导入-->
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--springcloud Alibab依赖导入-->
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--springboot依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--通用mapper依赖-->
            <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>2.1.5</version>
            </dependency>
            <!--druid数据源-->
            <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
            <!--JPA依赖-->
            <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
            <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>persistence-api</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

import依赖范围使用要求:

  • 打包类型必须是pom
  • 必须放在 dependencyManagement 中

②system

以Windows系统环境下开发为例,假设现在 D:\demo06-mysql-data-provider.jar 想要引入到我们的项目中,此时我们就可以将依赖范围配置为system范围:

 <dependency>
      <groupId>com.aaa</groupId>
      <artifactId>demo06-mysql-data-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
      <systemPath>D:\demo06-mysql-data-provider.jar</systemPath>
      <scope>system</scope>
    </dependency>

但是很明显,这样引入完全不具有可移植性,所以不要使用。如果需要引入体系外的jar包,需要利用其它的办法。

③runtime

专门用于编译时不需要,但是运行时需要的jar包。比如:编译时我们根据接口调用方法,但是运行时需要的是接口的实现类。比如热部署

2.可选依赖

①配置举例

optional

     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

②本质含义

就是可有可无的意思,B对我们的工程X没啥用
在这里插入图片描述

3.版本冲突

①最短路径优先

如下例子:结果会采用1.2.12
在这里插入图片描述

②路径相同时先声明者优先

如下例子:结果会采用.2.17
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值