Maven的pom.xml文件是项目的核心配置载体,所有和项目构建、依赖、插件相关的规则都需要在该文件中定义。合理的标签配置能够避免依赖冲突、简化构建流程,是Maven项目开发的基础环节。

pom.xml的基础结构标签
pom.xml的根标签是<project>,所有其他配置都嵌套在该标签内部,基础结构包含项目坐标、版本管理等核心信息。
项目坐标相关标签
项目坐标是Maven定位项目的唯一标识,包含三个核心标签:
- <groupId>:定义项目所属的组织或公司,通常采用反向域名格式,比如com.example
- <artifactId>:定义项目的唯一标识,一般是项目名
- <version>:定义项目的版本号,比如1.0.0-SNAPSHOT表示快照版本
以下是一个基础的项目坐标配置示例:
<?xml version="1.0" encoding="UTF-8"?>
<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.ipipp</groupId>
<artifactId>demo-project</artifactId>
<version>1.0.0</version>
<!-- 项目打包类型,默认是jar,可选war、pom等 -->
<packaging>jar</packaging>
</project>
依赖管理相关标签
依赖管理是pom.xml最常用的功能,通过<dependencies>标签可以引入项目所需的第三方库。
单个依赖的配置
每个依赖都放在<dependency>标签中,除了基础的坐标标签外,还可以配置<scope>来指定依赖的作用范围:
- compile:默认范围,编译、测试、运行阶段都有效
- test:仅测试阶段有效,比如JUnit
- provided:编译和测试阶段有效,运行阶段由容器提供,比如Servlet API
- runtime:测试和运行阶段有效,比如JDBC驱动
配置Spring Core依赖的示例:
<dependencies>
<!-- Spring Core依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
<scope>compile</scope>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
依赖版本统一管理
当项目中有多个依赖使用相同的版本号时,可以通过<properties>标签定义版本常量,避免重复配置和版本不一致问题。
<properties>
<spring.version>5.3.20</spring.version>
<junit.version>4.13.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
构建与插件配置标签
Maven的构建过程可以通过<build>标签下的插件进行自定义,常见的比如编译插件、打包插件。
编译插件配置
默认的Maven编译插件使用的是JDK1.5版本,如果需要指定更高的JDK版本,需要配置maven-compiler-plugin插件。
<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>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
&plugins>
</build>
资源文件配置
如果项目的资源文件(比如properties、xml文件)放在非默认的src/main/resources目录下,可以通过<resources>标签指定资源目录。
<build>
<resources>
<resource>
<!-- 资源文件目录 -->
<directory>src/main/config</directory>
<!-- 是否替换资源文件中的占位符 -->
<filtering>true</filtering>
</resource>
</resources>
</build>
多模块项目配置标签
如果项目是聚合工程,父项目的pom.xml需要配置<modules>标签指定子模块,同时打包类型需要设置为pom。
<!-- 父项目pom.xml -->
<groupId>com.ipipp</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>common-module</module>
<module>service-module</module>
<module>web-module</module>
</modules>
子模块中需要配置<parent>标签指定父项目坐标,这样就能继承父项目的依赖和插件配置。
<!-- 子模块pom.xml -->
<parent>
<groupId>com.ipipp</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>common-module</artifactId>
<version>1.0.0</version>