Maven 概述

什么是 Maven

Maven 是一个基于项目对象模型(POM)的项目管理工具,主要用于 Java 项目的构建依赖管理和项目管理

  • 核心功能
    • 项目构建:编译测试打包部署
    • 依赖管理:自动下载和管理第三方库
    • 项目信息管理:生成项目文档站点
  • 主要优势
    • 约定优于配置:标准化的项目结构
    • 依赖传递:自动处理依赖关系
    • 插件体系:丰富的扩展能力
    • 多模块支持:大型项目模块化开发

Maven 的核心概念

项目对象模型(POM)

POM(Project Object Model)是 Maven 项目的核心配置文件,描述了项目的基本信息依赖构建配置等

1
2
3
4
5
6
7
8
<!-- pom.xml 基本结构 -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
</project>

坐标系统

Maven 使用三个坐标唯一标识一个项目:

  • groupId:组织或项目的唯一标识,通常采用域名反写
  • artifactId:项目名称或模块名称
  • version:项目版本号

示例:com.google.guava:guava:31.1-jre

仓库类型

  • 本地仓库(Local Repository)
    • 位于用户目录下的 .m2/repository
    • 存储从远程仓库下载的依赖
  • 中央仓库(Central Repository)
    • Maven 官方维护的公共仓库
    • 包含绝大多数开源项目
  • 远程仓库(Remote Repository)
    • 公司或组织私有的仓库
    • 如 NexusArtifactory

Maven 的工作原理

构建生命周期

Maven 有三个内置的构建生命周期:

  • clean:清理项目
    • pre-clean:执行清理前的工作
    • clean:删除之前构建生成的文件
    • post-clean:执行清理后的工作
  • default(build):构建项目
    • 包含 compiletestpackageinstalldeploy 等阶段
  • site:生成项目站点
    • 生成项目文档和报告

构建阶段与插件目标

每个生命周期由多个阶段组成,每个阶段绑定到插件的目标(goal)

1
2
3
4
5
6
7
8
9
10
11
典型构建流程:
validate → compile → test → package → verify → install → deploy

常用阶段说明:
- validate:验证项目是否正确
- compile:编译源代码
- test:运行单元测试
- package:打包(jar/war)
- verify:运行集成测试
- install:安装到本地仓库
- deploy:部署到远程仓库

依赖解析机制

1
2
3
4
5
6
7
8
9
依赖查找顺序:
1. 本地仓库
2. 远程仓库(按配置顺序)
3. 中央仓库

依赖传递规则:
- 最短路径优先原则
- 最先声明优先原则
- 可选依赖不会被传递

安装配置

安装 Maven

环境要求

  • JDK 1.7+(Maven 3.3+ 需要 JDK 1.7+)
  • 操作系统:WindowsLinuxmacOS

Windows 安装

下载地址:https://maven.apache.org/download.cgi

步骤:

  1. 下载压缩包并解压到指定目录(如 D:\apache-maven-3.9.6)
  2. 配置环境变量:
    1
    2
    3
    4
    5
    # 添加 MAVEN_HOME
    MAVEN_HOME=D:\apache-maven-3.9.6

    # 添加到 PATH
    %MAVEN_HOME%\bin
  3. 验证安装:
    1
    mvn -version

Linux/macOS 安装

1
2
3
4
5
6
7
8
9
10
11
12
# 使用包管理器安装
# Ubuntu/Debian
sudo apt-get install maven

# CentOS/RHEL
sudo yum install maven

# macOS(使用 Homebrew)
brew install maven

# 验证安装
mvn -version

