Maven多模块项目架构配置介绍和实战

打印 上一主题 下一主题

主题 939|帖子 939|积分 2817

原文地址:https://ntopic.cn/p/2023071501/
源代码先行:
背景介绍

我们项目采用的是Maven多模块架构,我发现项目的部分子模块的pom.xml中重复引用了相同的JAR包。很明显,当初在配置Maven模块的时候,没有考虑清楚各个模块的架构职责,同时也不了解Maven模块依赖的传递性。本文主要介绍一下Maven多模块的配置思路和多模块的配置实操。
Maven多模块配置

在实操之前,我们先要了解配置概览,配置大致可分为三大步:确定项目需要哪几个模块,项目中的每个模块的依赖关系如何,最后根据依赖关系配置。
第一步:确定项目的模块划分

模块的划分没有任何强制要求,一般的划分思路如下:

  • test 集成测试模块:该模块除了测试用例代码外,没有实际业务逻辑。项目中我们用junit和Mockito测试框架比较多,Mockito集成测试框架和SpringBoot集成可我之前的博客:https://ntopic.cn/p/2023052001/
  • facade 对外服务接口模块(如果有的话,一般业务类项目较小,服务类较大):主要是对外提供服务接口、请求参数和返回结果等JAR包,其他项目需要依赖项目JAR包,因此需要单独一个模块
  • client 集成外部服务接口模块:和facade对应,如果依赖了外部服务的话
  • lang 基础公用模块:各个模块都会依赖的公共类模块,比如常用工具类、通用枚举等
  • das 数据访问模块:接入DB层的代码,包括DO/DAO等
  • service 领域核心服务模块:各个业务领域的核心服务逻辑
  • web/biz 业务模块:业务逻辑模块。如果业务比较复杂,可进一步拆分,如web-user/web-order等
根据上面说明,https://gitee.com/obullxl/ntopic-boot的模块划分如下:

  • ntopic-test 集成测试模块
  • ntopic 业务逻辑模块,包括SpringBoot启动类、Web模块、业务模块等
  • ntopic-servcie 领域核心服务
  • ntopic-client 外部服务器集成
  • ntopic-das DB操作
  • ntopic-lang 公共模块
IDEA代码结构GitHub代码样例第二步:确定各个模块的依赖关系

Maven模块的依赖具备传递性,即:若模块A依赖了模块B,模块B依赖了模块C,则模块A自动依赖了模块C(有点类似于数学中数值大小的传递性,A>B且B>C,则A>C)。
https://gitee.com/obullxl/ntopic-boot项目的各个模块依赖关系说明:

  • ntopic-test 集成测试模块:因为集成测试包括了所有的代码逻辑,所以它处于最上层
  • ntopic 业务模块,业务逻辑依赖service模块提供核心服务
  • ntopic-service 核心服务模块,它依赖DB数据读写和引入外部服务
  • ntopic-lang 公共逻辑模块,它进一步依赖其他统一模块,如commons模块等

第三步:Maven多模块实操

多模块的核心在pom.xml文件中,任何一个pom.xml模块,都需要指定五个核心配置元素:

  • groupId 代表大分组,一般都是公司的域名,如 cn.ntopic / com.aliaba 等
  • artifactId 代表具体的JAR包名,如 sequence-jdbc / fastjson 等
  • version 代表JAR包版本,如 1.0.1 / 1.2.76 等
  • packaging 代表模块打包方式,默认都是jar,对于多模块的总模块或者父模块为pom
  • name 代表模块名称,可选配置,建议配置
总pom.xml文件

总pom.xml配置了整个项目的所有信息,包括项目的模块列表、依赖的JAR包、仓库和打包方式等。

  • 指定父模块:可选的,可以没有父模块。如ntopic-boot是基于SpringBoot框架,所以它的父模块是SpringBoot的。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>org.springframework.boot</groupId>
  7.         <artifactId>spring-boot-starter-parent</artifactId>
  8.         <version>2.5.3</version>
  9.         <relativePath/>
  10.     </parent>
  11.     <groupId>ntopic</groupId>
  12.     <artifactId>ntopic-parent</artifactId>
  13.     <version>1.0.1</version>
  14.     <packaging>pom</packaging>
  15.     <name>ntopic-parent</name>
