Maven 4.0.0 模式-pom.xml配置详解
此 pom.xml 文件涵盖了 Maven 4.0.0 模式支持的所有主要标签,包括项目元数据、依赖管理、构建配置、发布管理等。每个标签都配有详细注释,说明其作用、常见用法和可能的值。
此文件旨在展示标签的完整性,部分配置(如仓库 URL、SCM 地址)是示例值,无法直接运行。实际使用时需根据项目需求调整。
按照 Maven 的逻辑结构组织,从项目坐标到高级配置,层次分明,便于学习。(协同ai生成)
xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 根元素,定义 Maven POM 文件的命名空间和模式 -->
<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">
<!-- POM 文件的模型版本,当前为 4.0.0,固定值 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目坐标:唯一标识项目 -->
<!-- groupId:组织或项目的标识,通常是反向域名 -->
<groupId>com.example</groupId>
<!-- artifactId:项目名称,唯一标识项目 -->
<artifactId>my-project</artifactId>
<!-- version:项目版本号,推荐使用语义化版本,SNAPSHOT 表示开发中 -->
<version>1.0.0-SNAPSHOT</version>
<!-- packaging:打包类型,常见值包括 jar、war、pom、ear 等 -->
<packaging>jar</packaging>
<!-- 项目元数据:提供项目的描述性信息 -->
<!-- name:项目名称,易于阅读 -->
<name>My Example Project</name>
<!-- description:项目描述,通常用于文档生成 -->
<description>A comprehensive Maven project for learning purposes.</description>
<!-- url:项目主页或文档地址 -->
<url>https://www.example.com</url>
<!-- inceptionYear:项目起始年份 -->
<inceptionYear>2025</inceptionYear>
<!-- organization:项目所属组织信息 -->
<organization>
<!-- name:组织名称 -->
<name>Example Inc.</name>
<!-- url:组织网站 -->
<url>https://www.example.org</url>
</organization>
<!-- licenses:项目许可信息 -->
<licenses>
<license>
<!-- name:许可名称 -->
<name>Apache License, Version 2.0</name>
<!-- url:许可详情链接 -->
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
<!-- distribution:分发方式,repo 表示可通过仓库分发 -->
<distribution>repo</distribution>
<!-- comments:许可备注 -->
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!-- developers:项目开发者信息 -->
<developers>
<developer>
<!-- id:开发者唯一标识 -->
<id>johndoe</id>
<!-- name:开发者姓名 -->
<name>John Doe</name>
<!-- email:开发者邮箱 -->
<email>[email protected]</email>
<!-- url:开发者主页 -->
<url>https://johndoe.example.com</url>
<!-- organization:开发者所属组织 -->
<organization>Example Inc.</organization>
<!-- organizationUrl:组织链接 -->
<organizationUrl>https://www.example.org</organizationUrl>
<!-- roles:开发者角色 -->
<roles>
<role>Developer</role>
<role>Architect</role>
</roles>
<!-- timezone:开发者时区 -->
<timezone>UTC+8</timezone>
</developer>
</developers>
<!-- contributors:项目贡献者信息,类似 developers 但通常是非核心贡献者 -->
<contributors>
<contributor>
<name>Jane Smith</name>
<email>[email protected]</email>
<organization>Community</organization>
<roles>
<role>Tester</role>
</roles>
</contributor>
</contributors>
<!-- mailingLists:项目邮件列表,用于沟通 -->
<mailingLists>
<mailingList>
<!-- name:邮件列表名称 -->
<name>Dev List</name>
<!-- subscribe:订阅地址 -->
<subscribe>[email protected]</subscribe>
<!-- unsubscribe:取消订阅地址 -->
<unsubscribe>[email protected]</unsubscribe>
<!-- post:发送邮件地址 -->
<post>[email protected]</post>
<!-- archive:邮件存档地址 -->
<archive>https://mail.example.com/archive</archive>
</mailingList>
</mailingLists>
<!-- prerequisites:项目构建的前提条件 通常在父 POM 或大型项目中使用 -->
<prerequisites>
<!-- 目前 <prerequisites> 仅支持 <maven> 这一子元素 -->
<!-- maven:最低要求的 Maven 版本 -->
<maven>3.6.0</maven>
</prerequisites>
<!-- modules:多模块项目中列出子模块 -->
<modules>
<!-- module:子模块目录名称 -->
<module>module-a</module>
<module>module-b</module>
</modules>
<!-- properties:定义项目级别的属性,供其他配置引用 -->
<properties>
<!-- Java 源码和目标版本 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- 源文件编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 自定义属性 -->
<spring.version>5.3.20</spring.version>
</properties>
<!-- dependencyManagement:统一管理依赖版本,通常在父 POM 中使用 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- dependencies:项目直接依赖的库 -->
<dependencies>
<!-- 依赖示例:JUnit 用于测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<!--
scope:定义依赖的使用范围,控制依赖在项目生命周期中的可用性。
可选值及其说明:
1. compile(默认):
- 依赖在编译、测试、运行时和打包阶段均可用。
- 适用于核心库(如 spring-core、commons-lang)。
- 传递性:会传递到依赖该项目的其他项目。
2. provided:
- 依赖仅在编译和测试阶段可用,运行时由环境提供(如容器)。
- 适用于 Servlet API、JSP API 等。
- 传递性:不会传递到其他项目。
3. runtime:
- 依赖在运行时和测试阶段需要,不参与编译。
- 适用于数据库驱动(如 MySQL JDBC 驱动)。
- 传递性:会传递到其他项目。
4. test:
- 依赖仅用于测试阶段(编译和运行测试代码)。
- 不参与主代码的编译、运行或打包。
- 适用于测试框架(如 JUnit、TestNG)或模拟库(如 Mockito)。
- 传递性:不会传递到其他项目。
5. system:
- 依赖从本地文件系统加载,不从 Maven 仓库下载。
- 需要配合 <systemPath> 指定本地路径。
- 不推荐使用,易导致不可移植。
- 传递性:不会传递到其他项目。
6. import:
- 用于 <dependencyManagement> 中,导入另一个 POM 的依赖管理配置。
- 仅在 <dependencyManagement> 有效,通常用于父子 POM 或 BOM(Bill of Materials)。
- 不直接影响依赖的生命周期。
当前值:test,表示此依赖(JUnit)仅用于测试阶段。
-->
<scope>test</scope>
<!--
optional:指定依赖是否为可选依赖,控制依赖的传递性。
可选值:
- true:表示依赖是可选的,不会自动传递到依赖该项目的其他项目。
- false(默认):表示依赖是必需的,会传递到依赖该项目的其他项目。
作用:
- 当 optional 设置为 true 时,该依赖仅在本项目中使用,其他项目引用本项目时不会继承此依赖。
- 适用于以下场景:
1. 依赖是项目内部实现细节,不希望暴露给使用方(例如,某些日志实现库)。
2. 依赖是可选功能,依赖方可以自行选择是否添加(例如,某些数据库驱动)。
3. 避免不必要的传递依赖,减少依赖冲突或臃肿。
注意事项:
- optional=true 的依赖不会出现在依赖方的类路径中,依赖方需要显式声明该依赖。
- 常用于库项目或框架开发(如 Spring 提供的可选依赖)。
- 不建议在应用程序项目中滥用 optional=true,因为可能导致依赖方遗漏必要依赖。
当前值:true,表示 JUnit 是本项目的可选依赖,仅用于本项目的测试,且不会传递到其他项目。
-->
<optional>true</optional>
</dependency>
<!-- 依赖示例:Apache Commons Lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<!-- exclusions:排除传递依赖 -->
<exclusions>
<exclusion>
<groupId>some.unwanted</groupId>
<artifactId>unwanted-lib</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- repositories:依赖的远程仓库 -->
<repositories>
<repository>
<!-- id:仓库唯一标识 -->
<id>aliyun</id>
<!-- name:仓库名称 -->
<name>Aliyun Maven Repository</name>
<!-- url:仓库地址 -->
<url>https://maven.aliyun.com/repository/public</url>
<!-- layout:仓库布局,默认值为 default -->
<layout>default</layout>
<!-- releases:是否支持发布版本 -->
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<!-- snapshots:是否支持快照版本 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!-- pluginRepositories:插件的远程仓库 -->
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<name>Aliyun Plugin Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!-- build:构建过程的配置 -->
<build>
<!-- defaultGoal:默认构建目标 -->
<defaultGoal>install</defaultGoal>
<!-- directory:构建输出目录,默认是 target -->
<directory>${project.basedir}/target</directory>
<!-- finalName:最终生成的文件名 -->
<finalName>${project.artifactId}-${project.version}</finalName>
<!-- sourceDirectory:源代码目录 -->
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<!-- testSourceDirectory:测试代码目录 -->
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<!-- resources:资源文件配置 -->
<resources>
<resource>
<!-- directory:资源文件目录 -->
<directory>${project.basedir}/src/main/resources</directory>
<!-- filtering:是否启用变量替换 -->
<filtering>true</filtering>
<!-- includes:包含的文件 -->
<includes>
<include>**/*.properties</include>
</includes>
<!-- excludes:排除的文件 -->
<excludes>
<exclude>**/*.bak</exclude>
</excludes>
</resource>
</resources>
<!-- testResources:测试资源文件 -->
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>false</filtering>
</testResource>
</testResources>
<!-- plugins:构建过程中使用的插件 -->
<plugins>
<!-- 插件示例:配置编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<!-- configuration:插件特定配置 -->
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<!-- executions:插件执行的特定阶段 -->
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!-- pluginManagement:统一管理插件版本 -->
<pluginManagement>
<plugins>
<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>
</pluginManagement>
</build>
<!-- reporting:生成项目报告的配置 -->
<reporting>
<!-- outputDirectory:报告输出目录 -->
<outputDirectory>${project.basedir}/target/site</outputDirectory>
<!-- plugins:报告插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.1</version>
<!-- reportSets:报告的子集 -->
<reportSets>
<reportSet>
<id>default</id>
<reports>
<report>dependencies</report>
<report>index</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<!-- profiles:构建配置文件,用于不同环境 -->
<profiles>
<profile>
<!-- id:配置文件唯一标识 -->
<id>dev</id>
<!-- activation:激活条件 -->
<activation>
<!-- property:基于属性激活 -->
<property>
<name>env</name>
<value>dev</value>
</property>
<!-- jdk:基于 JDK 版本激活 -->
<jdk>11</jdk>
<!-- os:基于操作系统激活 -->
<os>
<name>Windows 10</name>
<family>Windows</family>
</os>
<!-- file:基于文件存在激活 -->
<file>
<exists>${project.basedir}/src/main/resources/dev.properties</exists>
</file>
</activation>
<!-- properties:开发环境的属性 -->
<properties>
<env.type>development</env.type>
</properties>
<!-- dependencies:开发环境的依赖 -->
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</profile>
</profiles>
<!-- distributionManagement:发布配置 -->
<distributionManagement>
<!-- repository:发布版本的仓库 -->
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>https://nexus.example.com/repository/releases</url>
</repository>
<!-- snapshotRepository:快照版本的仓库 -->
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>https://nexus.example.com/repository/snapshots</url>
</snapshotRepository>
<!-- downloadUrl:项目下载地址 -->
<downloadUrl>https://nexus.example.com/repository/releases</downloadUrl>
<!-- site:项目文档站点 -->
<site>
<id>project-site</id>
<name>Project Documentation</name>
<url>https://www.example.com/docs</url>
</site>
</distributionManagement>
<!-- issueManagement:问题跟踪系统 -->
<issueManagement>
<!-- system:问题跟踪系统名称 -->
<system>JIRA</system>
<!-- url:问题跟踪系统链接 -->
<url>https://jira.example.com</url>
</issueManagement>
<!-- ciManagement:持续集成系统 -->
<ciManagement>
<!-- system:CI 系统名称 -->
<system>Jenkins</system>
<!-- url:CI 系统链接 -->
<url>https://jenkins.example.com</url>
<!-- notifiers:通知配置 -->
<notifiers>
<notifier>
<type>mail</type>
<sendOnError>true</sendOnError>
<configuration>
<address>[email protected]</address>
</configuration>
</notifier>
</notifiers>
</ciManagement>
<!-- scm:源码管理 -->
<scm>
<!-- connection:只读 SCM 连接 -->
<connection>scm:git:https://github.com/example/my-project.git</connection>
<!-- developerConnection:开发者读写 SCM 连接 -->
<developerConnection>scm:git:[email protected]:example/my-project.git</developerConnection>
<!-- url:SCM 浏览地址 -->
<url>https://github.com/example/my-project</url>
<!-- tag:SCM 标签 -->
<tag>HEAD</tag>
</scm>
<!-- parent:父 POM 配置,用于继承 -->
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<!-- relativePath:父 POM 文件的相对路径 -->
<relativePath>../parent/pom.xml</relativePath>
</parent>
</project>