目录
前言
Maven 是 Java 生态中最流行的项目管理和构建工具之一。它通过 pom.xml 文件管理项目的依赖、构建生命周期和插件配置。本文将从基础到高级,全面复习 Maven 的用法,并提供详细的代码示例和注意事项。同时,我们将深入探讨 dependencyManagement 标签的作用,并解释 Maven 中所有重要标签的用法。
1. 基础用法
1.1 Maven 安装与配置
1.3 pom.xml 基础配置
1.4 常用命令
2. 进阶用法
2.1 依赖管理
2.2 插件配置
2.3 多模块项目
3. 高级用法
3.1 自定义生命周期
3.2 使用 Profile
3.4 使用自定义仓库
4. Maven 标签详解
4.1 核心标签
4.2 代码示例
5. 注意事项
前言
Maven 是 Java 生态中最流行的项目管理和构建工具之一。它通过 pom.xml
文件管理项目的依赖、构建生命周期和插件配置。本文将从基础到高级,全面复习 Maven 的用法,并提供详细的代码示例和注意事项。同时,我们将深入探讨 dependencyManagement
标签的作用,并解释 Maven 中所有重要标签的用法。
1. 基础用法
1.1 Maven 安装与配置
- 下载 Maven:
- 从 Maven 官网 下载最新版本。
- 配置环境变量:
- 将 Maven 的
bin
目录添加到系统的PATH
环境变量中。 - 示例(Linux/Mac):
- 将 Maven 的
export PATH=$PATH:/path/to/maven/bin
-
- 示例(Windows):
- 在系统环境变量中添加
PATH
,值为C:\path\to\maven\bin
。
- 在系统环境变量中添加
- 示例(Windows):
- 验证安装:
mvn -v
出现版本号信息即安装配置成功。
1.3 pom.xml
基础配置
pom.xml
是 Maven 的核心配置文件。以下是一个简单的示例:
<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><groupId>com.example</groupId><artifactId>my-app</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>
</project>
1.4 常用命令
命令 | 描述 |
---|---|
mvn clean | 清理项目,删除 target 目录。 |
mvn compile | 编译项目源代码。 |
mvn test | 运行单元测试。 |
mvn package | 打包项目(如生成 JAR 文件)。 |
mvn install | 将项目安装到本地仓库。 |
mvn deploy | 将项目部署到远程仓库。 |
2. 进阶用法
2.1 依赖管理
- 添加依赖: 在
pom.xml
的<dependencies>
部分添加依赖项。例如:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.0</version></dependency>
</dependencies>
- 依赖范围 (
<scope>
):compile
:默认值,编译、测试和运行时都可用。provided
:编译和测试时可用,运行时由容器提供。runtime
:测试和运行时可用,编译时不可用。test
:仅在测试时可用。
- 排除传递依赖: 使用
<exclusions>
排除不需要的传递依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.0</version><exclusions><exclusion><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></exclusion></exclusions>
</dependency>
2.2 插件配置
Maven 插件用于扩展构建功能。以下是一个编译插件的配置示例:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins>
</build>
2.3 多模块项目
- 父
pom.xml
:
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>module1</module><module>module2</module></modules>
</project>
子模块 pom.xml
:
<project><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>module1</artifactId>
</project>
3. 高级用法
3.1 自定义生命周期
Maven 的生命周期包括 clean
、default
和 site
。可以通过插件自定义生命周期阶段。例如:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>3.0.0</version><executions><execution><phase>package</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Custom task during package phase</echo></tasks></configuration></execution></executions></plugin></plugins>
</build>
3.2 使用 Profile
Profile 用于根据环境(如开发、测试、生产)配置不同的构建选项。例如:
<profiles><profile><id>dev</id><properties><env>development</env></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>prod</id><properties><env>production</env></properties></profile>
</profiles>
激活 Profile:
mvn clean install -Pprod
3.3 依赖管理 (<dependencyManagement>
)
<dependencyManagement>
用于统一管理依赖版本,避免版本冲突。它不会实际引入依赖,而是为子模块或依赖项提供版本管理。例如:
<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>
在子模块中使用时,无需指定版本:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.4 使用自定义仓库
如果默认仓库无法满足需求,可以配置自定义仓库:
<repositories><repository><id>custom-repo</id><url>https://example.com/maven-repo</url></repository>
</repositories>
4. Maven 标签详解
4.1 核心标签
标签 | 描述 |
---|---|
<project> | 根标签,定义 POM 文件。 |
<modelVersion> | POM 模型版本,通常为 4.0.0 。 |
<groupId> | 项目组 ID,通常为组织或公司名称。 |
<artifactId> | 项目名称。 |
<version> | 项目版本。 |
<packaging> | 项目打包方式(如 jar 、war 、pom )。 |
<dependencies> | 定义项目依赖。 |
<dependency> | 定义单个依赖项。 |
<scope> | 定义依赖项的作用范围(如 compile 、test )。 |
<exclusions> | 排除传递依赖。 |
<build> | 定义构建配置。 |
<plugins> | 定义插件。 |
<plugin> | 定义单个插件。 |
<properties> | 定义项目属性。 |
<profiles> | 定义构建 Profile。 |
<profile> | 定义单个 Profile。 |
<modules> | 定义多模块项目的子模块。 |
<parent> | 定义父项目。 |
<dependencyManagement> | 统一管理依赖版本。 |
4.2 代码示例
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-app</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build>
</project>
5. 注意事项
- 依赖冲突:
- 使用
mvn dependency:tree
查看依赖树,解决冲突。
- 使用
- 构建性能:
- 避免引入不必要的依赖,减少构建时间。
- 版本管理:
- 使用
<dependencyManagement>
统一管理依赖版本。
- 使用
- 插件兼容性:
- 确保插件版本与 Maven 版本兼容。