maven介绍
maven:项目管理工具,主要作用是在项目开发时 对Java项目进行依赖管理和项目构建。
依赖管理:对jar包的管理,所有的jar包存在仓库中,导入坐标就可以使用。
项目构建:通过maven一个命令就可以完成项目从清理、编译、测试、报告、打包、部署整个过程。
maven仓库类型:
- 本地仓库
- 远程仓库
- maven中央仓库:本地仓库没有,就从中央仓库下载。
- maven私服:公司的仓库,需要自己搭建。
- 其他远程仓库(apache提供的)
maven命令:
- clean,compile,test,package,install
maven的依赖范围:
依赖范围:(编译,测试,运行)
compile: y y y
test: - y -
provided: y y -
runtime: x y y
system: y y -
maven依赖冲突
maven中经常出现:spring-webmvc-4依赖spring-beans-4,而spring-aop-5依赖spring-beans-5。这样就出现了依赖冲突。
maven会自动选择4版本,而我们希望用5版本。
maven解决依赖冲突的原则:
- 第一声明者优先原则:以坐标导入顺序为准。
- 路径近者优先原则:如果直接导入spring-beans坐标,那就以导入坐标为准。
排除依赖
使用exclusions标签将传递过来的依赖排除出去。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
<!--指定不需要的依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
版本锁定:建议使用该方法
第一步:在dependencyManagement标签中锁定依赖的版本
第二步:在dependencies标签中声明需要导入的maven坐标
<!-- 锁定版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>