Maven POM 标签完整参考手册
目录
- 一、根标签
- 二、项目基本信息
- 三、父 POM 继承
- 四、属性配置
- 五、依赖管理
- 六、模块管理
- 七、构建配置
- 八、分发管理
- 九、仓库配置
- 十、常用插件配置示例
一、根标签
| 标签 | 说明 | 示例 |
|---|---|---|
<project> | POM 文件的根元素,所有配置都包裹在里面 | <project xmlns="..."> |
<modelVersion> | POM 模型的版本,Maven 2/3 固定为 4.0.0 | <modelVersion>4.0.0</modelVersion> |
二、项目基本信息
| 标签 | 说明 | 示例 |
|---|---|---|
<groupId> | 项目所属的组织/公司 ID,通常使用反转的域名 | <groupId>net.iuyy.agent</groupId> |
<artifactId> | 项目的唯一标识符,也是构建产物的名称 | <artifactId>agent-core</artifactId> |
<version> | 项目版本号,SNAPSHOT 表示快照版本 | <version>1.0-SNAPSHOT</version> |
<packaging> | 打包方式,可选:pom、jar、war、ear、maven-plugin等 | <packaging>jar</packaging> |
<name> | 项目的显示名称(可选) | <name>Agent Core</name> |
<description> | 项目描述(可选) | <description>核心代理模块</description> |
<url> | 项目主页 URL(可选) | <url>http://www.iuyy.net</url> |
三、父 POM 继承
| 标签 | 说明 | 示例 |
|---|---|---|
<parent> | 定义父 POM,继承其配置 | <parent>...</parent> |
<parent>/<groupId> | 父项目的 groupId | <groupId>net.iuyy.agent</groupId> |
<parent>/<artifactId> | 父项目的 artifactId | <artifactId>agent-platform</artifactId> |
<parent>/<version> | 父项目的版本号 | <version>1.0</version> |
<parent>/<relativePath> | 父 POM 的相对路径,默认../pom.xml,空值表示从仓库查找 | <relativePath>../pom.xml</relativePath> |
<parent>/<relativePath/> | 空标签表示不从本地查找,直接去仓库 | <relativePath/> |
四、属性配置
| 标签 | 说明 | 示例 |
|---|---|---|
<properties> | 定义可复用的属性变量 | <properties>...</properties> |
<java.version> | 自定义属性,在 POM 中用${java.version}引用 | <java.version>17</java.version> |
<project.build.sourceEncoding> | 源码编码 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
<project.reporting.outputEncoding> | 报告输出编码 | <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
<maven.compiler.source> | 编译源码的 Java 版本 | <maven.compiler.source>17</maven.compiler.source> |
<maven.compiler.target> | 编译目标 Java 版本 | <maven.compiler.target>17</maven.compiler.target> |
<maven.compiler.encoding> | 编译编码 | <maven.compiler.encoding>UTF-8</maven.compiler.encoding> |
五、依赖管理
5.1<dependencies>- 直接依赖
| 标签 | 说明 | 示例 |
|---|---|---|
<dependencies> | 所有依赖的根容器 | <dependencies>...</dependencies> |
<dependency> | 单个依赖项 | <dependency>...</dependency> |
<dependency>/<groupId> | 依赖的组织 ID | <groupId>org.springframework.boot</groupId> |
<dependency>/<artifactId> | 依赖的构件 ID | <artifactId>spring-boot-starter-web</artifactId> |
<dependency>/<version> | 依赖的版本号 | <version>3.5.10</version> |
<dependency>/<type> | 依赖的类型,默认jar | <type>jar</type> |
<dependency>/<scope> | 依赖作用域 | <scope>compile</scope> |
<dependency>/<optional> | 是否可选依赖(不传递) | <optional>true</optional> |
<dependency>/<classifier> | 分类器,用于区分相同版本的不同构建 | <classifier>exec</classifier> |
<dependency>/<exclusions> | 排除传递性依赖 | <exclusions>...</exclusions> |
<dependency>/<exclusions>/<exclusion> | 单个排除项 | <exclusion>...</exclusion> |
5.2<dependencyManagement>- 依赖版本管理
| 标签 | 说明 | 示例 |
|---|---|---|
<dependencyManagement> | 管理依赖版本,子模块继承但不会自动引入 | <dependencyManagement>...</dependencyManagement> |
<dependencyManagement>/<dependencies> | 同<dependencies> | 同<dependencies> |
5.3 Scope 作用域
| Scope | 说明 | 生效阶段 |
|---|---|---|
compile | 默认,所有阶段都可用 | 编译、测试、运行 |
provided | 由 JDK 或容器提供,打包时不包含 | 编译、测试 |
runtime | 运行时需要,编译时不需要 | 测试、运行 |
test | 仅测试阶段需要 | 测试 |
system | 系统本地路径,不推荐使用 | 编译、测试 |
import | 仅用于 dependencyManagement,导入 BOM | 版本管理 |
六、模块管理(多模块项目)
| 标签 | 说明 | 示例 |
|---|---|---|
<modules> | 子模块列表容器 | <modules>...</modules> |
<module> | 子模块的相对路径(目录名) | <module>agent-core</module> |
七、构建配置
7.1<build>基本结构
| 标签 | 说明 | 示例 |
|---|---|---|
<build> | 构建配置的根容器 | <build>...</build> |
<build>/<finalName> | 构建产物的最终名称 | <finalName>agent-web</finalName> |
<build>/<directory> | 构建输出目录,默认target | <directory>target</directory> |
<build>/<sourceDirectory> | 源码目录,默认src/main/java | <sourceDirectory>src/main/java</sourceDirectory> |
<build>/<testSourceDirectory> | 测试源码目录 | <testSourceDirectory>src/test/java</testSourceDirectory> |
<build>/<outputDirectory> | 编译输出目录 | <outputDirectory>target/classes</outputDirectory> |
<build>/<testOutputDirectory> | 测试编译输出目录 | <testOutputDirectory>target/test-classes</testOutputDirectory> |
7.2<resources>- 资源文件
| 标签 | 说明 | 示例 |
|---|---|---|
<resources> | 资源列表容器 | <resources>...</resources> |
<resource> | 单个资源配置 | <resource>...</resource> |
<resource>/<directory> | 资源目录 | <directory>src/main/resources</directory> |
<resource>/<includes> | 包含的文件模式 | <includes>...</includes> |
<resource>/<excludes> | 排除的文件模式 | <excludes>...</excludes> |
<resource>/<filtering> | 是否启用资源过滤(替换变量) | <filtering>true</filtering> |
7.3<plugins>- 插件配置
| 标签 | 说明 | 示例 |
|---|---|---|
<plugins> | 插件列表容器 | <plugins>...</plugins> |
<plugin> | 单个插件配置 | <plugin>...</plugin> |
<plugin>/<groupId> | 插件组织 ID | <groupId>org.springframework.boot</groupId> |
<plugin>/<artifactId> | 插件构件 ID | <artifactId>spring-boot-maven-plugin</artifactId> |
<plugin>/<version> | 插件版本 | <version>3.5.10</version> |
<plugin>/<extensions> | 是否扩展 Maven 核心功能 | <extensions>true</extensions> |
<plugin>/<configuration> | 插件配置参数 | <configuration>...</configuration> |
<plugin>/<executions> | 插件执行目标 | <executions>...</executions> |
<plugin>/<executions>/<execution> | 单个执行配置 | <execution>...</execution> |
<execution>/<id> | 执行 ID | <id>repackage</id> |
<execution>/<goals> | 执行的目标列表 | <goals>...</goals> |
<execution>/<goal> | 单个目标 | <goal>repackage</goal> |
<execution>/<phase> | 绑定的生命周期阶段 | <phase>package</phase> |
<execution>/<configuration> | 该执行的特定配置 | <configuration>...</configuration> |
7.4<pluginManagement>- 插件版本管理
| 标签 | 说明 | 示例 |
|---|---|---|
<pluginManagement> | 管理插件版本,子模块继承但不会自动执行 | <pluginManagement>...</pluginManagement> |
<pluginManagement>/<plugins> | 同<plugins> | 同<plugins> |
八、分发管理
| 标签 | 说明 | 示例 |
|---|---|---|
<distributionManagement> | 分发配置 | <distributionManagement>...</distributionManagement> |
<repository> | 发布仓库 | <repository>...</repository> |
<snapshotRepository> | 快照仓库 | <snapshotRepository>...</snapshotRepository> |
<site> | 站点部署配置 | <site>...</site> |
九、仓库配置
| 标签 | 说明 | 示例 |
|---|---|---|
<repositories> | 远程仓库列表 | <repositories>...</repositories> |
<repository> | 单个仓库 | <repository>...</repository> |
<repository>/<id> | 仓库 ID | <id>aliyun</id> |
<repository>/<name> | 仓库名称 | <name>Aliyun Maven</name> |
<repository>/<url> | 仓库地址 | <url>https://maven.aliyun.com/...</url> |
<repository>/<snapshots> | 快照版本策略 | <snapshots>...</snapshots> |
<repository>/<releases> | 正式版本策略 | <releases>...</releases> |
十、常用插件配置示例
10.1 Spring Boot Maven Plugin
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><skip>false</skip><!-- 是否跳过 repackage --><mainClass>com.example.Application</mainClass><!-- 启动类 --><classifier>exec</classifier><!-- 生成带后缀的 jar --><jvmArguments><!-- JVM 参数 --><jvmArgument>-Dfile.encoding=UTF-8</jvmArgument></jvmArguments><systemPropertyVariables><!-- 系统属性 --><file.encoding>UTF-8</file.encoding></systemPropertyVariables><arguments><!-- 应用参数 --><argument>--server.port=8080</argument></arguments><excludes><!-- 排除依赖 --><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration><executions><execution><goals><goal>repackage</goal><!-- 重打包目标 --></goals></execution></executions></plugin>10.2 Maven Compiler Plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>17</source><!-- 源码版本 --><target>17</target><!-- 目标版本 --><encoding>UTF-8</encoding><!-- 编码 --><compilerArgs><!-- 额外参数 --><arg>-parameters</arg><!-- 保留参数名 --><arg>-Xlint:unchecked</arg><!-- 警告级别 --></compilerArgs></configuration></plugin>10.3 Maven Surefire Plugin(测试)
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skipTests>true</skipTests><!-- 跳过测试 --><includes><!-- 包含的测试类 --><include>**/*Test.java</include></includes><excludes><!-- 排除的测试类 --><exclude>**/*IntegrationTest.java</exclude></excludes><forkCount>4</forkCount><!-- 并行测试数 --><reuseForks>true</reuseForks><!-- 复用 fork --></configuration></plugin>10.4 Maven Jar Plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><excludes><!-- 排除文件 --><exclude>**/application-dev.yml</exclude></excludes><archive><manifest><addClasspath>true</addClasspath><!-- 添加 Classpath --><mainClass>com.example.Application</mainClass><!-- 主类 --></manifest><manifestEntries><!-- 自定义 Manifest --><Implementation-Version>${project.version}</Implementation-Version></manifestEntries></archive></configuration></plugin>10.5 Maven Source Plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><phase>package</phase><goals><goal>jar-no-fork</goal><!-- 生成源码 jar --></goals></execution></executions></plugin>