依赖范围
#①import
管理依赖最基本的办法是继承父工程,但是和 Java 类一样,Maven 也是单继承的。如果不同体系的依赖信息封装在不同 POM 中了,没办法继承多个父工程怎么办?这时就可以使用 import 依赖范围。
典型案例当然是在项目中引入 SpringBoot、SpringCloud 依赖:
org.springframework.cloud spring-cloud-dependenciesHoxton.SR9 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2.2.6.RELEASE pom import org.springframework.boot spring-boot-dependencies2.3.6.RELEASE pom import
import 依赖范围使用要求:
打包类型必须是 pom必须放在 dependencyManagement 中
官网说明如下:
This scope is only supported on a dependency of type pom in the
section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
#②system
以 Windows 系统环境下开发为例,假设现在 D:tempareatguigu-maven-test-aaa-1.0-SNAPSHOT.jar 想要引入到我们的项目中,此时我们就可以将依赖配置为 system 范围:
com.atguigu.maven atguigu-maven-test-aaa1.0-SNAPSHOT D:tempareatguigu-maven-test-aaa-1.0-SNAPSHOT.jar system
但是很明显:这样引入依赖完全不具有可移植性,所以不要使用。如果需要引入体系外 jar 包我们后面会讲专门的办法。
#③runtime
专门用于编译时不需要,但是运行时需要的 jar 包。比如:编译时我们根据接口调用方法,但是实际运行时需要的是接口的实现类。典型案例是:
org.springframework.boot spring-boot-devtoolsruntime true
#2、可选依赖
#①配置举例
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot spring-boot-devtoolsruntime true
#②本质含义
可选其实就是『可有可无』。官网的解释是:
其核心含义是:Project X 依赖 Project A,A 中一部分 X 用不到的代码依赖了 B,那么对 X 来说 B 就是『可有可无』的。



