当前位置: 首页 > news >正文

Maven 多模块管理

多模块管理简单地理解就是一个 Java 工程项目中不止有一个 pom.xml 文件,会在不同的目录中有多个这样的文件,进而实现 Maven 的多模块管理

在多人使用Maven协作开发项目时,尤其是稍微上点规模的项目,每个RD的工作都细分到具体功能和模块,有些模块甚至还要单独部署

我们假设有这样一个商城项目,包括以下几个模块:

  • 商城前台(shop)
  • 管理后台(admin)
  • 数据库交互模块(dao)
  • 通用业务模块(service)
  • 接口模块(api)
  • 通用工具(util)

其中shop和admin需要单独部署,dao、service、util你可能想要一些经验丰富的人来维护,如果使用一个应用来管理的话,所有的功能和模块都会耦合在一起,所有人都可以随意修改代码,这显然不是我们所期望的。

而且使用一个应用来管理的话,任何一个点的代码有变更,整个项目就需要重新build,使用模块化开发的另一个好处是如果dao的代码被修改,只需要重新build dao模块就可以了web模块可以build成war,dao、service、util等可以build成jar,只需要配置好依赖关系,就可以实现模块间的解耦合。这样的设计才是遵循“高内聚,低耦合”设计原则的。

我们使用上面的例子进行演示,先进行合理的优化,我们希望dao和service作为通用的底层工具来使用,把它们合并成一个核心模块(core)build成core.jar,简单的Maven模块化项目结构如下:

---------- mall         //顶级项目|------ pom.xml      //packaging = pom|------ mall-util    //通用工具|  |--- pom.xml      //packaging = jar|------ mall-core    //核心模块|  |--- pom.xml      //packaging = jar|------ mall-web-api //接口模块|  |--- pom.xml      //packaging = war|------ mall-web-admin//管理后台|  |--- pom.xml      //packaging = war|------ mall-web-shop//商城前台|  |--- pom.xml      //packaging = war

