pom.xml
简介
pom.xml 文件是 Maven 项目的核心配置文件,全称是 “Project Object Model”(项目对象模型)。包含了项目的各种配置信息,如依赖管理、构建过程、插件配置等。Maven 使用 pom.xml 来管理和构建项目。
每个 Maven 项目都有且仅有一个 pom.xml 文件,它是 Maven 工作的基础。
主要功能:
1.项目基本信息:
groupId:项目的组ID,通常是公司或组织的域名反写。
artifactId:项目的唯一标识符,通常是项目的名称。
version:项目的版本号。
packaging:项目的打包方式,常见的有 jar、war 等。
2.依赖管理:
dependencies:定义项目所需的外部库和它们的版本。
dependencyManagement:集中管理依赖的版本,子模块可以直接引用而不需要指定版本。
3.构建配置:
build:配置项目的构建过程,包括编译、测试、打包等。
plugins:定义构建过程中使用的插件及其配置。
4.属性配置:
properties:定义一些常用的属性,如 Java 版本、编码等。
5.仓库配置:
repositories:定义从哪里下载依赖。
distributionManagement:定义发布构件的位置。
6.模块管理:
modules:多模块项目中定义子模块。
pom.xml 详细讲解
1. pom.xml 基本结构
一个典型的 pom.xml 文件具有如下基本结构:
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.0</version><packaging>jar</packaging><!-- 项目信息 --><name>My Application</name><description>A sample Maven project</description><url>http://www.example.com</url><!-- 属性定义 --><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. 项目坐标详解
Maven 使用一组坐标来唯一标识一个项目,这些坐标组成了项目的"地址"。
2.1 groupId(组ID)
- 通常表示项目所属的组织或公司
- 推荐使用反向域名的方式命名,例如:
com.google、org.apache - 示例:
<groupId>com.company.project</groupId>
2.2 artifactId(构件ID)
- 项目的唯一标识符
- 通常与项目名称相同
- 示例:
<artifactId>my-webapp</artifactId>
2.3 version(版本号)
- 项目的版本信息
- 快照版本以
-SNAPSHOT结尾 - 示例:
<version>1.0.0</version>或<version>2.1.0-SNAPSHOT</version>
2.4 packaging(打包方式)
- 指定项目的打包类型
- 常见:
jar(默认)、war、pom、ear等 - 示例:
<packaging>war</packaging>
3. 属性(Properties)
属性提供了在 POM 中其他地方可以引用的占位符值。
<properties><!-- 项目源码编码 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- Java 版本 --><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><!-- 依赖版本管理 --><junit.version>5.8.2</junit.version><spring.version>5.3.21</spring.version></properties>引用方式:
${property.name},例如${junit.version}
4. 依赖管理(Dependencies)
依赖管理是 Maven 最核心的功能之一,它自动处理项目所需的第三方库。
4.1 基本依赖结构
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>4.2 依赖范围(Scope)
| Scope | 描述 | 编译classpath | 测试classpath | 运行classpath | 示例 |
|---|---|---|---|---|---|
| compile | 默认值,编译和运行都需要 | Y | Y | Y | Spring Core |
| provided | 编译和测试需要,运行时由容器提供 | Y | Y | N | Servlet API |
| runtime | 编译不需要,运行时需要 | N | Y | Y | JDBC Driver |
| test | 仅测试时需要 | N | Y | N | JUnit |
| system | 类似 provided,需指定本地路径 | Y | Y | N | 本地 jar 包 |
4.3 依赖传递
Maven 支持依赖传递机制:
- 当项目 A 依赖 B,B 依赖 C,则 A 自动依赖 C
- 可以通过 exclusions 排除不需要的传递依赖
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.3.21</version><exclusions><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency>5. 依赖管理(Dependency Management)
用于统一管理依赖版本,常用于父 POM:
<dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies></dependencyManagement>子模块只需声明 groupId 和 artifactId,版本会从父 POM 继承。
6. 构建配置(Build)
构建配置控制项目的编译、测试、打包等过程。
6.1 基本构建配置
<build><!-- 项目最终名称 --><finalName>myapp</finalName><!-- 源码目录 --><sourceDirectory>src/main/java</sourceDirectory><!-- 资源目录 --><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><!-- 测试源码目录 --><testSourceDirectory>src/test/java</testSourceDirectory><!-- 输出目录 --><outputDirectory>target/classes</outputDirectory><testOutputDirectory>target/test-classes</testOutputDirectory></build>6.2 插件管理
<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></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version><configuration><skipTests>false</skipTests></configuration></plugin></plugins></build>6.3 插件管理(Plugin Management)
类似于依赖管理,用于统一插件版本:
<pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version></plugin></plugins></pluginManagement>7. 继承与聚合
7.1 继承(Parent)
子项目可以从父项目继承配置:
<parent><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0.0</version><relativePath>../parent/pom.xml</relativePath></parent>7.2 聚合(Modules)
一个项目可以聚合多个子模块:
<modules><module>module-a</module><module>module-b</module><module>module-c</module></modules>8. Profiles(环境配置)
Profiles 允许根据不同的环境激活不同的配置:
<profiles><profile><id>dev</id><properties><database.url>jdbc:mysql://localhost:3306/dev</database.url></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>prod</id><properties><database.url>jdbc:mysql://prod-server:3306/prod</database.url></properties></profile></profiles>激活方式:
- 命令行:
mvn clean install -P prod - 环境变量:
MAVEN_ARGS=-P prod
9. 常用 Maven 命令
| 命令 | 功能说明 |
|---|---|
| mvn clean | 清理编译结果 |
| mvn compile | 编译项目 |
| mvn test | 运行测试 |
| mvn package | 打包项目 |
| mvn install | 安装到本地仓库 |
| mvn deploy | 发布到远程仓库 |
| mvn site | 生成项目站点文档 |
| mvn dependency:tree | 显示依赖树 |