✅ Maven pom.xml 标签大全(完整版)
在 Maven 项目中,pom.xml 是项目对象模型(Project Object Model)的核心配置文件,它定义了项目的结构、依赖、构建配置、插件、生命周期等信息。以下是 Maven pom.xml 中 完整的标签大全 ,按官方结构层级分类整理,涵盖所有标准标签、作用、使用场景、示例和最佳实践,适合开发、运维、架构师查阅和规范项目配置。
✅ 基于 Maven 4.0.0 POM Schema(
http://maven.apache.org/POM/4.0.0)✅ 所有标签均符合官方规范,适用于 Maven 3.x / 4.x
✅ 按照 逻辑层级 排列,便于阅读和理解
📌 一、根标签(Root Element)
| 标签 | 必需 | 说明 |
|---|---|---|
<project> |
✅ 是 | POM 的根元素,所有配置的容器 |
xmlns |
✅ 是 | 命名空间声明:http://maven.apache.org/POM/4.0.0 |
xmlns:xsi |
✅ 是 | XML Schema 实例命名空间 |
xsi:schemaLocation |
✅ 是 | 指定 POM Schema 文件位置,确保校验合法 |
<XML>
XML
<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">
✅ 注意 : 这三个命名空间 和 schemaLocation 必须完整存在, 否则 IDE 或构建工具会报
错。
说明:
xmlns:Maven POM 命名空间。xsi:schemaLocation:XML Schema 定位,用于校验 XML 结构。
📌 二、项目基本信息(Project Identity)
| 标签 | 必需 | 说明 | 示例 |
|---|---|---|---|
<modelVersion> |
✅ 是 | POM 模型版本,固定为 4.0.0 |
<modelVersion>4.0.0</modelVersion> |
<groupId> |
✅ 是 | 组织/公司标识(通常是反向域名) | <groupId>com.example</groupId> |
<artifactId> |
✅ 是 | 项目/模块名称 | <artifactId>my-app</artifactId> |
<version> |
✅ 是 | 版本号(支持 SNAPSHOT) | <version>1.2.3-SNAPSHOT</version> |
<packaging> |
否 | 打包类型,默认 jar |
<packaging>war</packaging> 或 <packaging>pom</packaging> |
📌 packaging 常见类型:
| 类型 | 说明 |
|---|---|
jar |
普通 Java 库(默认) |
war |
Web 应用(部署到 Tomcat) |
pom |
父项目或聚合项目(不生成包) |
ear |
Java EE 企业归档(含多个 war/jar) |
rar |
资源适配器(用于 JCA) |
💡
pom类型常用于父项目,仅管理子模块。
📌 三、项目元数据(Metadata)
| 标签 | 必需 | 说明 | 示例 |
|---|---|---|---|
<name> |
否 | 项目显示名称 | <name>My Awesome Application</name> |
<description> |
否 | 项目描述 | <description> A sample Maven project for learning. </description> |
<url> |
否 | 项目官网地址 | <url>https://example.com/my-app</url> |
<inceptionYear> |
否 | 项目创建年份 | <inceptionYear>2020</inceptionYear> |
✅ 这些标签主要用于生成项目站点(
mvn site)或仓库元数据,非构建必需。
✅ 示例:
XML
<groupId>com.example</groupId>
<artifactId>my-webapp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Web Application</name>
<description>A sample web application using Maven</description>
<url>https://example.com/myapp</url>
📌 四、开发者,贡献者与许可证(Developers & Licensing)
1. 开发者信息 <developers>
<XML>
XML
<developers>
<developer>
<id>john_doe</id>
<name>John Doe</name>
<email>john@example.com</email>
<url>https://john.example.com</url>
<organization>Example Corp</organization>
<organizationUrl>https://example.com</organizationUrl>
<roles>
<role>developer</role>
<role>architect</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
| 标签 | 说明 |
|---|---|
| <developers> | 开发者列表 |
| <developer> | 单个开发者信息 |
<id> |
开发者唯一标识(可选) |
<name> |
姓名 |
<email> |
邮箱 |
<url> |
个人主页 |
<organization> |
所属公司 |
<organizationUrl> |
公司网址 |
<roles> |
角色(如 developer、tester) |
<timezone> |
时区(用于构建时间计算) |
✅ 多个
<developer>可列出团队成员。✅
<contributors>标签结构相同,用于贡献者(非核心开发)。
2. 许可证 <licenses>
<XML>
XML
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
| 标签 | 说明 |
|---|---|
| <licenses> | 许可证列表 |
| <license> | 单个许可证 |
<name> |
许可证名称(如 MIT、GPL、Apache-2.0) |
<url> |
许可证全文链接 |
<distribution> |
分发方式(repo 表示在仓库中分发)repo(默认)、manual |
<comments> |
备注 |
✅ 企业项目建议使用 Apache-2.0 或 MIT,避免 GPL 等传染性协议。
📌 五、属性配置(Properties)
| 标签 | 说明 | 示例 |
|---|---|---|
<properties> |
定义可重用的变量,用于版本、编码、路径等 |
<XML>
XML
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>6.1.5</spring.version>
<junit.version>5.10.0</junit.version>
<logback.version>1.4.14</logback.version>
</properties>
🔍 常用内置属性(Maven 自动提供):
| 属性 | 说明 |
|---|---|
${project.version} |
当前项目版本 |
${project.groupId} |
当前 groupId |
${project.artifactId} |
当前 artifactId |
${project.basedir} |
项目根目录(pom.xml 所在目录) |
${user.home} |
用户主目录 |
${env.JAVA_HOME} |
环境变量 |
✅ 最佳实践 :所有依赖版本、Java 版本、编码格式都应通过
<properties>统一管理!✅ 可在
<dependency>、<plugin>、<resources>中使用${property}引用。
📌 六、依赖管理(Dependencies)
1. 项目直接依赖<dependencies> ------ 实际引入依赖
XML
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.21</version>
<scope>compile</scope>
<!-- compile, provided, runtime, test, system, import -->
<type>jar</type> <!-- 默认 jar -->
<optional>true</optional> <!-- 是否可选依赖 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<XML>
✅ <dependency> 子标签详解:
| 标签 | 必需 | 说明 |
|---|---|---|
<groupId> |
✅ 是 | 依赖组织 ID |
<artifactId> |
✅ 是 | 依赖项目 ID |
<version> |
✅ 是 | 版本号(推荐用属性) |
<scope> |
否 | 作用域(见下表) |
<type> |
否 | 类型(默认 jar,也可为 war, pom) |
<classifier> |
否 | 分类器(如 sources, javadoc) |
<optional> |
否 | 是否为可选依赖(被依赖时不传递) |
<exclusions> |
否 | 排除传递依赖(防冲突) |
📌 scope 作用域详解(重要!)
| scope | 说明 | 是否参与编译 | 是否打包 | 是否传递 |
|---|---|---|---|---|
compile |
默认,参与编译、打包、运行、测试 | ✅ 是 | ✅ 是 | ✅ 是 |
provided |
编译和测试使用,运行时由容器提供(如 Servlet API) | ✅ 是 | ❌ 否 | ❌ 否 |
runtime |
运行时需要(如 JDBC 驱动) | ❌ 否 | ✅ 是 | ✅ 是 |
test |
仅测试使用(如 JUnit) | ❌ 否 | ❌ 否 | ❌ 否 |
system |
本地路径(不推荐) | ✅ 是 | ✅ 是 | ✅ 是 |
import |
仅用于 <dependencyManagement>,导入 BOM |
❌ 否 | ❌ 否 | ❌ 否 |
⚠️
system依赖绑定本地路径,破坏可移植性,严禁用于生产项目!
2. 依赖管理<dependencyManagement> ------ 统一管理版本(不引入)
<XML>
XML
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
✅ 作用:只定义版本,不实际引入。
✅ 作用:在父 POM 中统一管理子模块的依赖版本,子模块在
<dependencies>中引用时,无需写<version>。✅ 用途:父 POM 管理多模块项目依赖版本,避免冲突。
3. <exclusions> ------ 排除传递依赖
<XML>
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
✅ 常用于:替换日志框架(如用 Logback 替换 Log4j2)、避免重复依赖。
📌 七、构建配置(Build)
<XML>
XML
<build>
<!-- 源码目录(默认可不配置) -->
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 是否启用资源过滤 -->
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<!-- 输出目录 -->
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<!-- 插件配置 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
<!-- 最终打包文件名 -->
<finalName>myapp-${project.version}</finalName>
<!-- 默认生命周期绑定 -->
<defaultGoal>package</defaultGoal>
</build>
| 标签 | 说明 |
|---|---|
<build> |
所有构建配置的容器 |
<sourceDirectory> |
源码目录,默认 src/main/java |
<testSourceDirectory> |
测试源码目录,默认 src/test/java |
<outputDirectory> |
编译输出目录,默认 target/classes |
<testOutputDirectory> |
测试编译输出,默认 target/test-classes |
<directory> |
构建输出根目录,默认 target |
<finalName> |
最终打包文件名(不含扩展名) |
<filters> |
资源过滤文件列表(用于属性替换) |
<defaultGoal> |
默认执行目标(如 package) |
✅ 建议:不要随意修改默认路径,除非有特殊需求。
📌 八、插件管理(Plugins)
1. <build><plugins> ------ 实际使用插件
<XML>
XML
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
2. <build><pluginManagement> ------ 统一管理插件版本(不执行)
<XML>
XML
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
✅ 常用插件:
| 插件 | 用途 |
|---|---|
maven-compiler-plugin |
编译 Java 源码 |
maven-surefire-plugin |
运行单元测试 |
maven-failsafe-plugin |
运行集成测试 |
maven-jar-plugin |
打包 JAR |
maven-war-plugin |
打包 WAR |
maven-assembly-plugin |
打包含依赖的 Fat Jar |
maven-shade-plugin |
重命名包、合并资源(用于阴影化) |
maven-resources-plugin |
处理资源文件 |
maven-deploy-plugin |
部署到远程仓库 |
maven-install-plugin |
安装到本地仓库 |
✅ 作用:父 POM 中统一管理插件版本,子模块中只需声明插件,无需写 version。
✅ 推荐:
<pluginManagement>+<plugins>配合使用,实现版本统一与灵活启用。
📌 九、资源配置(Resources)
| 标签 | 说明 |
|---|---|
<resources> |
定义主资源目录(src/main/resources) |
<testResources> |
定义测试资源目录(src/test/resources) |
<XML>
XML
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 是否替换占位符 -->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/dev/*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
✅
<filtering>true</filtering>:允许在资源文件中使用${property}替换(如${env})。
📌 十、配置文件(Profiles)------环境配置
<XML>
XML
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
<database.url>jdbc:h2:mem:testdb</database.url>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<filters>
<filter>src/main/filters/dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<env>production</env>
<database.url>jdbc:mysql://prod-server:3306/mydb</database.url>
</properties>
</profile>
</profiles>
✅ <activation> 激活方式:
| 方式 | 说明 |
|---|---|
<activeByDefault>true``</activeByDefault> |
默认激活 |
<jdk>17</jdk> |
JDK 版本匹配时激活 |
<os> |
操作系统匹配(如 Windows、Linux) |
<property> |
系统属性存在时激活 (如 <property> <name>env</name> <value>prod</value> ``</property>) |
<file> |
文件存在时激活 |
✅ 激活方式:
mvn package -Pprod<activeByDefault>true</activeByDefault>- 系统属性:
-Denv=prod- 文件存在:
<file><exists>config/prod.properties</exists></file>✅ 激活方式可组合使用!
✅ 典型用途:不同环境(dev/test/prod)的数据库配置、日志级别、依赖差异。
📌 十一、模块聚合(Multi-module)------模块化项目(多模块)
<XML>
XML
<modules>
<module>core</module>
<module>api</module>
<module>web</module>
</modules>
✅ 要求:
module-a/、module-b/是子目录,且子模块的pom.xml中<parent>指向当前父 POM。✅ 仅在
<packaging>pom</packaging>的父项目中使用。✅ 子模块目录必须存在,且每个子模块有自己的
pom.xml。✅ 命令
mvn clean install会递归构建所有子模块。
📌 十二、父 POM(Parent)
子模块继承父 POM:
<XML>
XML
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<relativePath>../parent/pom.xml</relativePath>
<!-- 可选,默认查找 ../pom.xml -->
</parent>
✅ 父 POM 通常
<packaging>pom</packaging>
📌 十三、仓库配置(Repositories & Plugin Repositories)
1. <repositories> ------项目依赖仓库
<XML>
XML
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>my-company-repo</id>
<url>https://nexus.example.com/repository/maven-public/</url>
</repository>
</repositories>
2. <pluginRepositories> ------ 插件仓库
<XML>
XML
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
✅ 企业项目建议配置私有仓库(Nexus/Artifactory),避免依赖中央仓库不稳定。
✅
releases和snapshots控制是否允许下载发布版或快照版。
📌 十三、分发管理(Distribution Management)
用于部署项目到远程仓库(如 Nexus、Maven Central):
<XML>
XML
<distributionManagement>
<repository>
<id>releases</id>
<url>https://nexus.example.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>https://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>
<site>
<id>website</id>
<url>scp://example.com/var/www/docs</url>
</site>
</distributionManagement>
✅ 需配合
settings.xml中的<servers>配置认证信息。
📌 十四、其他重要标签(辅助配置)
| 标签 | 说明 |
|---|---|
<licenses> |
许可证信息(见上) |
<scm> |
源码管理(Git/SVN)链接,用于生成站点 |
<issueManagement> |
问题跟踪系统(Jira、GitHub Issues) |
<ciManagement> |
持续集成系统(Jenkins、GitHub Actions) |
<organization> |
组织信息 |
| <prerequisites> | 指定 Maven 最低版本(如 <maven>3.6.0</maven>) |
| <distributionManagement> | 部署仓库配置(发布到 Nexus/Artifactory) |
| <reporting> | 生成报告插件配置(如 Javadoc、Checkstyle) |
示例:<scm>(用于生成 CHANGELOG、发布标签)
<XML>
XML
<scm>
<connection>scm:git:https://github.com/example/my-app.git</connection>
<developerConnection>
scm:git:https://github.com/example/my-app.git
</developerConnection>
<url>https://github.com/example/my-app</url>
<tag>HEAD</tag>
</scm>
示例:部署配置
XML
<distributionManagement>
<repository>
<id>my-repo</id>
<url>https://repo.example.com/releases</url>
</repository>
<snapshotRepository>
<id>my-snapshots</id>
<url>https://repo.example.com/snapshots</url>
</snapshotRepository>
</distributionManagement>
✅ 需在 ~/.m2/settings.xml 中配置 server 的 id 和 username/password
✅ 附:Maven POM 标签结构图(层级关系)
<TEXT>
XML
<project>
├── <modelVersion>
├── <groupId>
├── <artifactId>
├── <version>
├── <packaging>
├── <name>
├── <description>
├── <url>
├── <inceptionYear>
├── <organization>
├── <developers>
├── <licenses>
├── <properties>
├── <dependencyManagement>
│ └── <dependencies> → <dependency>...</dependency>
├── <dependencies>
│ └── <dependency>...</dependency>
├── <build>
│ ├── <sourceDirectory>
│ ├── <testSourceDirectory>
│ ├── <resources>
│ ├── <testResources>
│ ├── <plugins>
│ │ └── <plugin>...</plugin>
│ └── <pluginManagement>
│ └── <plugins> → <plugin>...</plugin>
├── <profiles>
│ └── <profile> → <id>, <activation>, <properties>, <dependencies>, <build>
├── <modules>
├── <repositories>
├── <pluginRepositories>
├── <distributionManagement>
├── <scm>
├── <issueManagement>
└── <ciManagement>
✅ 最佳实践总结(一句话记住)
🔑 "父项目用 pom + dependencyManagement + pluginManagement 统一管理,子项目只写 groupId/artifactId,不写 version,用属性管版本,用 profile 管环境。"
✅ 推荐工具辅助阅读
| 工具 | 功能 |
|---|---|
| IntelliJ IDEA | 自动高亮、提示、依赖树可视化 |
| Maven Helper 插件 | 可视化冲突分析、一键排除 |
mvn help:effective-pom |
查看最终生效的完整 POM |
mvn dependency:tree |
查看依赖树,找冲突 |
| SonarQube / OWASP Dependency-Check | 检查安全漏洞 |
📎 附录:完整示例模板(推荐收藏)
<XML>
XML
<?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.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 元数据 -->
<name>My Application</name>
<description>A sample Maven project</description>
<url>https://example.com</url>
<!-- 开发者 -->
<developers>
<developer>
<name>John Doe</name>
<email>john@example.com</email>
</developer>
</developers>
<!-- 许可证 -->
<licenses>
<license>
<name>Apache License 2.0</name>
<url>
https://www.apache.org/licenses/LICENSE-2.0.txt
</url>
</license>
</licenses>
<!-- 属性 -->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>6.1.5</spring.version>
<junit.version>5.10.0</junit.version>
</properties>
<!-- 依赖管理(父项目用) -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 实际依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 构建配置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<!-- 环境配置 -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
</properties>
</profile>
</profiles>
</project>
✅ 总结:POM.xml 标签结构层级(推荐记忆顺序)
<TEXT>
XML
project
├── modelVersion
├── groupId / artifactId / version / packaging / name / description / url
├── properties
├── parent
├── modules
├── developers / contributors
├── licenses
├── scm
├── issueManagement / ciManagement
├── repositories / pluginRepositories
├── dependencyManagement
├── dependencies
├── build
│ ├── sourceDirectory / testSourceDirectory
│ ├── resources / testResources
│ ├── plugins
│ └── finalName
├── profiles
└── distributionManagement
📌 提示 :Maven 官方文档是权威来源:https://maven.apache.org/pom.html