JavaWeb - Maven 高级
1 Maven 依赖传递
// 依赖传递的概念
c -> 依赖 b -> 依赖 a // c 间接依赖a
// web项目直接依赖了spring-webmvc,而spring-webmvc依赖了spingaop、spring-beans等,web项目间接依赖spingaop、spring-beans等
1.1 依赖冲突
// 由于依赖传递, spring-webmvc 依赖 spirng-beans-5.1.5,spring-aop 依赖 springbeans-5.1.6,项目中spirng-beans 可能与期望不同
// 解决依赖冲突
// 1 maven 依赖调节原则
- 第一声明者优先原则
// 根据坐标导入顺序决定
- 路径近者优先原则
// 直接依赖大于依赖传递,及该项目POM中引入依赖为准
// 2 排除依赖
- exclusions标签将传递的依赖排除
<dependency>
<groupId>xx</groupId>
<artifactId>xx</artifactId>
<version>xx</version>
<exclusions>
<exclusion>
<artifactId>xx</artifactId>
<groupId>xx</groupId>
</exclusion>
</exclusions>
</dependency>
// 3 版本锁定
- dependencyManagement标签中锁定依赖的版本 //只进行版本锁定不会实际导包, dependencies 标签外
- 在dependencies标签中声明需要导入的maven坐标 // 不用写具体版本
// properties 标签 将dependencyManagement version抽取出来统一配置修改
<properties>
<mybatis.version>3.5.1</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
2 Maven 聚合工程
// 项目规模大,通过模块化拆分成小模块,分别开发
- 按业务模块拆分,一个模块一个maven工程
- 按层进行拆分,dao,service,web,每层对应一个maven工程
// maven 工程的继承
- 父工程 (提取公共代码和配置) <packaging>pom</packaging>
- 子工程 导入父工程的坐标
<parent>
<groupId>
<artifactId>
<version>
// maven 工程的聚合 打包等操作方便
// 父工程,可聚合非子工程
<modiles>
<module>
<module>
// 项目的依赖通过导入项目坐标
<dependencies>
<dependency>
<groupId>
<artifactId>
<version>