引言

Maven是Java项目的标准构建工具。本文介绍Maven的核心功能和实战应用。

一、Maven核心概念

1.1 坐标(Coordinates)

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.0</version>
</dependency>

1.2 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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- 依赖配置 -->
</dependencies>

<build>
<plugins>
<!-- 插件配置 -->
</plugins>
</build>
</project>

二、依赖管理

2.1 依赖范围

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
<!-- compile:默认,编译和运行都需要 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
<scope>compile</scope>
</dependency>

<!-- provided:编译需要,运行时由容器提供 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- test:仅测试时需要 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- runtime:运行时需要 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
<scope>runtime</scope>
</dependency>

2.2 依赖传递

1
2
3
4
5
6
7
8
9
10
11
<!-- 排除传递依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

2.3 依赖管理(父POM)

1
2
3
4
5
6
7
8
9
10
11
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

三、生命周期

3.1 三套生命周期

1
2
3
4
5
6
7
8
9
10
11
12
# clean生命周期
mvn clean

# default生命周期
mvn compile # 编译
mvn test # 测试
mvn package # 打包
mvn install # 安装到本地仓库
mvn deploy # 部署到远程仓库

# site生命周期
mvn site

3.2 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 清理并打包
mvn clean package

# 跳过测试打包
mvn clean package -DskipTests

# 安装到本地仓库
mvn clean install

# 运行Spring Boot项目
mvn spring-boot:run

# 查看依赖树
mvn dependency:tree

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

四、插件配置

4.1 编译插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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>
</configuration>
</plugin>
</plugins>
</build>

4.2 Spring Boot插件

1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

五、多模块项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 父POM -->
<packaging>pom</packaging>
<modules>
<module>module-common</module>
<module>module-dao</module>
<module>module-service</module>
<module>module-web</module>
</modules>

<!-- 子模块 -->
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>

总结

Maven核心:

  1. 依赖管理:统一管理项目依赖
  2. 生命周期:标准化构建流程
  3. 插件系统:扩展构建功能
  4. 仓库管理:本地和远程仓库

参考资料