这些模块中api、admin、shop(war均是可以单独部署的web应用相互之间没有依赖关系,但都依赖于core模块,而core模块依赖于util模块。

模块拆分策略:推荐按照功能拆分,后期方便转换成微服务架构

按职责划分:
在这里插入图片描述

按功能拆分:
例如,在电商系统中如下module
在这里插入图片描述

创建项目

在这里插入图片描述
然后删掉src(父项目必须遵循),只保留:.idea 文件夹 、项目 pom 文件、以及一个 *.iml 文件

注意: 因为父模块只做依赖管理,不需要编写代码,所以 src 文件夹可以直接删除。编写parent的pom.xml只是为了在各个模块中减少重复的配置。

在项目下创建子模块:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
然后会发现module 的 pom 文件发生了变化:
在这里插入图片描述
新增了两段配置

<packaging>pom</packaging><modules><module>module-util</module>
</modules>

pom 是最简单的打包类型。不像jar和war,它生成的构件只有它本身。将 packaging 申明为 pom 则意味着没有代码需要测试或者编译,也没有资源需要处理。

由于我们使用了聚合,所以打包方式必须为pom,否则无法构建。

module的值是子模块相对于当前 POM 的路径。

再看子模块中的 pom:
在这里插入图片描述
也是分成两个部分

<parent><groupId>com.wqlm</groupId><artifactId>module</artifactId><version>1.0-SNAPSHOT</version>
</parent><artifactId>module-util</artifactId>
<parent><groupId>com.wqlm</groupId><artifactId>module</artifactId><version>1.0-SNAPSHOT</version><!--<relativePath/>-->
</parent>

声明了该模块继承自 com.wqlm:module:1.0-SNAPSHOT,其实这里面还省略了
<relativePath></relativePath> 由于 relativePath 默认是 …/pom.xml 而我们的子项目确实在父项目的下一级目录中,所以是可以不用填写的

Maven首先在当前构建项目的环境中查找父pom,然后项目所在的文件系统查找,然后是本地存储库,最后是远程repo。

artifactId 是子模块的组件id,由于继承了父pom,所以groupId、version 也可以不写,不写的话就默认继承自父pom

使用多模块

如上所示,在创建多个模块之后,可以在父pom中添加公共配置,然后所有的子模块都会继承这些配置。除此之外,还可以通用对子模块进行 编译、打包、安装… 操作

如果子模块间相互依赖,需要在 dependency 中引入要依赖的子模块,如图
在这里插入图片描述
上图中子模块 module-common:1.0-SNAPSHOT 依赖了 module-util:1.0-SNAPSHOT

子模块间的相互依赖,需要管理好依赖项的版本号,负责容易依赖版本冲突。

简单来说就是把公共依赖及版本号在父 pom 中申明,子项目引入依赖时只需要指定 groupId、artifactId 不需要指定版本号

如下,先在父 pom 中申明依赖及版本号
在这里插入图片描述
再在子项目中引入依赖项,注意,不需要指定版本号,默认查找父pom中定义的版本号

在这里插入图片描述

多项目实例

以一个普通 Spring Boot 项目为例,首先放一张图,看一下整体项目完成后的结构
在这里插入图片描述

其中目录结构为

- detail-page- detail-client- detail-service- detail-start
  • detail-client 用于放需要打包传到 maven 库的代码
  • detail-service 用于放置主要的业务逻辑代码
  • detail-start 用于放启动代码

其中需要注意的是 pom.xml 的文件的配置,该配置决定了父子模块之间的关系

- detail-page:父模块- detail-client:子模块,无依赖- detail-service:子模块,依赖detail-client- detail-start:子模块,依赖detail-service

注意:在依赖引用过程中,千万不可以出现循环依赖,比如 client 引用了 service,service 也引用了 client,如果出现这种情况 maven 在打包的时候会直接报错

1、父模块 detail-page 的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.drawcode</groupId><artifactId>detail-page</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging>  <!-- 此处必须为pom --><name>detail-page</name><properties><java.version>1.8</java.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><!-- modules即为父子关系 --><modules><module>detail-client</module><module>detail-service</module><module>detail-start</module></modules><!-- dependencyManagement非常重要,决定了子pom.xml是否可以直接引用父pom.xml的包 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!--注意这个包就是项目本身的模块--><dependency><groupId>com.drawcode</groupId><artifactId>detail-service</artifactId><version>${project.version}</version><!-- 这个版本就表示0.0.1-SNAPSHOT --></dependency><!--注意这个包就是项目本身的模块--><dependency><groupId>com.drawcode</groupId><artifactId>detail-client</artifactId><version>${project.version}</version></dependency></dependencies></dependencyManagement><build><plugins><!-- 注意此处为空 --></plugins></build></project>

detail-start 的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><!--parent使用的即为父pom.xml的信息--><parent><groupId>com.drawcode</groupId><artifactId>detail-page</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><artifactId>detail-start</artifactId><packaging>jar</packaging> <!-- 注意此处要配置为jar --><name>detail-start</name><!--子pom.xml不必添加dependencyManagement--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--这里可以看到因为父pom.xml已经引用了自身项目的包模块,所以这里可以不加version直接使用--><dependency><groupId>com.drawcode</groupId><artifactId>detail-service</artifactId></dependency></dependencies><build><plugins><!--因为启动类在detail-start中,所以此处必须添加该plugin--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

detail-service 的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.drawcode</groupId><artifactId>detail-page</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --></parent><modelVersion>4.0.0</modelVersion><artifactId>detail-service</artifactId><packaging>jar</packaging><name>detail-service</name><!--detail-service依赖于detail-client--><dependencies><dependency><groupId>com.drawcode</groupId><artifactId>detail-client</artifactId></dependency></dependencies>
</project>

detail-start 的 pom.xml
因为 detail-start 没有任何依赖所以比较简单

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.drawcode</groupId><artifactId>detail-page</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --></parent><modelVersion>4.0.0</modelVersion><artifactId>detail-client</artifactId><packaging>jar</packaging><name>detail-client</name><dependencies></dependencies><build></build></project>

其中建议除了各个子模块单独使用的包之外,其他的都要在父模块下的 pom.xml 中配置包信息,这样便于包的版本控制
在这里插入图片描述
项目内部存在了包的依赖之后,不同模块之间的代码即可进行使用,比如 detail-service 依赖 detail-client,那么 detail-client 中的 Test2 就可以被 detail-service 使用了
在这里插入图片描述
但是反过来 detail-client 不可以使用 detail-service 中的类,因为依赖是单向的关系

如何启动

启动指令如下

$ mvn clean install && mvn spring-boot:run -pl detail-start

其中 spring-boot:run 可以使用就是因为 spring-boot-maven-plugin 的存在

-pl detail-start 则代表的是有 application 启动类的子模块目录
在这里插入图片描述

http://www.lryc.cn/news/45424.html

相关文章:

  • crash 内核调试工具 ps 指令 显示的进程状态 RU, IN, UN, ZO, ST, TR, DE, SW, WA, PA 什么意思
  • Spring《二》bean的实例化与生命周期
  • java与kotlin 写法区别
  • 服务器运行深度学习代码使用指南
  • 计算机组成原理 - 2. 数据的表示和运算
  • 【js】基础知识点--语句,break和continue,switch,with,for..in,do-while,while
  • 【C++】迭代器
  • 数据可视化在前端中的应用
  • FFmpeg 合并视频文件没声音,不同步原因
  • 绕不开的“定位”
  • 《Effective Objective-C 2.0 》 阅读笔记 item12
  • 云原生计算能消除技术债务吗?
  • 9. 回文数
  • [SV]SystemVerilog线程之fork...join专题
  • 你看这个spring的aop它又大又宽
  • 设计模式-创建-单例模式
  • 使用mybatis-plus-generator配置一套适合你的CRUD
  • MATLAB实现各种离散概率密度函数(概率密度/分布/逆概率分布函数)
  • 指针的基本知识
  • 当你的IDE装上GPT
  • 一图看懂 pathlib 模块:面向对象的文件系统路径, 资料整理+笔记(大全)
  • 前端如何将node.js 和mongodb部署到linux服务器上
  • mysql数据迁移
  • 【4.3蓝桥备战】小朋友崇拜圈、正则问题
  • MySQL读写分离中间件
  • 【Spring源码设计模式】单例模式外带设计模式的单例模式
  • go并发编程 —— singleflight设计模式
  • 【LeetCode】二叉树的中序遍历(递归,迭代,Morris遍历)
  • 银行数字化转型导师坚鹏:数字化转型背景下的银行柜员提升之道
  • ChatGPT的平替来了?一文总结 ChatGPT 的开源平替,你值得拥有