程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 依赖管理 - Maven

    • Maven 安装配置
    • Maven 仓库解析
    • Maven 项目构建
    • Maven 依赖管理
    • 手动安装Maven依赖
    • Maven 聚合
    • Maven 继承
      • 1. 继承的作用
      • 2. 继承的基本配置
        • 2.1 子模块中定义父模块
      • 3. 父模块中的依赖管理
        • 3.1 父模块中的依赖管理配置
        • 3.2 子模块中继承依赖
      • 4. 继承的好处
      • 5. 继承与聚合的区别
      • 6. 继承与聚合的相同点与不同点
      • 7. Maven 继承示例
        • 1. 父模块的 pom.xml 配置
        • 2. 子模块 A 的 pom.xml 配置
        • 3. 子模块 B 的 pom.xml 配置
        • 4. 结合聚合与继承
    • Maven 属性
    • Maven 版本与测试
    • Maven 资源与多环境配置
    • Maven 生命周期与插件
    • 本地搭建Maven私服
    • Docker搭建Maven私服
    • Maven 上传中央仓库
    • Maven 依赖排除插件
  • 开发管理 - Git

  • Git小乌龟教程

  • 代码管理
  • 依赖管理 - Maven
scholar
2024-08-20
目录

Maven 继承

# Maven 继承

  • 1. 继承的作用
  • 2. 继承的基本配置
    • 2.1 子模块中定义父模块
  • 3. 父模块中的依赖管理
    • 3.1 父模块中的依赖管理配置
    • 3.2 子模块中继承依赖
  • 4. 继承的好处
  • 5. 继承与聚合的区别
  • 6. 继承与聚合的相同点与不同点
  • 7. Maven 继承示例
    • 1. 父模块的 pom.xml 配置
    • 2. 子模块 A 的 pom.xml 配置
    • 3. 子模块 B 的 pom.xml 配置
    • 4. 结合聚合与继承

# 1. 继承的作用

Maven 继承(Inheritance)允许子模块继承父模块的配置。类似于 Java 中的类继承,Maven 继承让多个模块能够共享相同的配置,如依赖、插件、构建设置等,减少重复配置并提升管理效率。

# 2. 继承的基本配置

在子模块的 pom.xml 文件中,通过定义父模块的坐标和位置来实现继承:

# 2.1 子模块中定义父模块

<project>
    <modelVersion>4.0.0</modelVersion>

    <!-- 定义父模块 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
        <!-- 父模块的 POM 文件路径 -->
        <relativePath>../parent-project/pom.xml</relativePath>
    </parent>

    <!-- 子模块的自身信息 -->
    <groupId>com.example</groupId>
    <artifactId>child-project</artifactId>
    <version>1.0.0</version>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

在这个配置中,子模块通过 <parent> 元素指定了父模块的 groupId、artifactId、version 以及父模块的 pom.xml 文件路径(相对路径)。子模块继承了父模块的所有配置,如依赖管理、插件配置等。

# 3. 父模块中的依赖管理

在父模块中,通常使用 dependencyManagement 来统一管理依赖的版本。dependencyManagement 仅提供依赖版本管理,不会实际引入依赖,具体的依赖需要在子模块中声明。

# 3.1 父模块中的依赖管理配置

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging> <!-- 父模块的打包类型为 pom -->

    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.5.4</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 3.2 子模块中继承依赖

在子模块中,只需声明需要使用的依赖,而不需要指定版本号,版本号由父模块中的 dependencyManagement 提供:

<project>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

在上述配置中,子模块继承了父模块的版本管理,只需声明依赖的 groupId 和 artifactId,无需指定版本。

# 4. 继承的好处

  • 集中管理依赖版本:当多个子模块使用相同的依赖时,只需在父模块更新版本,所有子模块都会同步更新,避免在每个子模块中手动维护依赖版本。
  • 减少重复配置:父模块可以统一配置插件、构建配置等,子模块自动继承这些配置,简化开发。
  • 提升项目结构清晰度:通过继承,模块间的关系更为清晰,配置更加集中和易于管理。

# 5. 继承与聚合的区别

Maven 中的继承和聚合经常结合使用,但它们的功能和作用有明显区别:

  • 继承(Inheritance):用于共享配置。子模块通过继承父模块的 pom.xml,共享依赖管理、插件配置等内容。
  • 聚合(Aggregation):用于统一构建。父模块通过列出子模块路径,实现对多个子模块的统一管理和构建。

