Maven 是什么
Maven 将项目开发过程和管理过程抽象成一个项目对象模型(POM),本质上是一个项目管理工具。Maven 主要用于Java项目的依赖管理、编译、测试、打包和部署等操作。
Maven的核心设计围绕标准化和自动化,通过一系列约定和抽象简化项目构建与管理。
Maven 的核心概念
POM
POM(Project Object Model 项目对象模型)是Maven项目的核心配置文件,以XML格式描述项目的元数据、依赖、插件、构建规则等信息。
POM文件的组成
1、基础结构
pom文件以<project>作为开头,用于定义pom文件结构和版本约束。
xmlns和xmlns:xsi用于标识命名空间;xsi:schemaLocation用于指定XML模式文件的位置,用于验证POM文件合法性。
<modelVersion>用于标识POM模型版本。
(注:Maven2.x和3.x均使用的POM模型4.0.0版本,所以非特殊情况,版本必须设置为4.0.0)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>My Project</name>
<description>A demo project for Maven</description>
<url>http://example.com</url>
</project>
2、项目坐标
作为唯一标识项目的三元组。
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
3、依赖管理
定义项目的第三方依赖库。
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<!-- ... -->
</dependencies>
常用元素:
groupId:组织或项目的唯一标识符。
artifactId:项目中模块的名称。
version:指定依赖的版本。
scope:作用域。
optional:标记为可选项依赖。
exclusions:排除传递性依赖。
4、构建配置
定义编译、打包、插件等行为。
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
5、父模块与子模块
多模块配置,父模块聚合多个子模块。
<!-- 父模块 -->
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
<!-- 子模块 -->
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>module-a</artifactId>
6、依赖管理统一配置
父模块POM文件统一管理依赖版本,子模块引用时无需指定版本。
<!-- 父模块 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 子模块 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <!-- 无需写version -->
</dependency>
</dependencies>
7、属性定义
通过属性定义的方式,集中管理版本变量。
<!-- 定义变量 -->
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.3.18</spring.version>
</properties>
<!-- 引用属性 -->
<version>${spring.version}</version>
生命周期
1、clean生命周期
清理构建产物,核心阶段如下:
pre-clean:清理前的准备工作。
clean:删除构建输出目录。
post-clean:清理后的守卫操作。
2、default生命周期
编译、测试、打包、部署,核心阶段如下:
validate:校验项目配置是否合法。
compile:编译主代码到 target/classes。
test:运行单元测试。
package:打包代码。
verify:执行集成测试或质量检测。
install:将构建的产物安装到本地仓库。
deploy:将构建产物发布到远程仓库。
3、site生命周期
生成项目文档和报告。
pre-site:生成文档前的准备工作。
site:生成HTML文档到target/site目录。
post-site:文档生成后的收尾操作。
site-deploy:将文档部署到服务器。
总结
Maven 凭借其 标准化、依赖管理和自动化构建 能力,成为 Java 生态系统中的基石工具。无论是新项目选型还是旧项目维护,合理运用 Maven 的约定与扩展能力,将显著提升开发效率与项目可维护性。