手动安装(通用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 下载 Maven
wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz

# 2. 解压
tar -xzf apache-maven-3.9.6-bin.tar.gz
sudo mv apache-maven-3.9.6 /opt/maven

# 3. 配置环境变量
echo 'export MAVEN_HOME=/opt/maven' >> ~/.bashrc
echo 'export PATH=$MAVEN_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

# 4. 验证
mvn -version

初始配置

Maven 的配置文件位于 ${MAVEN_HOME}/conf/settings.xml,用户级别的配置位于 ~/.m2/settings.xml

配置本地仓库

1
2
3
4
5
<!-- settings.xml -->
<settings>
<!-- 修改本地仓库位置 -->
<localRepository>D:/maven-repository</localRepository>
</settings>

配置镜像加速

1
2
3
4
5
6
7
8
9
<!-- 使用阿里云镜像加速 -->
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>

配置 JDK 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- 方式一:在 settings.xml 中配置 -->
<profiles>
<profile>
<id>jdk-11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
</profile>
</profiles>

<!-- 方式二:在 pom.xml 中配置(推荐) -->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

常用配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!-- 完整的 settings.xml 配置示例 -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
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>/path/to/local/repo</localRepository>

<!-- 交互式输入 -->
<interactiveMode>true</interactiveMode>

<!-- 离线模式 -->
<offline>false</offline>

<!-- 插件组 -->
<pluginGroups>
<pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>

<!-- 服务器认证信息 -->
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>

<!-- 镜像配置 -->
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>*</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>

<!-- 代理配置 -->
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.example.com</host>
<port>8080</port>
</proxy>
</proxies>

<!-- 配置文件 -->
<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
</properties>
</profile>
</profiles>

<!-- 激活的配置 -->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
</settings>

基本操作

创建项目

使用 archetype 创建项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 交互式创建项目
mvn archetype:generate

# 非交互式创建(快速原型)
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

# 创建 Web 项目
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-webapp \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false


# 常用 archetype:
# - maven-archetype-quickstart:简单 Java 应用
# - maven-archetype-webapp:Web 应用
# - maven-archetype-site-simple:简单站点

手动创建项目结构

1
2
3
4
5
6
7
8
9
10
11
12
# 标准 Maven 项目结构
my-project/
├── src/
│ ├── main/
│ │ ├── java/ # Java 源代码
│ │ ├── resources/ # 资源文件
│ │ └── webapp/ # Web 资源(WAR 项目)
│ └── test/
│ ├── java/ # 测试代码
│ └── resources/ # 测试资源
├── target/ # 构建输出目录
└── pom.xml # 项目配置文件

构建项目

常用构建命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 清理项目
mvn clean

# 编译项目
mvn compile

# 运行测试
mvn test

# 打包项目
mvn package

# 安装到本地仓库
mvn install

# 部署到远程仓库
mvn deploy

# 生成项目站点
mvn site


# 组合命令
mvn clean compile
mvn clean package
mvn clean install
mvn clean deploy

跳过测试

1
2
3
4
5
# 跳过测试执行
mvn clean package -DskipTests

# 跳过测试编译和执行
mvn clean package -Dmaven.test.skip=true

查看项目信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看项目依赖树
mvn dependency:tree

# 查看有效 POM
mvn help:effective-pom

# 查看有效设置
mvn help:effective-settings

# 查看插件信息
mvn help:describe -Dplugin=compiler

# 查看项目帮助信息
mvn help:help

# 分析依赖冲突
mvn dependency:analyze

清理项目

1
2
3
4
5
6
7
8
9
10
11
12
13
# 清理 target 目录
mvn clean

# 清理并重新构建
mvn clean compile

# 清理编译测试打包
mvn clean package


# 细节注意:
# 1. clean 会删除 target 目录
# 2. 建议在每次构建前执行 clean,确保构建环境干净

依赖管理

依赖声明

pom.xml 中使用 <dependencies> 标签声明依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
<!-- 基本依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>

依赖范围(Scope)

Scope 编译 测试 运行 示例
compile spring-core(默认)
provided servlet-api
runtime JDBC 驱动
test junit
system 本地 JAR(不推荐)
1
2
3
4
5
细节注意:
1. compile 是默认范围,适用于大多数情况
2. provided 用于容器提供的依赖(如 Tomcat 提供的 servlet-api)
3. test 仅用于测试阶段
4. 避免使用 system 范围,会导致不可移植

依赖传递

Maven 会自动解析依赖的依赖(transitive dependencies)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- A 依赖 B,B 依赖 C -->
<!-- A 的 pom.xml -->
<dependency>
<groupId>com.example</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
</dependency>

<!-- B 的 pom.xml -->
<dependency>
<groupId>com.example</groupId>
<artifactId>C</artifactId>
<version>1.0</version>
</dependency>

<!-- 结果:A 会自动依赖 C -->

排除依赖

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>com.example</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<exclusions>
<!-- 排除不需要的传递依赖 -->
<exclusion>
<groupId>com.example</groupId>
<artifactId>C</artifactId>
</exclusion>
</exclusions>
</dependency>

依赖调解

当出现依赖冲突时,Maven 使用以下规则:

  • 最短路径优先:依赖路径越短优先级越高
  • 最先声明优先:路径长度相同时,先声明的优先
1
2
3
4
5
# 查看依赖树,分析冲突
mvn dependency:tree

# 查看详细依赖信息
mvn dependency:tree -Dverbose

可选依赖

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>com.example</groupId>
<artifactId>optional-lib</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>


# 细节注意:
# 1. 可选依赖不会被传递
# 2. 使用者需要显式声明该依赖才能使用
# 3. 适用于提供多种实现方案的库

依赖管理(Dependency Management)

在父 POM 中统一管理依赖版本,子模块继承时不需要指定版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!-- 父 POM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
</dependency>
</dependencies>
</dependencyManagement>

<!-- 子模块 POM -->
<dependencies>
<!-- 不需要指定 version -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

插件管理

常用插件

插件 功能 常用目标
maven-compiler-plugin 编译 Java 代码 compile, testCompile
maven-surefire-plugin 运行单元测试 test
maven-jar-plugin 打包 JAR 文件 jar
maven-war-plugin 打包 WAR 文件 war
maven-assembly-plugin 自定义打包 single
maven-shade-plugin 创建可执行 JAR shade
maven-source-plugin 生成源码包 jar-no-fork
maven-javadoc-plugin 生成 Javadoc jar

插件配置

编译器插件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

Surefire 插件配置(测试)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<!-- 跳过测试 -->
<skipTests>false</skipTests>
<!-- 包含的测试类 -->
<includes>
<include>**/*Test.java</include>
</includes>
<!-- 排除的测试类 -->
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
<!-- 并行执行测试 -->
<parallel>methods</parallel>
<threadCount>4</threadCount>
</configuration>
</plugin>

Shade 插件配置(可执行 JAR)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

插件管理(Plugin Management)

在父 POM 中统一管理插件版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 父 POM -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!-- 子模块 POM -->
<build>
<plugins>
<!-- 不需要指定 version -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

多模块项目

项目结构

1
2
3
4
5
6
7
8
9
10
11
parent-project/
├── pom.xml # 父 POM
├── module-a/
│ ├── pom.xml # 子模块 A
│ └── src/
├── module-b/
│ ├── pom.xml # 子模块 B
│ └── src/
└── module-c/
├── pom.xml # 子模块 C
└── src/

父 POM 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<project>
<modelVersion>4.0.0</modelVersion>
<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>
<module>module-c</module>
</modules>

<!-- 统一管理依赖版本 -->
<dependencyManagement>
<dependencies>
<!-- 共享依赖 -->
</dependencies>
</dependencyManagement>

<!-- 统一管理插件版本 -->
<build>
<pluginManagement>
<plugins>
<!-- 共享插件 -->
</plugins>
</pluginManagement>
</build>
</project>

子模块配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<project>
<modelVersion>4.0.0</modelVersion>

<!-- 继承父 POM -->
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>module-a</artifactId>
<packaging>jar</packaging>

<!-- 子模块依赖 -->
<dependencies>
<!-- 可以依赖其他子模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>module-b</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

模块间依赖

1
2
3
4
5
6
7
8
9
10
依赖关系示例:
module-a 依赖 module-b
module-c 依赖 module-a 和 module-b

构建顺序:
1. module-b(无依赖)
2. module-a(依赖 module-b)
3. module-c(依赖 module-a 和 module-b)

Maven 会根据依赖关系自动确定构建顺序

生命周期与阶段

完整生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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:部署站点

绑定插件目标到阶段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>custom-task</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<!-- 配置内容 -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

自定义生命周期

通过插件可以实现自定义任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>custom-script</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>scripts/custom.sh</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>

profiles 配置

Profile 的作用

Profile 允许为不同环境定义不同的配置,如开发测试生产环境

定义 Profile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
<db.url>jdbc:mysql://localhost:3306/dev</db.url>
<db.username>dev_user</db.username>
<db.password>dev_pass</db.password>
</properties>
</profile>

<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<env>testing</env>
<db.url>jdbc:mysql://test-server:3306/test</db.url>
<db.username>test_user</db.username>
<db.password>test_pass</db.password>
</properties>
</profile>

<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<env>production</env>
<db.url>jdbc:mysql://prod-server:3306/prod</db.url>
<db.username>prod_user</db.username>
<db.password>prod_pass</db.password>
</properties>
</profile>
</profiles>

激活 Profile

1
2
3
4
5
6
7
8
9
10
11
12
13
# 命令行激活
mvn clean package -P prod

# 激活多个 profile
mvn clean package -P dev,debug

# 通过属性激活
mvn clean package -Denv=prod

# 在 settings.xml 中激活
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>

Profile 激活条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<profile>
<id>jdk-11</id>
<activation>
<!-- JDK 版本 -->
<jdk>11</jdk>

<!-- 操作系统 -->
<os>
<name>Windows 10</name>
<family>Windows</family>
<arch>x86_64</arch>
<version>10.0</version>
</os>

<!-- 文件存在 -->
<file>
<exists>config.properties</exists>
<missing>debug.properties</missing>
</file>

<!-- 属性值 -->
<property>
<name>environment</name>
<value>production</value>
</property>
</activation>
</profile>

使用 Profile 属性

1
2
3
4
5
6
7
8
9
10
<!-- 在 pom.xml 中使用 profile 定义的属性 -->
<properties>
<database.url>${db.url}</database.url>
<database.username>${db.username}</database.username>
</properties>

<!-- 在资源文件中使用 -->
<!-- application.properties -->
db.url=${db.url}
db.username=${db.username}

资源过滤

资源文件处理

Maven 可以过滤资源文件,替换其中的占位符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</build>

过滤示例

1
2
3
4
# src/main/resources/application.properties
app.name=${project.name}
app.version=${project.version}
db.url=${db.url}
1
2
3
4
5
6
<!-- pom.xml 中定义属性 -->
<properties>
<db.url>jdbc:mysql://localhost:3306/mydb</db.url>
</properties>

<!-- 构建后,application.properties 中的占位符会被替换 -->

发布管理

配置发布仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<distributionManagement>
<!-- 发布版本仓库 -->
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://nexus.example.com/repository/maven-releases/</url>
</repository>

<!-- 快照版本仓库 -->
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

配置服务器认证

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- settings.xml -->
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>

执行部署

1
2
3
4
5
6
7
8
9
10
11
# 部署到远程仓库
mvn deploy

# 清理后部署
mvn clean deploy


# 细节注意:
# 1. 快照版本(SNAPSHOT)可以重复部署
# 2. 发布版本(Release)不能重复部署相同版本
# 3. 建议使用 CI/CD 工具自动化部署

最佳实践

项目结构规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
推荐的项目结构:
project/
├── src/
│ ├── main/
│ │ ├── java/
│ │ ├── resources/
│ │ │ ├── config/
│ │ │ ├── i18n/
│ │ │ └── static/
│ │ └── webapp/
│ └── test/
│ ├── java/
│ └── resources/
├── docs/ # 文档
├── scripts/ # 脚本
├── pom.xml
├── README.md
└── .gitignore

版本管理规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
语义化版本(Semantic Versioning):
- MAJOR.MINOR.PATCH(主版本.次版本.修订版本)
- 主版本:不兼容的 API 修改
- 次版本:向下兼容的功能性新增
- 修订版本:向下兼容的问题修正

示例:
- 1.0.0:初始稳定版本
- 1.0.1:修复 bug
- 1.1.0:新增功能
- 2.0.0:重大变更

快照版本:
- 1.0.0-SNAPSHOT:开发中的版本
- 正式发布时去掉 -SNAPSHOT

依赖管理最佳实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!-- 1. 使用 dependencyManagement 统一管理版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.3.20</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- 2. 避免硬编码版本号,使用属性 -->
<properties>
<spring.version>5.3.20</spring.version>
<junit.version>4.13.2</junit.version>
</properties>

<!-- 3. 定期更新依赖,修复安全漏洞 -->
<!-- 检查依赖更新 -->
mvn versions:display-dependency-updates

<!-- 检查插件更新 -->
mvn versions:display-plugin-updates

插件配置最佳实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 1. 在父 POM 中使用 pluginManagement 统一管理插件版本 -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!-- 2. 明确指定插件版本,避免使用最新版本 -->
<!-- 3. 合理配置插件参数,提高构建效率 -->

构建优化

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 并行构建(多模块项目)
mvn clean install -T 4 # 使用 4 个线程
mvn clean install -T 1C # 每个 CPU 核心一个线程

# 2. 离线模式(依赖已下载)
mvn clean install -o

# 3. 只构建特定模块
mvn clean install -pl module-a -am

# 4. 跳过不必要的步骤
mvn clean install -DskipTests -Dmaven.javadoc.skip=true

常见问题

依赖冲突解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1. 查看依赖树
mvn dependency:tree

# 2. 查看依赖冲突
mvn dependency:tree -Dverbose | grep "conflict"

# 3. 排除冲突的依赖
<exclusions>
<exclusion>
<groupId>conflict-group</groupId>
<artifactId>conflict-artifact</artifactId>
</exclusion>
</exclusions>

# 4. 强制使用特定版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>conflict-group</groupId>
<artifactId>conflict-artifact</artifactId>
<version>desired-version</version>
</dependency>
</dependencies>
</dependencyManagement>

下载速度慢

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 配置国内镜像 -->
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<mirror>
<id>tencent</id>
<mirrorOf>central</mirrorOf>
<url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
</mirror>
</mirrors>

内存不足

1
2
3
4
5
# 设置 Maven 内存
export MAVEN_OPTS="-Xms512m -Xmx2048m"

# Windows
set MAVEN_OPTS=-Xms512m -Xmx2048m

编码问题

1
2
3
4
5
<!-- 在 pom.xml 中配置编码 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

报错处理

💗💗 Maven 报错:Could not resolve dependencies

1
2
3
4
5
6
7
8
9
10
11
12
错误原因:
1. 依赖不存在或版本错误
2. 网络问题无法下载
3. 仓库配置错误

解决方案:
1. 检查依赖坐标是否正确
2. 检查网络连接和仓库配置
3. 清除本地缓存后重试
rm -rf ~/.m2/repository/group/artifact
mvn clean install
4. 检查是否需要认证信息

💗💗 Maven 报错:Failed to execute goal

1
2
3
4
5
6
7
8
9
10
错误原因:
1. 插件配置错误
2. JDK 版本不匹配
3. 项目结构不符合规范

解决方案:
1. 检查插件版本和配置
2. 确认 JDK 版本符合要求
3. 检查项目结构是否符合 Maven 规范
4. 查看详细错误信息:mvn clean install -X

💗💗 Maven 报错:SSL certificate problem

1
2
3
4
5
6
解决方案:
# 忽略 SSL 证书验证(不推荐,仅用于测试)
mvn clean install -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true

# 更好的方案:导入证书到 Java 信任库
keytool -importcert -alias nexus -file cert.pem -keystore $JAVA_HOME/lib/security/cacerts

学习资源

  • 视频
    • 尚硅谷Maven教程https://www.bilibili.com/video/BV12q4y147e4
  • 官方文档
    • Maven 官方文档:https://maven.apache.org/guides/index.html
    • Maven Central 搜索:https://search.maven.org/
  • 书籍
    • Maven 实战:许晓斌著
    • Maven 权威指南:Sonatype 著
  • 教程
    • Maven 入门教程:https://www.runoob.com/maven/maven-tutorial.html
    • Baeldung Maven 教程:https://www.baeldung.com/category/maven/
  • 工具
    • Maven 版本管理插件:versions-maven-plugin
    • 依赖分析工具:maven-dependency-plugin
  • 社区
    • Stack Overflow Maven 标签:https://stackoverflow.com/questions/tagged/maven
    • Maven 邮件列表:https://maven.apache.org/mailing-lists.html