为什么我们要学习 maven?
maven 还未出世的时候,我们有很多痛苦的经历。
痛点 1:jar 包难以寻找
痛点 2:jar 包依赖的问题
痛点 3:jar 不方便管理
痛点 4:项目编译
Maven 简介
Maven 是 Apache 软件基金会的一个开源项目,是一个优秀的项目构建工具,它用来帮助开发者管理项目中的 jar,以及 jar 之间的依赖关系、完成项目的编译、测试、打包和发布等工作。
Maven 中的概念
Pom(Project Object Model 项目对象模型)
Maven 管理的项目的根目录下都有一个 pom.xml 文件。pom.xml 文件指示Maven 如何工作。
在 pom.xml 文件中配置项目基本信息以及项目构建信息等。比如:项目坐标、项目依赖的 jar、插件、编译选项等。
一旦在 pom.xml 文件中配置了所依赖的 jar,Maven 会自动从构件仓库中下载相应的构件。
项目坐标
maven 给每个 jar 定义了唯一的标志,这个在 maven 中叫做项目的坐标,通过这个坐标可以找到你需要 用到的任何版本的 jar 包。
groupId、artifactId、packaging、version 的组合被称为项目的坐标,它们形成了项目的唯一标识,Maven。
通过坐标来精确定位构件。其中 groupId、artifactId、version 是必须的,且这三项的值必须唯一,packaging 是可选的(默认为 jar)。
仓库
中央仓库全球共享,先将 jar 从中央仓库下载到本地仓库,然后在项目中引用本地仓库的 jar。
![](https://img-blog.csdnimg.cn/img_convert/d4a34d319ffb26c1dae2b55d08f3d36e.png)
Maven 开发环境搭建
前提
安装 jdk
Maven 工作时需要通过网络下载各种插件以完成任务,因此应保证网络的通畅。
下载 maven 服务器
maven 官网:http:// maven.apache. org
![](https://img-blog.csdnimg.cn/img_convert/14e1c969a534fc25e968a37464249c17.png)
点击 download 进行下载如下图
![](https://img-blog.csdnimg.cn/img_convert/66192e6547429f5a11956985a5421120.png)
安装,配置 maven
点击下载二进制的 apache-maven-3.6.0-bin.zip 进行解压,”然后在系统环境中配置环境变量,在path中配置到bin目录,配置成功后输入命令mvn -version查看版本如下图:
![](https://img-blog.csdnimg.cn/img_convert/a484d3860955b60825b47d43e6c959b7.png)
表示 maven 环境配置成功。
配置本地仓库地址
接下来根据情况在本地磁盘中建立通过 maven 下载后 jar 文件所存储的文件夹,一般我们称为”仓库(repository)”;最终是如何将下载的 jar 文件存储到我们指定的仓库中呢?需要在 maven 的服务器解压的文件中找到 conf 文件夹下的settings.xml 文件进行修改,如下图所示:
![](https://img-blog.csdnimg.cn/img_convert/f65a973c009bf638e78f01c12cdc4f19.png)
配置阿里云 maven 镜像仓库,下载速度更快:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
idea 中设置 maven
![](https://img-blog.csdnimg.cn/img_convert/e7487b346ed6eda23e366c672ec28411.png)
创建 maven 工程
![](https://img-blog.csdnimg.cn/img_convert/11200c42ffd8af9b9448fc701802a3bf.png)
![](https://img-blog.csdnimg.cn/img_convert/f6d9f8335e9bc67695af95b5ef8fec89.png)
![](https://img-blog.csdnimg.cn/img_convert/fa155cbbc3174836d07a22b5f516bb9c.png)
![](https://img-blog.csdnimg.cn/img_convert/adaff43da0d1c3bb08d3bb0758ab72e3.png)
Pom.xml 配置
maven 仓库官网(http://mvnrepository.com/)如下图所示:
在此官网就可以下载自己所需开发 jar 包,继续点击链接打开如下:
![](https://img-blog.csdnimg.cn/img_convert/895620ff6beb4329a156796ab20c1ec1.png)
例如下载 servlet jar 包
在 Pom.xml 文件配置 jar 包信息,使用 maven 自动下载 jar 包
管理 jar 之间的依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--配置 servlet 开发环境 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
各个子元素功能解释:
<groupId>,<artifactId>和<version>:依赖项的基本坐标;
<type>:依赖项的 packaging 类型,默认是 jar;
<scope>:依赖项的作用范围,默认是 compile。各个取值及含义如下:
compile:编译时和运行时依赖。如果你的项目最终打包为 war 包,则该依赖包会被拷贝到 war 包中的 lib 目录中。
provided:编译项目主代码和测试代码时使用此依赖项,运行时由 jre 或 web++容器提供。如果你的项目最终打包为 war 包,则该 jar 包不会被打包。
maven 命令
Maven常用命令
1. compile 编译
2. clean 删除 target
3. package 打包
4. install 把项目 install 到本地仓库
5. test 运行测试代码
![](https://img-blog.csdnimg.cn/img_convert/89c537acc12565410a85c78a14991c2c.png)