# maven概述
# 什么是maven
maven,译为专家,是Apache下的纯Java开发的开源项目。是一个项目管理工具,可以对java项目进行构建、依赖管理。
# maven环境
maven是一个基于java的工具,maven的运行依赖JDK.
# POM项目对象模型
# 配置项类型
- 项目依赖
- 插件
- 执行目标
- 项目构建profile
- 项目版本
- 项目开发者列表
- 相关邮件列表信息
# maven生命周期
# 生命周期流程
- validate: 验证项目
- compile: 执行编译
- test: 单元测试
- package: 打包
- verify: 检查,对测试结果进行检查
- install: 安装,安装到本地仓库,供其它项目使用
- deploy: 部署,拷贝到远程仓库
# 生命周期注意事项
执行某一个阶段的流程任务时,同时会执行它的前序流程任务.
# maven依赖作用域
# test
- 只在测试时有效
- 不进行依赖传递
# compile
- 默认的有效范围
- 在编译、运行、测试时,均有效
- 依赖传递
# provided
- 在编译测试时,有效,运行时无效
- 不进行依赖传递
# runtime
- 在运行、测试时有效,编译时无效
- 依赖传递
# system
- 在编译,测试时有效,运行时无效。 同provided
- 依赖传递
# 依赖传递性及优先级
# optional控制依赖传递性
- 设置optional为true,可以终止依赖传递
# 依赖优先级
间接依赖路径最短优先原则
pom文件声明顺序优先原则
# 依赖隔离
# 通过exclusions隔离
<exclusions>
<exclusion>
<groupId>com.automannn</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
- 隔离时,可以使用通配符
- 隔离可以传递
# 新建空包
- 新建一个空包,空包的坐标和需要隔离的Jar包坐标保持一致,deploy的公司的私服上面
- 通过pom声明优先原则,替换需要隔离的包
- 优点:不用在所有间接依赖的地方去exclusion,该空包后面所有无论是直接依赖还是间接依赖的相同坐标的jar包都不会被使用了。
# 配置集中管理-BOM
# 什么是bom
- BOM(Bill of Magterials)是一个特殊的POM文件,主要作用是声明一组依赖项的版本号,以便其它项目复用。其本身不包含任何依赖项,仅声明版本号。
# 如何使用bom
- 注意,需要显示的声明type为pom,才能够被识别为BOM;
<project>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>
# maven仓库
# 仓库类型及优先级
- 本地仓库
- 中央仓库 -> id为 central,为固定值
- 远程仓库
# 常用maven命令
# 安装本地jarLib
mvn install:install-file -DgroupId=yourGroupId -DartifactId=yourArtifactId -Dversion=1.0.0 -Dpackaging=jar -Dfile=absolutePath/your_jarLib.jar -DrepositoryId=yourRepositoryId
# 发布本地jarLib到远程仓库
mvn deploy:deploy-file -DgroupId=groupName -DartifactId=artifactid -Dversion=yourVersion -Dpackaging=jar -Dfile=absulotePath/your_jarLib.jar -Durl=http://xxx.xxx.xxx.xxx:8081/nexus/content/repositories/subPath/ -DrepositoryId=repoId
易错点:在idea的命令界面中,命令前后不能留空
# 常规发布
- 配置server,(可在用户pom,也可以在全局pom)
<servers>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
- 发布配置
<!-- 发布maven私服 -->
<distributionManagement>
<repository>
<id>nexus-snapshots</id>
<name>my-snapshots-repo</name>
<url>http://47.100.xxx.xxx:30010/repository/3rd-part/</url>
</repository>
<snapshotRepository>
<id>nexus-repository</id>
<name>my-release-repo</name>
<url>http://47.100.xxx.xxx:30010/repository/3rd-part/</url>
</snapshotRepository>
</distributionManagement>
- 使用命令上传
mvn deploy -Dmaven.test.skip=true
# 版本管理
# 常见版本号说明
eg: 2.0.3-RELEASE
- 2: 主版本号,功能模块有较大更新,或整体架构发生变化时,主版本号会更新
- 0: 次版本号,局部的一些变动
- 3: 修改版本号,bug的修改或者小的变动
- RELEASE: 希腊字母版本号,表明当前版本的软件处于哪个开发阶段
# 希腊字母版本号
- BASE: 设计阶段,只有相应的设计,没有具体的功能实现
- Alpha: 软件的初级版本,基本功能已经实现,存在较多bug
- Beta: 消除了严重BUG,但存在一些潜在的BUG,还需要不断测试
- RELEASE: 最终版本
# 常见问题处理
# 本地仓库更新策略导致更新失败
- 解决方式有多种,详见
https://blog.csdn.net/feiying0canglang/article/details/125673756
- 推荐通过命令行的方式,强制更新
mvn clean package -U
# 根据类名查找maven依赖
- 根据全类名查找:https://search.maven.org (opens new window)
- fc:全类名的方式进行检索,比如:
fc:com.alibaba.druid.pool.DruidDataSource
,fc代表:full class name - c:类名的方式进行检索,比如:
c:DruidDataSource
- fc:全类名的方式进行检索,比如:
- 根据关键字检索: https://mvnrepository.com/ (opens new window)