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

文章目录

    • 1. 父子工程概述
    • 2. 项目结构
    • 3. 父工程 POM 详解
      • 3.1 基本信息配置
      • 3.2 子模块声明
      • 3.3 版本统一管理
      • 3.4 依赖管理 (dependencyManagement)
      • 3.5 公共依赖 (dependencies)
      • 3.6 插件管理 (pluginManagement)
    • 4. 子模块 POM 详解
      • 4.1 继承父工程
      • 4.2 模块特有依赖
    • 5. 依赖关系层次
      • 5.1 依赖传递关系
      • 5.2 各模块职责
    • 6. 关键概念解析
      • 6.1 dependencyManagement vs dependencies
      • 6.2 继承机制
      • 6.3 依赖作用域 (Scope)
    • 7. 最佳实践
      • 7.1 版本管理
      • 7.2 模块划分
      • 7.3 依赖管理
      • 7.4 构建配置
    • 8. 常见问题
      • 8.1 依赖冲突
      • 8.2 版本不一致
      • 8.3 构建失败
    • 9. 总结

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.examplegroupId>
<artifactId>spring-boot-parentartifactId>
<version>1.0.0version>
<packaging>pompackaging>  

3.2 子模块声明

<modules>
    <module>commonmodule>
    <module>apimodule>
    <module>servicemodule>
    <module>webmodule>
modules>

3.3 版本统一管理

<properties>
    <spring-boot.version>3.2.0spring-boot.version>
    <spring-cloud.version>2023.0.0spring-cloud.version>
    <mysql.version>8.0.33mysql.version>
    
properties>

3.4 依赖管理 (dependencyManagement)

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

<dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-dependenciesartifactId>
            <version>${spring-boot.version}version>
            <type>pomtype>
            <scope>importscope>  
        dependency>
        
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
        dependency>
        
        
        <dependency>
            <groupId>com.examplegroupId>
            <artifactId>commonartifactId>
            <version>${project.version}version>
        dependency>
    dependencies>
dependencyManagement>

3.5 公共依赖 (dependencies)

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

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>
dependencies>

3.6 插件管理 (pluginManagement)

<build>
    <pluginManagement>
        <plugins>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${spring-boot.version}version>
            plugin>
        plugins>
    pluginManagement>
build>

4. 子模块 POM 详解

4.1 继承父工程

<parent>
    <groupId>com.examplegroupId>
    <artifactId>spring-boot-parentartifactId>
    <version>1.0.0version>
    <relativePath>../parent-pom.xmlrelativePath>
parent>

4.2 模块特有依赖

<dependencies>
    
    <dependency>
        <groupId>com.examplegroupId>
        <artifactId>commonartifactId>
        
    dependency>
    
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        
    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>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.33version>
    dependency>
dependencyManagement>


<dependencies>
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        
    dependency>
dependencies>

6.2 继承机制

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

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

6.3 依赖作用域 (Scope)

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <scope>testscope>  
dependency>

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <optional>trueoptional>  
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 的继承机制实现了:

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

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

你可能感兴趣的:(smart,Spring,spring,boot,后端,java)