1 maven概述
- maven 是管理项目的工具。
- 项目各个阶段:清理、初始化、编译、测试、报告 、打包、部署、站点生成。
- maven目前开发内容:
- 依赖管理:维护jar包。
- 测试
- 打包
- 核心思想:项目对象模型 (Project Object Model),每一个maven项目,都有一个pom.xml文件,进行项目管理。
2 仓库
- 仓库分类:私有仓库、中央仓库、第三方仓库(远程)
- 私有仓库:每个人本地仓库,方面离线操作。
- 中央仓库:官方仓库,存放所有依赖。在国外。https://search.maven.org/
- 第三方仓库:由非盈利机构搭建第三方私有仓库,对外提供依赖下载。
- 阿里云
- 华为云
3 maven环境搭建
3.1 下载
- 版本:3.5.3 (3.3.9)
3.2 安装
- 将下载资源解压即可
3.3 配置:系统环境变量
- 配置window 系统环境变量
- MAVEN_HOME:
- 内容:maven安装目录
- 原因:方法其他环境变量使用、方便其他软件使用(idea)
- path:
- 内容:maven的bin目录(使用MAVEN_HOMN 确定安装目录)
- 原因:在cmd可以使用maven命令。
- MAVEN_HOME:
3.4 Maven使用
3.4.1 私有仓库配置
- 私有仓库的根目录:
D:\Java\maven\yycg_repository
- maven配置私有仓库
%MAVEN_HOME%/conf/settings.xml
3.4.2 配置镜像(第三方仓库,私服)
-
配置 aliyun的镜像
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
3.5 IDEA 配置
3.5.1 IDEA 配置 maven
-
idea在安装时,自动使用 MAVEN_HOME 配置的 本地maven。
-
如果没有识别,手动配置,采用通用配置
File/Settings/Maven...
-
配置1:确定maven安装目录
-
配置2:更新本地仓库
-
3.5.2 新项目配置
-
如果通用配置可以,建议使用通用。
-
如果通用不可用,使用新项目配置。
3.5.3 配置失败,重新配置
- 将idea配置信息删除(如果删除,相当于新安装的idea,包括激活码没有了。)
3.6 IDEA 中 maven使用
3.6.1 创建maven项目
- 步骤1:选择maven,进行项目创建
-
步骤2:填写项目详情
-
步骤3:开启自动导入
- idea 2019 能够选择开启自动导入
- idea 2020及其之后版本,必须手动操作
3.6.2 基本使用
- maven项目的生命周期命令的使用。
3.6.3 坐标
-
坐标:在maven中每一个项目都一个唯一标识,这个标识称为坐标,也称为依赖 dependency 。
-
坐标组成:组、标识、版本
-
通过坐标完成的使用
- 在maven项目中,通过坐标可以导入对应的jar包。
- 可以在本地仓库中,通过坐标获得jar包具体的位置。
-
使用坐标
-
情况1:直接使用
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> </dependencies>
-
情况2:先锁定版本,再使用
<!-- 锁定版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> </dependencies> </dependencyManagement> <!--使用--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
-
情况3:先定义版本,再锁定版本,最后使用
<!-- 版本号 --> <properties> <junit.version>4.9</junit.version> </properties> <!-- 锁定版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> </dependencyManagement> <!--使用--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
-