ToB企服应用市场:ToB评测及商务社交产业平台

标题: Maven多模块项目架构配置介绍和实战 [打印本页]

作者: 西河刘卡车医    时间: 2023-7-15 14:49
标题: Maven多模块项目架构配置介绍和实战
原文地址:https://ntopic.cn/p/2023071501/
源代码先行:
背景介绍

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

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

模块的划分没有任何强制要求,一般的划分思路如下:
根据上面说明,https://gitee.com/obullxl/ntopic-boot的模块划分如下:
IDEA代码结构GitHub代码样例第二步:确定各个模块的依赖关系

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

第三步:Maven多模块实操

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

总pom.xml配置了整个项目的所有信息,包括项目的模块列表、依赖的JAR包、仓库和打包方式等。
  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>
复制代码
  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] ------------------------------------------------------------------------
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4