使用idea完成多模块的搭建
文章目录
一、实现父工程和子工程的连接
1.创建一个空项目
2.创建子模块
重复以上操作创建需要的子模块
3.删除多余启动项
4.在MainDemo中声明子模块
5.定义Maven项目的父工程
在五个子模块的pom文件空白出添加, 使用父项目来定义一组项目共同的配置信息,这样子项目就可以继承配置信息 ,具体的内容根据自己项目而定
<parent>
<groupId>com.example</groupId>
<artifactId>MainDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
例:
6.将父工程与子工程关联
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.6</version>
</dependency>
<!-- 后台管理系统模块-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 公共模块-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 入口-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 用户模块-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- web模块-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
7.运行demo-server模块的启动项
出现springboot启动图标即完成
二、实现模块与模块之间的连接
1.子模块之间的联系
2.连接子模块
根据子模块的关系图实现连接,具体代码如下,以demo-admin为例子,与demo-common相关联
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 关联工具模块-->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
因为整个父工程的pom中依赖关系为声明,所以在子工程demo-service中需要额外添加以下代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.6</version>
</dependency>
三.可能遇到的问题
解决方案