「Maven」- pom.xml

常用属性及其含义

repositories vs. pluginRepositories

maven – repository vs. pluginRepository – Stack Overflow

The PluginRepository is where maven searches for maven plugins. That is why it mentions builds (the build lifecycle) and reports (the site lifecycle). E.g. that is where the maven-compile-plugin and maven-surefire-plugin exists.

The normal repository is used for dependencies for your application, i.e. the sections <dependencies> and <dependencyManagement>. It could be your junit or commons-logging or whichever jar you needed.

Normally they could both point to the same repositories and in fact by default, they both point to Maven Central, but you could have some special locations you needed in there, or alternatively have different update policies on the repositories.

针对技术实现,两者并无较大差异。但是,通过两者,我们能够针对插件和依赖使用不同的管理策略。

模块聚合

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-module</module>
  </modules>
</project>

模块继承

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
  </parent>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

pom.xml 中的的 parent 什么意思

DeepSeek / pom.xml 中的的 parent 什么意思

# Maven POM 中的 parent 元素详解

在 Maven 的 `pom.xml` 文件中,`<parent>` 元素用于指定当前项目的父 POM,这是 Maven 继承机制的核心部分。

## 基本概念

`<parent>` 元素允许一个项目从另一个项目(父项目)继承配置信息,这样可以实现:

  1. 配置复用 – 避免重复定义相同的配置
  2. 统一管理 – 集中管理多个子项目的公共配置
  3. 标准化 – 确保项目遵循统一的标准和规范

## 基本语法

“`xml
<parent>

<groupId>com.example</groupId>

<artifactId>parent-project</artifactId>

<version>1.0.0</version>

<relativePath>../parent/pom.xml</relativePath> <!– 可选 –>

</parent>

“`

## 关键属性说明

  1. groupId, artifactId, version

– 这三个元素唯一标识父 POM

– 与普通依赖的坐标格式相同

  1. relativePath (可选)

– 指定父 POM 的相对路径

– Maven 会先查找该路径,找不到才去仓库查找

– 默认值是 `../pom.xml`

## 继承机制

子项目会继承父 POM 中的以下内容:

  1. 依赖管理 – `<dependencyManagement>` 中的依赖
  2. 插件配置 – `<pluginManagement>` 中的插件
  3. 属性 – `<properties>` 中定义的属性
  4. 报告配置 – 报表和报表插件配置
  5. 其他通用配置 – 如 groupId、version、描述信息等

## 典型使用场景

### 1. 企业级父 POM

“`xml
<!– 子项目 –>
<parent>

<groupId>com.company</groupId>

<artifactId>company-parent</artifactId>

<version>1.0.0</version>

</parent>

“`

企业级父 POM 通常包含:
– 统一的项目元数据
– 标准的依赖管理
– 公共插件配置
– 代码质量检查配置
– 构建和部署配置

### 2. Spring Boot 项目

“`xml
<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.7.0</version>

</parent>

“`

Spring Boot 父 POM 提供了:
– 预定义的依赖版本
– 默认的插件配置
– 资源过滤配置
– 默认的构建配置

### 3. 多模块项目

“`xml
<!– 子模块 –>
<parent>

<groupId>com.example</groupId>

<artifactId>multi-module-project</artifactId>

<version>1.0.0</version>

<relativePath>../pom.xml</relativePath>

</parent>

“`

在多模块项目中,父 POM 通常:
– 定义所有子模块共用的配置
– 管理模块间的依赖关系
– 集中控制构建顺序

## 覆盖继承

子项目可以覆盖从父 POM 继承的配置:

“`xml
<properties>

<!– 覆盖父POM中定义的属性 –>

<java.version>11</java.version>

</properties>

<dependencies>

<!– 添加新的依赖 –>

<dependency>
<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>
</dependency>

</dependencies>

“`

## 注意事项

  1. 版本继承 – 如果子项目不指定 version,会继承父项目的 version
  2. groupId 继承 – 如果子项目不指定 groupId,会继承父项目的 groupId
  3. artifactId 不继承 – artifactId 必须显式声明
  4. relativePath – 在多模块项目中,通常需要指定 relativePath
  5. 超级POM – 所有 POM 都隐式继承自 Maven 的超级 POM

## 实际案例

### 父 POM (parent/pom.xml)

“`xml
<project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>

<artifactId>parent-project</artifactId>

<version>1.0.0</version>

<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.8</java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<version>2.7.0</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>

“`

### 子 POM (child/pom.xml)

“`xml
<project>

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.example</groupId>

<artifactId>parent-project</artifactId>

<version>1.0.0</version>

<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>child-project</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<!– 不需要指定版本,从父POM继承 –>
</dependency>
</dependencies>

</project>

“`

通过 `<parent>` 元素,Maven 实现了强大的配置继承机制,大大简化了多项目管理和企业级项目的配置工作。