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