1. 为什么要用 Maven
1.1 项目管理问题
1 . 项目中需要引入的第三方 Jar 包越来越多,导致项目占用资源过多。
2 . Jar 包增加导致项目管理繁琐。
a. Jar 包依赖问题,可能一个 Jar 包需要其他多个 Jar 辅助工作。
b. 需要进行版本迭代,所有相关 Jar 包都需要整体替换。
c. 导致 Jar 包冗余,Jar 包引入多次
1.2 解决方案
目前比较流行的:
Maven
企业中有部分正在流行使用
Gradle
2. Maven 概述
Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念。Maven 主要管理 Jar 包依赖,Jar 版本控制,项目构造,项目管理..提高开发效率,降低项目冗余
3. Maven 安装
3.1 下载 Maven
推荐从官网下载,目前 Maven 的最新版本是 3.8,上课使用 3.6
Maven官方下载地址
3.2 Maven 安装
第一步:
解压,主要解压路径不能有中文,不能在 C 盘
解压之后的目录结构:
bin:
MVN 执行相关工具,脚本文件
boot:
Maven 运行辅助类库
conf:
配置文件,核心配置文件 settings.xml
lib:
Maven 运行对应的 java 类库
第二步:
配置 Maven 环境变量
a. 配置系统变量 MAVEN_HOME 对应的变量值是 MAVEN 安装路径
b. 修改 path 添加 %MAVEN_HOME% \bin
c. 测试 MVN 环境
cmd > mvn -v
3.3 Maven 配置 settings.xml
3.3.1 本地仓库配置
Maven 管理的 Jar 包,获取方式简单总结三种方式:
1. 远程仓库 【常用】
2. 本地仓库 【常用】
3. 私服
<settings xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" >
<localRepository > D:\WEBSoft\repository</localRepository >
3.3.2 默认JDK配置
配置当前 Maven 管理创建的 Java 项目默认的 JDK 版本
<profiles >
<profile >
<id > JDK8</id >
<activation >
<activeByDefault > true</activeByDefault >
<jdk > 1.8</jdk >
</activation >
<properties >
<maven.compiler.source > 1.8</maven.compiler.source >
<maven.compiler.target > 1.8</maven.compiler.target >
<maven.compiler.compilerVersion > 1.8</maven.compiler.compilerVersion >
</properties >
</profile >
</profiles >
<activeProfiles >
<activeProfile > JDK8</activeProfile >
</activeProfiles >
3.3.3 远程仓库配置
极大的提高 Maven 获取下载 Jar 包速度,默认是访问境外路径,网速受限,速率降低,推荐使用 阿里巴巴官方镜像!!!
免费,公开,速度快,效率高!!!
<mirrors >
<mirror >
<id > alimaven</id >
<name > aliyun maven</name >
<url > http://maven.aliyun.com/nexus/content/groups/public/</url >
<mirrorOf > central</mirrorOf >
</mirror >
</mirrors >
4. Maven 仓库
4.1 概念
1. 存储 Jar 包对应的位置,存储项目依赖对应的位置。
2. 每一个 Jar 包/ 每一个依赖都有一个唯一的标识(坐标),Java 项目可以根据坐标找到对应依赖【规范】
4.2 仓库分类
当前一个项目需要依赖的时候,查询对应依赖的顺序:
本地仓库 > 私服(一般公司会配置公司内部私服) > 公共仓库(需要配置) > 中央仓库
Maven 会根据当前项目所有的依赖/ Jar 包检索对应的 Jar 包内容,如果本地没有,从远端下载,存储到本地,二次使用时,优先选择本地仓库
Spring -context
Spring -core
本地仓库,远程仓库都在 settings.xml 中配置
本地:<localRepository > D:\WEBSoft\repository</localRepository >
远程:<mirror > ....</mirror >
4.3 中央仓库
Maven 默认使用你的是 Maven 社区提供的中央仓库,如果需要搜索检索依赖关系,可以在中央提供的搜素依赖网站进行检索功能:
【重点】https://mvnrepository.com/ Maven 提供的中央仓库检索网站
Maven 依赖关系检索网站
4.4 公共仓库【阿里巴巴】
http: //maven.aliyun.com/nexus /content/groups /public/
阿里巴巴在国内提供的官方镜像 公共仓库 可以提供下载效率。
5. IDEA 配置 Maven 环境
6. IDEA 创建 Maven 项目
6.1 Maven项目
6.2 Maven Java WEB 项目
<groupId > com.qfedu</groupId >
<artifactId > Day55_MavenWebApp_GL</artifactId >
<version > 1.0-SNAPSHOT</version >
<packaging > war</packaging >
<dependencies >
<dependency >
<groupId > junit</groupId >
<artifactId > junit</artifactId >
<version > 4.11</version >
<scope > test</scope >
</dependency >
<dependency >
<groupId > javax.servlet</groupId >
<artifactId > javax.servlet-api</artifactId >
<version > 4.0.1</version >
</dependency >
<dependency >
<groupId > javax.servlet.jsp</groupId >
<artifactId > jsp-api</artifactId >
<version > 2.2.1-b03</version >
</dependency >
<dependency >
<groupId > commons-beanutils</groupId >
<artifactId > commons-beanutils</artifactId >
<version > 1.9.4</version >
</dependency >
<dependency >
<groupId > com.alibaba</groupId >
<artifactId > fastjson</artifactId >
<version > 1.2.75</version >
</dependency >
<dependency >
<groupId > dom4j</groupId >
<artifactId > dom4j</artifactId >
<version > 1.1</version >
</dependency >
<dependency >
<groupId > jaxen</groupId >
<artifactId > jaxen</artifactId >
<version > 1.2.0</version >
</dependency >
<dependency >
<groupId > mysql</groupId >
<artifactId > mysql-connector-java</artifactId >
<version > 5.1.47</version >
</dependency >
<dependency >
<groupId > jstl</groupId >
<artifactId > jstl</artifactId >
<version > 1.2</version >
</dependency >
<dependency >
<groupId > com.alibaba</groupId >
<artifactId > druid</artifactId >
<version > 1.2.8</version >
</dependency >
</dependencies >
7. 依赖生命周期
导入的依赖/Jar包,有效范围,生命周期
项目对于依赖的生命周期:
测试 编译 运行 打包
test 有且当前依赖/Jar包,只在测试代码中有效,参与测试代码的编译和运行,项目部署无效,也不会打包到项目对于 war 包中。例如 junit
compile 缺省修饰,当前 依赖/Jar包 代码的所有过程,测试 编译 运行 打包。
runtime 运行时有效 测试 运行 打包 mysql驱动
provided 和 compile 类似,参与测试,编译,运行,打包 例如 Servlet-api
system Maven 不会在仓库(本地仓库和远程仓库)中查询对应的依赖/Jar包,只会在当前项目所在磁盘中找出到对应依赖/Jar包,一般用于测试,运行
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现