复制代码

  • 指定模块本项目的模块列表:
  1.     <modules>
  2.         <module>ntopic-test</module>
  3.         <module>ntopic</module>
  4.         <module>ntopic-service</module>
  5.         <module>ntopic-das</module>
  6.         <module>ntopic-client</module>
  7.         <module>ntopic-lang</module>
  8.     </modules>
复制代码

  • 模块依赖管理,各个子模块中,可以直接使用这里配置的依赖:
  1.     <dependencyManagement>
  2.       <dependencies>
  3.               
  4.               <dependency>
  5.                   <groupId>ntopic</groupId>
  6.                   <artifactId>ntopic-lang</artifactId>
  7.                   <version>${ntopic.version}</version>
  8.               </dependency>
  9.               <dependency>
  10.                   <groupId>ntopic</groupId>
  11.                   <artifactId>ntopic-client</artifactId>
  12.                   <version>${ntopic.version}</version>
  13.               </dependency>
  14.         
  15.         
  16.         <dependency>
  17.             <groupId>org.springframework.boot</groupId>
  18.             <artifactId>spring-boot-starter-jdbc</artifactId>
  19.             <version>${springboot.boot.version}</version>
  20.         </dependency>
  21.         
  22.         <dependency>
  23.             <groupId>com.google.guava</groupId>
  24.             <artifactId>guava</artifactId>
  25.             <version>30.1.1-jre</version>
  26.         </dependency>
  27.         
  28.         </dependencies>
  29.     </dependencyManagement>
复制代码

  • 额外的Maven仓库和打包方式配置:
  1.     <repositories>
  2.         <repository>
  3.             <id>Gitee-obullxl</id>
  4.             <url>https://gitee.com/obullxl/maven-repository/raw/sequence-jdbc</url>
  5.         </repository>
  6.     </repositories>
  7.     <build>
  8.         <plugins>
  9.             <plugin>
  10.                 <groupId>org.springframework.boot</groupId>
  11.                 <artifactId>spring-boot-maven-plugin</artifactId>
  12.                 <version>${springboot.boot.version}</version>
  13.             </plugin>
  14.         </plugins>
  15.     </build>
复制代码
子模块pom.xml文件

总模块配置好了之后,子模块的配置就简单多了,只需要配置3个信息块:父模块、四元素和依赖其它子模块。其中依赖的子模块按照第二步中的依赖关系配置即可:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>ntopic</groupId>
  7.         <artifactId>ntopic-parent</artifactId>
  8.         <version>1.0.1</version>
  9.     </parent>
  10.     <artifactId>ntopic-service</artifactId>
  11.     <packaging>jar</packaging>
  12.     <version>${ntopic.version}</version>
  13.     <name>ntopic-service</name>
  14.     <dependencies>
  15.         
  16.         <dependency>
  17.             <groupId>ntopic</groupId>
  18.             <artifactId>ntopic-das</artifactId>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>ntopic</groupId>
  22.             <artifactId>ntopic-client</artifactId>
  23.         </dependency>
  24.     </dependencies>
  25. </project>
复制代码
编译项目各个模块

经过上面的配置,项目Maven多模块架构已经配置完成,进行项目编译即可:
  1. mvn compile
复制代码
编译成功的结果:
  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] Reactor Summary:
  3. [INFO]
  4. [INFO] ntopic-parent ...................................... SUCCESS [  0.022 s]
  5. [INFO] ntopic-lang ........................................ SUCCESS [  0.873 s]
  6. [INFO] ntopic-das ......................................... SUCCESS [  0.121 s]
  7. [INFO] ntopic-client ...................................... SUCCESS [  0.042 s]
  8. [INFO] ntopic-service ..................................... SUCCESS [  0.055 s]
  9. [INFO] ntopic ............................................. SUCCESS [  0.073 s]
  10. [INFO] ntopic-test ........................................ SUCCESS [  0.114 s]
  11. [INFO] ------------------------------------------------------------------------
  12. [INFO] BUILD SUCCESS
  13. [INFO] ------------------------------------------------------------------------
  14. [INFO] Total time: 2.094 s
  15. [INFO] Finished at: 2023-07-15T13:41:57+08:00
  16. [INFO] Final Memory: 35M/448M
  17. [INFO] ------------------------------------------------------------------------
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表