# 6. 继承与聚合的相同点与不同点

相同点:

  • 聚合与继承的父模块 pom.xml 文件打包类型均为 pom,可以将两者的配置放在同一个 pom.xml 文件中。
  • 聚合与继承都属于设计型模块,本身没有实际的业务代码,主要用于项目管理。

不同点:

  • 聚合:在父模块中配置参与聚合的子模块,父模块感知到有哪些子模块被聚合。
  • 继承:在子模块中配置继承关系,父模块无法感知到哪些子模块继承了自己。

# 7. Maven 继承示例

假设我们有一个大型的企业项目,项目中包含多个子模块,每个子模块负责不同的业务功能。我们希望统一管理这些模块的依赖和构建配置,同时在子模块中只关注业务逻辑。为此,我们可以使用 Maven 的继承功能来实现这个需求。

项目结构

parent-project/
│
├── pom.xml (父模块的 POM 文件)
│
├── module-a/
│   └── pom.xml (子模块 A)
├── module-b/
│   └── pom.xml (子模块 B)
└── module-c/
    └── pom.xml (子模块 C)
1
2
3
4
5
6
7
8
9
10

# 1. 父模块的 pom.xml 配置

父模块主要用于统一管理依赖和配置,因此它的打包类型为 pom。在父模块中,我们使用 dependencyManagement 来管理依赖的版本,同时配置一些公共的插件和构建设置。

<project>
    <modelVersion>4.0.0</modelVersion>
    
    <!-- 父模块的基本信息 -->
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging> <!-- 打包类型为 pom,表示这是一个管理项目 -->

    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.5.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.12.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 公共插件配置 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

在这个配置中:

  • dependencyManagement 中声明的依赖版本会被子模块继承,但子模块需要显式声明依赖才能使用这些库。
  • 公共插件(如 maven-compiler-plugin 和 maven-surefire-plugin)的配置会被所有子模块继承。

# 2. 子模块 A 的 pom.xml 配置

子模块 A 继承了父模块的配置,可以直接使用父模块中定义的依赖和插件配置:

<project>
    <modelVersion>4.0.0</modelVersion>
    
    <!-- 继承父模块 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath> <!-- 父模块的相对路径 -->
    </parent>

    <artifactId>module-a</artifactId> <!-- 子模块 A 的标识 -->

    <dependencies>
        <!-- 引用父模块中管理的依赖,无需指定版本 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

在这个配置中:

  • 子模块 A 继承了父模块的依赖管理,因此只需声明依赖的 groupId 和 artifactId,无需指定版本。
  • 公共插件和配置(如编译配置)也自动应用到子模块 A 中。

# 3. 子模块 B 的 pom.xml 配置

子模块 B 也继承了父模块的配置,但它可以根据自身需求添加额外的依赖:

<project>
    <modelVersion>4.0.0</modelVersion>
    
    <!-- 继承父模块 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>module-b</artifactId> <!-- 子模块 B 的标识 -->

    <dependencies>
        <!-- 引用父模块中管理的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 添加子模块 B 的独特依赖 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

在这个配置中:

  • 子模块 B 继承了父模块的依赖和插件配置,并额外添加了一个独有的依赖 commons-lang3。
  • 这样,子模块既可以共享父模块的配置,又能根据自身需求进行扩展。

# 4. 结合聚合与继承

在实际项目中,继承和聚合常常结合使用。父模块不仅管理依赖和配置,还聚合所有子模块,使得可以一次性构建整个项目:

<project>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <!-- 依赖管理和插件配置如前 -->
    
    <!-- 聚合子模块 -->
    <modules>
        <module>module-a</module>
        <module>module-b</module>
        <module>module-c</module>
    </modules>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

在这个配置中,父模块既起到依赖管理的作用,也起到聚合构建的作用。运行父模块时,所有子模块都会被一起构建。

总结

通过以上示例我们可以看出,Maven 的继承和聚合功能帮助项目团队有效管理复杂的多模块结构,减少重复配置,提升项目的一致性和可维护性。在大型项目中,这种结构能够显著提高开发和构建的效率。

编辑此页 (opens new window)
上次更新: 2024/12/28, 18:32:08
Maven 聚合
Maven 属性

← Maven 聚合 Maven 属性→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式