开发基础知识 - Maven

Background

Java 编译,打包,运行

Maven介绍

Maven 是一个项目管理和构建工具,主要用于 Java 项目。

Why we need maven

  • 我们写 Java 项目时经常需要用到第三方库,比如数据库驱动、日志工具等等,每个库有版本,可能还需要这些库的库。如果我们自己去找 JAR 文件、下载、放入项目、管理版本,很容易出错。
  • 把一个项目从代码变成可以运行的程序,中间有很多步骤:编译、打包、运行测试、生成文档、部署等。每个步骤手动操作非常烦人而且容易出错。

使用Maven后,只需要在 pom.xml 文件中写明需要的库,Maven 会自动从中央仓库下载正确的版本,并配置好路径。

Maven的组成

  1. pom.xml
    pom文件是项目的配置文件。pom文件包含以下信息:
  • 项目的基本信息(名字、版本)
  • 项目需要哪些依赖(比如 Spring、MySQL 驱动)
  • 项目如何构建(比如插件、打包方式)
  • 项目是否有子模块(如果是父模块)
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
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<!-- 项目身份标识 -->
<groupId>com.example</groupId> <!-- 谁开发的项目(通常是组织/公司/包名) -->
<artifactId>demo-project</artifactId> <!-- 项目的名字(模块名) -->
<version>1.0.0</version> <!-- 版本号 -->
<packaging>jar</packaging>

<name>Demo Maven Project</name>
<description>A simple Maven project example</description>
<!-- 依赖 -->
<dependencies>
<!-- 添加一个第三方库,例如 Jackson 处理 JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
<!-- 示例依赖:JUnit 用于测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope> <!-- 仅用于测试阶段 -->
</dependency>
</dependencies>

</project>

以下三个标识定义一个资源

  • groupId:谁开发的项目(通常是组织/公司/包名)
  • artifactId:项目的名字(模块名)
  • version:版本号
  1. 本地仓库
    Maven 会把下载的依赖包保存在你电脑上的一个位置(通常在 ~/.m2/repository 里),下次用同样的依赖就不需要重新下载。

  2. 中央仓库
    如果本地没有你需要的库,Maven 会自动从网上的中央仓库去下载。

  3. 私有仓库
    私有仓库用于公司内部不希望公开的jar包,只需要在本地的~/.m2/settings.xml配置好私有仓库的访问,就可以使用。

  4. 生命周期和插件
    Maven定义了统一的构建流程,并且用命令自动化执行:

  • mvn clean:删除上一次构建的结果(.class和.jar文件),保证干净的开始
  • mvn compile:编译 src/main/java 源代码到 /target 目录下。但它不会打包成 jar,会生成 .class文件。
  • mvn test:编译并运行单元测试
  • mvn package:编译、测试并打包项目,生成.jar文件,默认打包的不是 fat jar。
  • mvn install:打包后安装到本地仓库(~/.m2/repository/),供其他项目使用

How maven works

你声明一个依赖后,Maven 会自动下载它所依赖的所有库(传递依赖),一直递归下去,直到依赖链的最底层,全自动完成。

Maven模块管理

实际中,我们通常有很多服务(模块),文件结构:

1
2
3
4
5
6
7
8
my-mall/
├── pom.xml <-- 父模块
├── common-utils/ <-- 子模块
│ └── pom.xml
├── user-service/ <-- 子模块
│ └── pom.xml
└── order-service/ <-- 子模块
└── pom.xml

父模块 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
28
29
30
31
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-mall</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <!-- 父模块不能打包成 jar -->

<!-- 所有子模块声明 -->
<modules>
<module>common-utils</module>
<module>user-service</module>
<module>order-service</module>
</modules>

<!-- 统一配置管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.27</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<!-- 编译插件 -->
</plugins>
</build>
</project>

子模块 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
28
<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>

<!-- 继承父项目 -->
<parent>
<groupId>com.example</groupId>
<artifactId>my-mall</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>common-utils</artifactId>
<packaging>jar</packaging> <!-- 打包成jar -->

<name>Common Utils Module</name>

<dependencies>
<!-- 引用父模块统一管理的 spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>

</project>
  • dependencies
    使用<dependency>声明一个依赖后,Maven就会自动下载这个依赖包并把它放到classpath中。

  • dependencyManagement
    在大型项目中,多个模块可能使用同一个库(如 Spring Boot),如果每个模块都自己写依赖版本,可能会产生冲突。dependencyManagement是Maven用来统一定义依赖版本的一个区域,子模块继承后,只写 groupId 和 artifactId 即可。

  • modules
    在一个父项目中,<modules> 标签用来列出所有子模块名称。

  • profiles
    profiles是Maven的一种机制,用来根据不同的环境或目标,启用不同的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<profiles>
<profile>
<id>dev</id>
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/dev_db</jdbc.url>
</properties>
</profile>

<profile>
<id>prod</id>
<properties>
<jdbc.url>jdbc:mysql://prod-db:3306/prod_db</jdbc.url>
</properties>
</profile>
</profiles>

以上例子在不同环境切换不同的数据库。运行时启用某个profile: mvn clean package -Pprod

  • build
    <build> 是Maven项目中用来控制如何生成.jar包的区域.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<build>
<!-- 设置输出 jar 文件名 -->
<finalName>my-awesome-app</finalName>

<!-- 设置构建插件 -->
<plugins>
<plugin>
<!-- 设置 Java 版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

开发基础知识 - Maven
https://thiefcat.github.io/2025/07/01/SWE-basic/tools/maven/
Author
小贼猫
Posted on
July 1, 2025
Licensed under