type
Post
status
Published
date
Jul 2, 2021
slug
summary
Maven 父子聚合工程项目搭建实例
tags
Maven
项目方案
category
技术分享
icon
password
平日普通的 Java 工程目录结构中,常在单独的模块中开发
Maven 聚合工程可以让多个普通的包拆分到多个模块中,模块之间相互依赖,也能分开部署
在分布式微服务项目中使用的非常多

操作步骤

构建父工程

首先创建一个普通的 Maven 父工程
  • 在父工程的 POM 文件中,修改打包方式为 pom:
    • <packaging>pom</packaging>
  • 使用 dependencyManagement 标签定义依赖
    • 父工程他只负责管理依赖,以及依赖的版本,而不会导入额外的jar依赖。 如此一来父工程的职责就很单一了,而且也符合了面向对象开发的父子继承关系, 依赖的导入只有在各自的子工程中才会进行导入
      <dependencyManagement> <dependencies> <!-- mysql 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot-starter.version}</version> </dependency> <!-- 通用mapper逆向工具 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mapper-spring-boot-starter.version}</version> </dependency> <!--pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper-spring-boot-starter.version}</version> </dependency> ..... </dependencies> </dependencyManagement>

      构建子工程

      选中当前模块并新建普通 Maven 项目模块,IDEA 中勾选父工程
      notion image
      此时子模块的 pom.xml 文件中会自动添加对父工程的依赖
      <parent> <artifactId>YXVideo</artifactId> <groupId>xyz.wvuuvw</groupId> <version>1.0-SNAPSHOT</version> </parent>
      同时,父模块的的 pom.xml 文件中将会声明添加的子模块
      <modules> <module>yxvideo-common</module> <module>yxvideo-model</module> <module>yxvideo-mapper</module> <module>yxvideo-service</module> <module>yxvideo-api</module> </modules>

      项目实例

      按照上述操作构建父工程和子工程,目录结构如下
      notion image

      工程结构

      典型的 MVC 模式 web 项目的工程结构如下:
    • xx-common:通用工程,包含一些工具类,枚举类,自定义异常,封装的公共方法
    • xx-model: 模型工程,所有的子工程以及所有(微)服务工程所涉及到的实体类都可以再此处管理 比如:POJO, Bean, Entity, BO, VO, DTO, MO, EO...
    • xx-mapper:数据层,操作数据库(DAO层)
    • xx-service:业务逻辑层
    • xx-api:接口层(Controller)
    • 工程依赖

      类似于单体项目各个包之间的依赖关系,子工程中也存在类似的依赖,需要在 Pom.xml 中定义
      common → model → mapper → service → api
      <!-- xx-model --> <dependencies> <dependency> <groupId>xyz.wvuuvw</groupId> <artifactId>yxvideo-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!-- xx-mapper --> <dependencies> <dependency> <groupId>xyz.wvuuvw</groupId> <artifactId>yxvideo-model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!-- xx-service --> <dependencies> <dependency> <groupId>xyz.wvuuvw</groupId> <artifactId>yxvideo-mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!-- xx-api --> <dependencies> <dependency> <groupId>xyz.wvuuvw</groupId> <artifactId>yxvideo-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
Go语言文件处理相关库使用EasyYapi — 接口文档和调试工具