Spring Boot 多模块工程
1、新建一个maven工程
工程名 :spring-boot-sample-project
删除多余的文件,只留spring-boot-sample-project工程中的pom.xml文件
2、在spring-boot-sample-project工程中增加子模块
新增:spring-boot-sample-api 模块,注意图中2位置的包名com.example.sample.api,因为后面会涉及到多模块之间的相互调用,为了方便扫描,将包名统一成com.example.sample.模块的名
同理,新增:spring-boot-sample-web模块,选择Spring Boot web开发所需要的依赖的,这里我勾选了Spring Web 和 Lombok
最后整个工程目录如下:
3、配置pom.xml 文件
3.1、父类工程spring-boot-sample-project的pom.xml
1、首先是父类工程的基本信息
<!-- 基本信息 -->
<description>Spring Boot 多模块构建</description>
<modelVersion>4.0.0</modelVersion>
<name>spring-boot-sample</name>
<packaging>pom</packaging>
<!-- 项目说明:这里作为聚合工程的父工程 -->
<groupId>com.example</groupId>
<artifactId>spring-boot-sample-project</artifactId>
<version>1.0-SNAPSHOT</version>
2、因为整个工程都会依赖于Spring Boot,因此spring-boot-sample-project需要继承Spring Boot父类
<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
3、声明子模块,创建好的子模块放到modules
<!-- 模块说明:这里声明多个子模块 -->
<modules>
<module>spring-boot-sample-api</module>
<module>spring-boot-sample-web</module>
</modules>
4 、版本管理,可以将子模块等需要版本管理的依赖放到dependencyManagement进行版本管理
<!-- 版本说明:这里统一管理依赖的版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-sample-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-sample-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
5、其他
3.1.1、完整的父工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 基本信息 -->
<description>Spring Boot 多模块构建</description>
<modelVersion>4.0.0</modelVersion>
<name>spring-boot-sample-project</name>
<packaging>pom</packaging>
<!-- 项目说明:这里作为聚合工程的父工程 -->
<groupId>com.example</groupId>
<artifactId>spring-boot-sample-project</artifactId>
<version>1.0-SNAPSHOT