Spring Boot 父子工程 POM 依赖关系详解

发布于:2025-07-04 ⋅ 阅读:(21) ⋅ 点赞:(0)

1. 父子工程概述

Spring Boot 父子工程是一种多模块项目结构,通过 Maven 的继承机制来管理依赖关系。这种结构具有以下优势:

  • 统一版本管理:所有子模块使用相同的依赖版本
  • 代码复用:公共代码可以提取到独立模块
  • 模块化开发:不同功能模块独立开发和维护
  • 依赖隔离:避免循环依赖,明确模块职责

2. 项目结构

spring-boot-parent/
├── parent-pom.xml          # 父工程POM
├── common/                 # 公共工具模块
│   └── pom.xml
├── api/                    # API接口模块
│   └── pom.xml
├── service/                # 业务服务模块
│   └── pom.xml
└── web/                    # Web应用启动模块
    └── pom.xml

3. 父工程 POM 详解

3.1 基本信息配置

<groupId>com.example</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>  <!-- 关键:父工程必须是pom类型 -->

3.2 子模块声明

<modules>
    <module>common</module>
    <module>api</module>
    <module>service</module>
    <module>web</module>
</modules>

3.3 版本统一管理

<properties>
    <spring-boot.version>3.2.0</spring-boot.version>
    <spring-cloud.version>2023.0.0</spring-cloud.version>
    <mysql.version>8.0.33</mysql.version>
    <!-- 其他版本号... -->
</properties>

3.4 依赖管理 (dependencyManagement)

重要概念dependencyManagement 不会直接引入依赖,只是管理版本号。

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot 依赖管理 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>  <!-- 导入Spring Boot的依赖管理 -->
        </dependency>
        
        <!-- 自定义依赖版本管理 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        
        <!-- 内部模块依赖管理 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

3.5 公共依赖 (dependencies)

所有子模块都会继承这些依赖:

<dependencies>
    <!-- 所有子模块都会自动包含这些依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

3.6 插件管理 (pluginManagement)

<build>
    <pluginManagement>
        <plugins>
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

4. 子模块 POM 详解

4.1 继承父工程

<parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>1.0.0</version>
    <relativePath>../parent-pom.xml</relativePath>
</parent>

4.2 模块特有依赖

<dependencies>
    <!-- 依赖其他子模块 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>common</artifactId>
        <!-- 不需要指定版本,由父工程管理 -->
    </dependency>
    
    <!-- 依赖外部库 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <!-- 不需要指定版本,由父工程管理 -->
    </dependency>
</dependencies>

5. 依赖关系层次

5.1 依赖传递关系

web (启动模块)
├── service (业务服务)
│   ├── api (API接口)
│   │   └── common (公共工具)
│   └── common (公共工具)
└── common (公共工具)

5.2 各模块职责

模块 职责 主要依赖
common 公共工具类、常量、异常 hutool、fastjson、validation
api API接口定义、DTO common、swagger
service 业务逻辑、数据访问 api、common、mybatis、redis
web 应用启动、控制器 service、actuator、devtools

6. 关键概念解析

6.1 dependencyManagement vs dependencies

  • dependencyManagement:版本管理,不引入依赖
  • dependencies:实际引入依赖
<!-- 父工程:只管理版本 -->
<dependencyManagement>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>
</dependencyManagement>

<!-- 子模块:实际引入依赖 -->
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <!-- 版本由父工程管理 -->
    </dependency>
</dependencies>

6.2 继承机制

子模块自动继承父工程的:

  • 公共依赖
  • 版本管理
  • 插件配置
  • 属性定义

6.3 依赖作用域 (Scope)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>  <!-- 仅测试时使用 -->
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>  <!-- 不会传递给依赖方 -->
</dependency>

7. 最佳实践

7.1 版本管理

  • 所有版本号统一在父工程 properties 中定义
  • 使用 ${property.name} 引用版本号
  • 定期更新依赖版本

7.2 模块划分

  • 按功能职责划分模块
  • 避免循环依赖
  • 保持模块独立性

7.3 依赖管理

  • 公共依赖放在父工程
  • 模块特有依赖放在子模块
  • 合理使用依赖作用域

7.4 构建配置

  • 只有启动模块配置 spring-boot-maven-plugin
  • 统一编译和测试插件版本
  • 配置合适的打包策略

8. 常见问题

8.1 依赖冲突

  • 使用 mvn dependency:tree 查看依赖树
  • 在父工程中排除冲突依赖
  • 使用 exclusions 标签排除传递依赖

8.2 版本不一致

  • 检查父工程版本管理是否完整
  • 确保子模块正确继承父工程
  • 验证 relativePath 配置是否正确

8.3 构建失败

  • 检查模块依赖关系是否正确
  • 验证插件配置是否兼容
  • 确认 Java 版本设置一致

9. 总结

Spring Boot 父子工程通过 Maven 的继承机制实现了:

  • 统一管理:版本、依赖、插件统一管理
  • 模块化:功能模块独立开发和维护
  • 复用性:公共代码和配置复用
  • 可维护性:清晰的依赖关系和模块职责

这种结构特别适合中大型项目的开发和维护,能够有效提高开发效率和代码质量。