Maven POM 参考
什么是 POM ?
POM(Project Object Model)称为“项目对象模型”。它是 Maven 项目的 XML 表示形式,保存在名为 pom.xml
的文件中。
概述
这是 pom.xml 中的元素列表。注意,modelVersion
的值为 4.0.0。这是目前唯一受支持的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
最小配置
POM 包含关于项目的所有必要信息,以及在构建过程中使用的插件配置。一个 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
</project>
Maven 坐标
上面定义的 POM 是 Maven 允许的最小值。groupId:artifactId:version
都是必需的元素(不过,如果从父 POM 继承 groupId 和 version,则不需要显式定义它们——后面将详细介绍继承)。这标记了存储库中的一个特定位置,就像 Maven 项目的一个坐标系统:
- groupId: 这在一个组织或一个项目中是独一无二的。例如,所有核心 Maven artifacts 都(应该)位于
groupId
org.apache.maven
下。 - artifactId: 它与
groupId
一起组成一个标识,将这个项目与世界上的所有其他项目区分开来。 - version: groupId:artifactId 标识一个项目,但它们不能描述确切的版本。代码的更改应该进行版本控制,而此元素就是用来控制这些版本的。
上面给出的三个元素唯一指向一个项目及特定版本。
packaging
<packaging>
元素用来设置项目的打包方式。在上面的例子中,org.codehaus.mojo:my-project:1.0
的示例 POM 将默认打包为一个 jar
。我们可以指定不同的打包方式把它变成一个 war
:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<packaging>war</packaging>
...
</project>
当没有声明打包方式时,Maven默认的打包方式是 jar
。常见的值有 pom,jar,war。
POM 之间的关系
Maven 的一个强大功能是它对项目关系的处理。这包括依赖(和传递依赖)、继承和聚合(多模块项目)。
依赖
POM 的基石是它的依赖列表。大多数项目都依赖于其他项目来正确地构建和运行。Maven 引入了这些依赖及依赖的依赖(传递依赖),允许您的列表只关注项目所需的依赖。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
...
</project>
-
groupId,artifactId,version:
你会经常看到这些元素。它们用于指定特定项目的 Maven 坐标,将其作为该项目的依赖项。这些值应该是:
- groupId, artifactId: 直接对应依赖的坐标,
- version: 用于获取依赖的有效版本。
-
scope:
有五种可用的范围:
- compile - 这是默认 scope。compile 依赖在所有类路径中都可用。此外,这些依赖会被传递到相关的项目中。
- provided - 这与 compile 非常相似,但表示您希望 JDK 或容器在运行时提供它。它只在编译和测试类路径上可用,并且不是可传递的。
- runtime - 此 scope 表示编译不需要依赖,但运行时需要。它在运行时和测试类路径中,但不在编译类路径中。
- test - 此 scope 表示仅在测试编译和执行阶段可用。它是不可传递的。
- system - 这个 scope 与 provided 类似,只是您必须提供显式包含它的 JAR。该 artifact 总是可用的,而不是在存储库中查找。
Exclusions
Exclusions 告诉 Maven 不要包含作为该依赖的依赖(它的传递依赖)的指定项目。例如,maven-embedder
需要 maven-core
,而我们不希望使用它或它的依赖项:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>
- exclusions: Exclusions 包含一个或多个
exclusion
元素,每个元素包含一个groupId
和一个artifactId
,表示要排除的依赖。与optional
不同,exclusions
会主动地从依赖树中移除自己,而optional
可能会安装或不使用。
继承
Maven为构建管理带来的一个强大的附加功能是项目继承的概念。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
</project>
parent 和 aggregation (多模块)项目的 packaging
类型必须是 pom
。这些类型定义了与一组生命周期阶段绑定的目标。例如,如果打包是 jar,那么打包阶段将执行 jar:jar 目标。现在我们可以向父 POM 添加值,它将由其子 POM 继承。
- groupId
- version
- description
- url
- inceptionYear
- organization
- licenses
- developers
- contributors
- mailingLists
- scm
- issueManagement
- ciManagement
- properties
- dependencyManagement
- dependencies
- repositories
- pluginRepositories
- build
- plugin executions with matching ids
- plugin configuration
- etc.
- reporting
- profiles
未继承的元素包括:
- artifactId
- name
- prerequisites
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>
注意 relativePath 元素。它不是必需的,但是可以用作 Maven 的一个标志,在搜索本地存储库和远程存储库之前,首先搜索为这个项目的父库提供的路径。
Dependency Management
通过 dependencyManagement
,父 POM 可以为子 POM 和传递依赖配置值。
-
dependencyManagement:
POM 使用它来帮助管理所有子节点之间的依赖关系信息。如果
my-parent
使用dependencyManagement
在junit:junit:4.12
上定义一个依赖,那么从这个项目继承的 POM 可以设置它们的依赖项,只提供 groupId=junit 和 artifactId=junit, Maven将填充父项目设置的版本。这种方法的好处是显而易见的。可以在一个中心位置设置依赖项详细信息,该位置将传播到所有继承的 POM。
聚合(或多模块)
有多个模块的项目称为多模块或聚合器项目。模块是 POM 列出的项目,并作为一个组执行。一个 pom 打包的项目可以通过将一组项目作为模块列出来聚合它们的构建,这些模块是到这些项目的目录或 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
<modules>
<module>my-project</module>
<module>another-project</module>
<module>third-project/pom-example.xml</module>
</modules>
</project>
在列出模块时,您不需要自己考虑模块间的依赖关系;即 POM 给出的模块顺序并不重要。Maven 将对模块进行拓扑排序,以便总是在构建依赖模块之前构建依赖项。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示