1.jar 包”域”的管理
1.jar 包 “域” 的概念
maven 中的 jar 包存在 “域” 的概念,对 jar 包设置域就能解决
jar包的 5 种域(默认 compile,其他有 4 种)
依赖范围 | 对于编译classpath有效 | 对于test classpath有效 | 对于运行时classpath | 例子 |
compile | Y | Y | Y | spring-core |
provided | Y | Y | ervlet-api | |
runtime | Y | Y | jdbc驱动 | |
test | Y | junit | ||
system | Y | Y | 本地maven仓库之外的类库 |
2.对指定的 jar 包设置对应的域
在 dependency 标签中,添加 scope 属性,指定 Jar 包的域,比如
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
2.多个 jar 包冲突的解决
直接依赖:就是直接通过坐标导入的
间接依赖:就是该 jar 包本身是通过被已导入的 jar 包所依赖, (被) 自动导入的
(使用 idea 时,可以快速查看 maven 的 jar 包依赖关系)
1.第一声明优先原则
比如同时导入了 spring-webmvc 和 spring-aop(不同版本)
而这 2 个都包含了 spring-beans,
那么,项目真正导入的 spring-beans 版本是最先声明的那个
2.路径近者优先原则
比如 spring-webmvc 和 spring-aop 和 spring-beans
前 2 个是间接依赖,后者是直接依赖,
那么,项目真正导入的是 spring-beans 的版本
3.排除依赖方式(推荐使用)
使用exclusions标签将传递过来的依赖排除
比如这里引入了 spring-beans ,排除它所间接导入的 spring-core
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusions>
</dependency>