Maven 完整教程
Maven 完整教程
本教程基于JDK 25和Maven 3.9版本。
第一章:Maven简介与核心概念
1.1 什么是Maven
Maven是一个强大的项目管理和构建自动化工具,主要用于Java项目。
它基于项目对象模型(POM)的概念,通过一个中央信息块(pom.xml文件)来管理项目的构建、报告和文档。
1.2 Maven的核心优势
- 依赖管理:自动下载、管理项目依赖库,解决版本冲突
- 标准化构建流程:提供统一的项目结构和构建生命周期
- 插件机制:通过插件扩展功能,满足各种构建需求
- 项目信息管理:集中管理项目信息,便于团队协作
- 跨平台支持:可在任何安装了Java的平台上运行
1.3 Maven核心概念
- POM (Project Object Model):项目对象模型,通过pom.xml文件描述项目
- 坐标 (Coordinates):唯一标识一个项目的groupId、artifactId、version
- 依赖 (Dependency):项目所依赖的外部库或模块
- 仓库 (Repository):存储依赖的地方,分为本地仓库和远程仓库
- 生命周期 (Lifecycle):构建过程的一系列阶段
- 插件 (Plugin):执行具体构建任务的组件
第二章:Maven安装与配置
2.1 系统要求
- JDK 25或更高版本
- 操作系统:Windows、Linux或macOS
- 网络连接(用于下载依赖和插件)
2.2 下载与安装
- 访问Maven官方网站下载最新版(3.9.11):https://maven.apache.org/download.cgi
- 解压到任意目录,例如:
- Windows:
D:\dev\java\apache-maven-3.9.11
- Linux/macOS:
/usr/local/apache-maven-3.9.11
- Windows:
2.3 环境变量配置
Windows系统:
# 设置Maven安装目录
set MAVEN_HOME=D:\dev\java\apache-maven-3.9.11# 将Maven的bin目录添加到PATH
set PATH=%PATH%;%MAVEN_HOME%\bin
Linux/macOS系统:
# 编辑bash配置文件
vi ~/.bashrc# 添加以下内容
export MAVEN_HOME=/usr/local/apache-maven-3.9.11
export PATH=$PATH:$MAVEN_HOME/bin# 使配置生效
source ~/.bashrc
2.4 验证安装
mvn -version
成功安装会显示类似以下信息:
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: D:\dev\java\apache-maven-3.9.11
Java version: 25, vendor: Oracle Corporation, runtime: D:\dev\java\jdk\jdk-25
Default locale: zh_CN, platform encoding: UTF-8
OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"
2.5 配置Maven
Maven的主要配置文件位于:
- 全局配置:
MAVEN_HOME/conf/settings.xml
- 用户配置:
~/.m2/settings.xml
(Windows用户为C:\Users\用户名\.m2\settings.xml
)
推荐配置:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"><!-- 本地仓库位置,建议修改到非系统盘 --><localRepository>D:/dev/java/.m2/repository</localRepository><!-- 配置镜像,加速依赖下载 --><mirrors><!-- 阿里云中央仓库 --><mirror><!-- 镜像的唯一标识,maven 内部用,随便写但别重复 --><id>aliyunmaven</id><!-- 把 Maven 自带的“central”仓库(repo1.maven.org)全部重定向到阿里云 --><mirrorOf>central</mirrorOf><!-- 可读性名字,列表或报错时给人看 --><name>阿里云公共仓库</name><!-- 真实的镜像地址;注意你贴的那行有嵌套 <url> 标签,实际要写成纯文本 --><url>https://maven.aliyun.com/repository/public</url></mirror></mirrors><profiles><!-- 配置JDK默认版本 --><profile><!-- 给这个 profile 起个名字,命令行 -Pjdk-25 可手动激活 --><id>jdk-25</id><activation><!-- 默认就启用,除非用户显式指定其他 profile --><activeByDefault>true</activeByDefault><!-- 只有当运行时的 JDK 版本等于 25 时才激活;双保险 --><jdk>25</jdk></activation><properties><!-- 下面 3 行是“真正告诉编译器用 25”的核心参数 --><!-- 源代码语言级别:25(模式匹配、虚拟线程 2.0 等新语法) --><maven.compiler.source>25</maven.compiler.source><!-- 生成的字节码目标版本:25(major version 69) --><maven.compiler.target>25</maven.compiler.target><!-- 显式指定要调用 javac 的版本号,防止找到旧 jdk --><maven.compiler.compilerVersion>25</maven.compiler.compilerVersion><!-- 统一字符编码,避免 Windows 控制台乱码 --><maven.compiler.encoding>utf-8</maven.compiler.encoding><project.build.sourceEncoding>utf-8</project.build.sourceEncoding><project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding><!-- 测试相关:失败也继续跑,CI 阶段不阻断 --><!-- 测试失败不抛 Build Failure --><maven.test.failure.ignore>true</maven.test.failure.ignore><!-- 直接跳过测试编译与运行,节省构建时间 --><maven.test.skip>true</maven.test.skip><!-- 跳过 javadoc 生成,本地开发/调试更快 --><maven.javadoc.skip>true</maven.javadoc.skip></properties></profile></profiles></settings>
第三章:Maven项目结构与POM文件
3.1 标准项目结构
Maven定义了统一的项目结构,所有Maven项目都遵循这一结构:
my-project/
├── pom.xml # 项目核心配置文件
├── src/
│ ├── main/ # 主代码目录
│ │ ├── java/ # Java源代码
│ │ ├── resources/ # 资源文件
│ │ ├── filters/ # 资源过滤文件
│ │ ├── webapp/ # Web应用资源(如JSP、JS等)
│ │ └── ...
│ └── test/ # 测试代码目录
│ ├── java/ # 测试Java源代码
│ ├── resources/ # 测试资源文件
│ └── filters/ # 测试资源过滤文件
└── target/ # 构建输出目录(自动生成)
3.2 POM文件详解
POM(Project Object Model)是Maven的核心,位于项目根目录下的pom.xml
文件中。
基本POM结构:
<?xml version="1.0" encoding="UTF-8"?>
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><!-- POM模型版本,Maven 3.0及以上使用4.0.0 --><modelVersion>4.0.0</modelVersion><!-- 项目坐标:唯一标识一个项目 --><groupId>com.example</groupId> <!-- 组织ID,通常是公司域名倒写 --><artifactId>my-project</artifactId> <!-- 项目ID,通常是项目名称 --><version>1.0.0-SNAPSHOT</version> <!-- 版本号,SNAPSHOT表示快照版本 --><packaging>jar</packaging> <!-- 打包类型,默认jar,web项目用war --><name>My Project</name> <!-- 项目名称 --><description>A sample Maven project</description> <!-- 项目描述 --><!-- 父项目配置(可选) --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.6</version></parent><!-- 项目属性 --><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>25</java.version><junit.version>5.13.4</junit.version></properties><!-- 依赖配置 --><dependencies><!-- JUnit 5测试框架 --><!-- 使用JUnit Jupiter聚合依赖(推荐) --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- 其他依赖... --></dependencies><!-- 构建配置 --><build><plugins><!-- Maven编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version><configuration><source>${java.version}</source> <!-- 源代码兼容性版本 --><target>${java.version}</target> <!-- 目标代码兼容性版本 --></configuration></plugin></plugins></build>
</project>
3.3 依赖范围(Scope)
Maven定义了几种依赖范围,用于控制依赖在不同构建阶段的可见性:
compile
:默认范围,编译、测试、运行时都有效provided
:编译和测试时有效,运行时由容器提供(如servlet-api)runtime
:测试和运行时有效,编译时不需要test
:仅测试时有效(如JUnit)system
:与provided类似,但需要显式指定本地JAR路径import
:仅用于dependencyManagement中,导入其他POM的依赖配置
第四章:Maven常用命令
4.1 项目构建命令
# 创建一个新的Maven项目(交互式)
mvn archetype:generate# 创建一个简单的Java项目
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=falsemvn archetype:generate \-DgroupId=com.example \ # ← 指定项目所属“组织/公司”的反向域名,对应 pom.xml 里的 <groupId>-DartifactId=my-app \ # ← 指定项目(主构件)的名字,对应 pom.xml 里的 <artifactId>,也会成为根目录名-DarchetypeArtifactId=maven-archetype-quickstart \ # ← 使用哪个“骨架模板”;quickstart 是最小 Java 单模块模板-DarchetypeVersion=1.5 \ # ← 骨架模板自身的版本号;1.5 是 2025 年仍在维护的 LTS 版本-DinteractiveMode=false # ← 关闭交互式提问,一次性生成,俗称“批模式”# 编译源代码
mvn compile# 编译测试代码
mvn test-compile# 运行测试
mvn test# 打包项目(生成JAR/WAR等)
mvn package# 安装项目到本地仓库
mvn install# 将项目部署到远程仓库
mvn deploy# 清理构建结果
mvn clean# 运行清理并打包(常用)
mvn clean package# 运行清理、测试并打包
mvn clean verify# 生成项目文档
mvn site
4.2 依赖管理命令
# 显示项目依赖树
mvn dependency:tree# 分析依赖冲突
mvn dependency:analyze# 下载项目所有依赖到本地仓库
mvn dependency:go-offline# 查找依赖更新
mvn versions:display-dependency-updates
4.3 其他常用命令
# 查看Maven版本信息
mvn -version# 查看帮助信息
mvn -help# 查看某个插件的帮助
mvn [插件前缀]:help# 强制更新快照版本依赖
mvn clean install -U# 跳过测试
mvn clean package -DskipTests# 跳过测试编译
mvn clean package -Dmaven.test.skip=true# 指定 profiles 构建
mvn clean package -Pprofile1,profile2
第五章:Maven生命周期与插件
5.1 Maven生命周期
Maven定义了三个相互独立的生命周期:
-
clean:清理项目
pre-clean
:执行清理前的操作clean
:清理上一次构建生成的文件post-clean
:执行清理后的操作
-
default:构建项目(最核心)
validate
:验证项目是否正确initialize
:初始化构建状态generate-sources
:生成源代码process-sources
:处理源代码generate-resources
:生成资源文件process-resources
:复制并处理资源文件到目标目录compile
:编译项目的源代码process-classes
:处理编译生成的文件generate-test-sources
:生成测试源代码process-test-sources
:处理测试源代码generate-test-resources
:生成测试资源文件process-test-resources
:复制并处理测试资源文件test-compile
:编译测试源代码process-test-classes
:处理测试编译生成的文件test
:运行测试prepare-package
:准备打包package
:打包成可发布的格式pre-integration-test
:集成测试前的准备integration-test
:执行集成测试post-integration-test
:集成测试后的操作verify
:验证包是否有效install
:安装包到本地仓库deploy
:部署包到远程仓库
-
site:生成项目站点文档
pre-site
:生成站点前的操作site
:生成项目站点文档post-site
:生成站点后的操作site-deploy
:部署站点到服务器
生命周期特点:
- 生命周期的阶段是有序的,执行后面的阶段会自动执行前面的阶段
- 三个生命周期相互独立,执行一个生命周期的阶段不会影响其他生命周期
5.2 Maven插件
插件是Maven功能的核心实现,每个插件可以实现多个目标(goal),对应生命周期的阶段。
常用插件:
- 编译插件:
maven-compiler-plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version><configuration><source>25</source><target>25</target><encoding>UTF-8</encoding></configuration>
</plugin>
- 打包插件:
maven-jar-plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version><configuration><archive><manifest><mainClass>com.example.App</mainClass> <!-- 指定主类 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix></manifest></archive></configuration>
</plugin>
- 资源插件:
maven-resources-plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version><configuration><archive><manifest><mainClass>com.example.App</mainClass> <!-- 指定主类 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix></manifest></archive></configuration>
</plugin>
- 测试插件:
maven-surefire-plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.4</version>
</plugin>
第六章:依赖管理高级特性
6.1 依赖传递性
Maven的依赖具有传递性,当引入一个依赖时,它所依赖的库也会被自动引入。
依赖传递规则:
- 路径最近者优先:直接依赖优先于传递依赖
- 声明顺序优先:当依赖路径长度相同时,在POM中声明较早的依赖优先
6.2 排除依赖
当传递依赖引起冲突或不需要某些依赖时,可以排除它们:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><!-- 排除Spring Boot默认的日志框架 --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions>
</dependency>
6.3 依赖版本管理
使用dependencyManagement
统一管理依赖版本:
<dependencyManagement><dependencies><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.20.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.20.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.20</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId><version>2.20.0</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.17</version></dependency></dependencies>
</dependencyManagement><!-- 实际引用时不需要指定版本 -->
<dependencies><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
</dependencies>
6.4 可选依赖
当希望某个依赖不被传递时,可以设置为可选:
<dependency><groupId>com.example</groupId><artifactId>optional-feature</artifactId><version>1.0.0</version><optional>true</optional> <!-- 设为可选依赖 -->
</dependency>
第七章:Maven仓库
7.1 仓库类型
Maven有三种类型的仓库:
- 本地仓库:位于开发者本地的仓库,默认路径为
~/.m2/repository
- 中央仓库:Maven官方维护的远程仓库,包含大部分常用的开源库
- 私有仓库:企业或组织内部搭建的仓库,用于管理内部组件和第三方库
7.2 配置远程仓库
在POM中配置远程仓库:
<repositories><repository><id>aliyun</id><name>Aliyun Repository</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled> <!-- 启用发布版本下载 --></releases><snapshots><enabled>false</enabled> <!-- 禁用快照版本下载 --></snapshots></repository><!-- 配置Spring仓库 --><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository>
</repositories>
对于插件仓库,需要单独配置:
<pluginRepositories><pluginRepository><id>aliyun-plugin</id><name>Aliyun Plugin Repository</name><url>https://maven.aliyun.com/repository/public</url></pluginRepository>
</pluginRepositories>
7.3 部署到远程仓库
配置部署信息:
<distributionManagement><!-- 发布版本仓库 --><repository><id>releases</id><name>Internal Releases</name><url>https://repo.example.com/releases</url></repository><!-- 快照版本仓库 --><snapshotRepository><id>snapshots</id><name>Internal Snapshots</name><url>https://repo.example.com/snapshots</url></snapshotRepository>
</distributionManagement>
然后在settings.xml
中配置仓库认证信息:
<servers><server><id>releases</id><username>your-username</username><password>your-password</password></server><server><id>snapshots</id><username>your-username</username><password>your-password</password></server>
</servers>
执行部署命令:
mvn clean deploy
第八章:Maven多模块项目
8.1 多模块项目结构
大型项目通常拆分为多个模块,共享依赖和配置:
parent-project/
├── pom.xml # 父POM
├── module-common/ # 公共模块
│ └── pom.xml
├── module-service/ # 服务模块
│ └── pom.xml
└── module-web/ # Web模块└── pom.xml
8.2 创建父POM
<?xml version="1.0" encoding="UTF-8"?>
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging> <!-- 父项目必须是pom类型 --><name>Parent Project</name><!-- 声明子模块 --><modules><module>module-common</module><module>module-service</module><module>module-web</module></modules><!-- 统一管理依赖版本 --><dependencyManagement><dependencies><!-- 公共依赖 --><dependency><groupId>com.example</groupId><artifactId>module-common</artifactId><version>${project.version}</version></dependency><!-- 其他依赖版本... --></dependencies></dependencyManagement><!-- 所有子模块共享的依赖 --><dependencies><!-- 例如:日志框架 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.17</version></dependency></dependencies>
</project>
8.3 子模块POM
以module-web
为例:
<?xml version="1.0" encoding="UTF-8"?>
<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"><!-- 继承父项目 --><parent><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>module-web</artifactId><packaging>war</packaging> <!-- Web模块打包为war --><name>Web Module</name><!-- 依赖其他模块 --><dependencies><dependency><groupId>com.example</groupId><artifactId>module-common</artifactId></dependency><dependency><groupId>com.example</groupId><artifactId>module-service</artifactId></dependency><!-- Web相关依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>
8.4 多模块项目构建
在父项目目录下执行命令,会自动按依赖顺序构建所有模块:
# 构建所有模块
mvn clean package# 只构建特定模块(会自动构建其依赖的模块)
mvn clean package -pl module-web -am# 跳过测试构建所有模块
mvn clean package -DskipTests
第九章:Maven Profiles
9.1 什么是Profiles
Profiles允许你为不同环境(如开发、测试、生产)定义不同的配置,实现环境隔离。
9.2 定义Profiles
在POM中定义:
<profiles><!-- 开发环境 --><profile><id>dev</id><activation><activeByDefault>true</activeByDefault> <!-- 默认激活 --></activation><properties><env>development</env><db.url>jdbc:mysql://localhost:3306/dev_db</db.url></properties><dependencies><!-- 开发环境特有依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency></dependencies></profile><!-- 生产环境 --><profile><id>prod</id><properties><env>production</env><db.url>jdbc:mysql://prod-db:3306/prod_db</db.url></properties><build><plugins><!-- 生产环境特有插件配置 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.6.0</version><!-- 配置... --></plugin></plugins></build></profile>
</profiles>
9.3 激活Profiles
- 在命令行激活:
# 激活prod profile
mvn clean package -Pprod# 同时激活多个profile
mvn clean package -Pdev,test
- 在settings.xml中配置默认激活的profile:
<activeProfiles><activeProfile>dev</activeProfile>
</activeProfiles>
- 根据环境变量激活:
<profile><id>env-based</id><activation><property><name>env</name><value>linux</value></property></activation><!-- 配置... -->
</profile>
第十章:Maven与IDE集成
10.1 IntelliJ IDEA集成
IntelliJ IDEA内置了对Maven的完美支持:
-
导入Maven项目:
- 选择
File > Open
,然后选择项目的pom.xml
文件 - 选择"Open as Project"
- 选择
-
常用Maven操作:
- 在右侧的"Maven"工具窗口中可以看到所有Maven命令
- 右键点击命令可以执行,或创建快捷方式
-
配置Maven:
- 打开
File > Settings > Build, Execution, Deployment > Build Tools > Maven
- 可以指定Maven安装目录、settings.xml位置和本地仓库位置
- 打开
10.2 Eclipse集成
Eclipse通过M2Eclipse插件支持Maven:
-
安装M2Eclipse插件:
- Eclipse通常已预装,如果没有,通过
Help > Eclipse Marketplace
搜索"Maven"安装
- Eclipse通常已预装,如果没有,通过
-
导入Maven项目:
- 选择
File > Import > Maven > Existing Maven Projects
- 选择项目根目录,点击"Finish"
- 选择
-
常用Maven操作:
- 右键点击项目,选择
Run As > Maven build...
- 可以输入Maven命令执行
- 右键点击项目,选择
第十一章:Maven高级技巧
11.1 资源过滤
根据不同环境过滤资源文件:
<build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering> <!-- 启用过滤 --><includes><include>**/*.properties</include><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources-${env}</directory> <!-- 环境特定资源 --><filtering>true</filtering></resource></resources>
</build>
资源文件中可以使用变量:
# application.properties
app.name=${project.name}
app.version=${project.version}
db.url=${db.url}
11.2 构建编号与版本管理
使用buildnumber-maven-plugin
生成构建编号:
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>buildnumber-maven-plugin</artifactId><version>3.2.1</version><executions><execution><id>generate-buildnumber</id><phase>validate</phase><goals><goal>create</goal></goals></execution></executions><configuration><format>{0,date,yyyy-MM-dd HH:mm:ss}</format> <!-- 日期时间格式 --><items><item>timestamp</item></items></configuration>
</plugin>
11.3 生成可执行JAR
使用maven-assembly-plugin
生成包含所有依赖的可执行JAR:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.7.1</version><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><mainClass>com.example.App</mainClass> <!-- 指定主类 --></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions>
</plugin>
11.4 自定义Archetype
创建自己的项目模板:
- 创建一个示例项目,然后执行:
mvn archetype:create-from-project
- 进入生成的archetype目录:
cd target/generated-sources/archetype
- 安装到本地仓库:
mvn install
- 使用自定义archetype创建项目:
mvn archetype:generate -DarchetypeGroupId=com.example -DarchetypeArtifactId=my-archetype -DarchetypeVersion=1.0.0
第十二章:Maven最佳实践
12.1 项目组织
- 遵循标准的Maven项目结构
- 合理拆分多模块项目,保持模块职责单一
- 使用父POM统一管理依赖版本和插件配置
12.2 依赖管理
- 谨慎使用依赖范围,避免不必要的依赖
- 定期检查并更新依赖版本,修复安全漏洞
- 使用
dependencyManagement
统一管理版本,避免版本冲突 - 排除不需要的传递依赖
12.3 构建优化
- 合理配置本地仓库位置,避免放在系统盘
- 使用国内镜像加速依赖下载
- 适当使用
mvn dependency:go-offline
提前下载依赖,加速离线构建 - 对于大型项目,考虑使用增量构建
12.4 版本管理
- 遵循语义化版本规范(Major.Minor.Patch)
- 使用SNAPSHOT版本标识开发中的版本
- 发布版本使用RELEASE标识,一旦发布不允许修改
- 使用版本管理工具(如versions-maven-plugin)管理版本号
12.5 持续集成
- 在CI/CD流程中集成Maven构建
- 构建时指定
-B
参数启用批处理模式 - 构建过程中运行所有测试,并生成测试报告
- 配置构建缓存,加速CI环境中的构建
结语
Maven作为Java生态系统中最流行的构建工具之一,极大地简化了项目构建和依赖管理流程。本教程涵盖了Maven的核心概念、基本用法和高级特性,希望能帮助你更好地理解和使用Maven。
随着技术的发展,Maven也在不断演进,建议定期查看官方文档,了解最新特性和最佳实践。同时,也可以关注Gradle等其他构建工具,选择最适合你项目需求的工具。
祝你在Maven的使用过程中取得高效的开发体验!