springboot+maven多模块构建教程

1. 前言

2. 多模块项目结构

通常是有一个父级项目(root)下有多个子级项目(mudules),相当于一个springboot项目里面又嵌套了多个同级的子级springboot项目。一般的多模块项目结构包括五个常用的子级模块:

  • Web模块
  • Service模块
  • Dao模块
  • Common模块
  • Api模块

3. 构建实战

构建父级项目

  1. 创建新项目
  2. 选择好项目的相关配置
  • GroupId
    子级项目和父级项目都统一的名字
  • Aritifact
    是父级项目和子级项目子级独有的包名

构建过springboot项目的大家,肯定发现这个其实跟建立一个springboot项目是一样的流程,没错就是一样的,只不过建立一个springboot项目我们称之为单模块springboot项目。如果想了解更为细节的单模块构建springboot项目的,我之前写了一篇SpringBoot2.x学习笔记基础篇,有兴趣的可以看看~

3. 删除冗余文件

如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven

构建子级模块

1. 构建Web模块

步骤跟建立单个springboot项目是一样,但是是保姆教程,我还贴图给大家看看。

  • 新建模块
  • 配置模块信息和springboot版本
  • 添加web模块的必备依赖
    springweb依赖
  • 删除子级项目冗余文件
2. 构建Service模块
  • 新建模块
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_java_02


  • 配置模块信息和springboot版本号
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_03


  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_04


  • 删除冗余文件
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_05


3. 构建Dao模块
  • 新建模块
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_06


  • 配置模块信息和springboot版本号
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_07


  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_08


  • 删除冗余文件
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_redis_09


构建Api模块
  • 新建模块
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_redis_10


  • 配置模块信息和springboot版本号
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_11


如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_08

  • 删除冗余文件
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_redis_13


构建Common模块
  • 新建模块
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_14


  • 配置模块信息和springboot版本
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_15


  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_maven_08


  • 删除冗余文件
  • 如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_redis_17

  • 这里我们已经将五个子模块和一个父项目构建完毕,接下来是配置父子pom.xml文件了。
配置父子项目Pom.xml文件
  1. 父级pom.xml
// 添加模块信息
    <modules>
        <module>web</module>
        <module>service</module>
        <module>dao</module>
        <module>common</module>
        <module>api</module>
    </modules>
// 添加子模块项目依赖
<dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  • 除去原始依赖
<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>
  • 标注打包方式
<packaging>pom</packaging>
  • 删除build标签
<build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>
  • 完整的父级项目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">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>multimod</name>
   <description>multimod</description>
   <!--父模块的打包方式-->
   <packaging>pom</packaging>
   <!--parent指明继承关系,给出被继承的父项目具体信息-->
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.7.13</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
       <java.version>1.8</java.version>
   </properties>
   <modules>
       <module>web</module>
       <module>service</module>
       <module>dao</module>
       <module>common</module>
       <module>api</module>
   </modules>
   <dependencyManagement>
       <dependencies>

           <dependency>
               <groupId>com.xyy</groupId>
               <artifactId>web</artifactId>
               <version>0.0.1-SNAPSHOT</version>
           </dependency>
           <dependency>
               <groupId>com.xyy</groupId>
               <artifactId>service</artifactId>
               <version>0.0.1-SNAPSHOT</version>
           </dependency>
           <dependency>
               <groupId>com.xyy</groupId>
               <artifactId>dao</artifactId>
               <version>0.0.1-SNAPSHOT</version>
           </dependency>
           <dependency>
               <groupId>com.xyy</groupId>
               <artifactId>common</artifactId>
               <version>0.0.1-SNAPSHOT</version>
           </dependency>
           <dependency>
               <groupId>com.xyy</groupId>
               <artifactId>api</artifactId>
               <version>0.0.1-SNAPSHOT</version>
           </dependency>
       </dependencies>
   </dependencyManagement>
</project>
  1. 子模块的完整Pom.xml文件内容
    为了不重复冗余,这里只详细展示web模块的Pom.xml文件修改流程,其余模块均有完整代码贴出。
  • 标注继承关系
<parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
  • 添加依赖
<dependency>
            <groupId>com.example</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  • 删除springboot的Stater和Test (因为是已经继承了父级项目的starter-parent)
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 删除bulid标签
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 完整web模块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">
    <modelVersion>4.0.0</modelVersion>
	<!--当前子模块信息-->
    <groupId>com.example</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <description>web</description>
	<!--表明继承对象-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--添加子模块依赖  -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

依次同理,下面是service、dao、common、api的Pom.xm文件

<!--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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service</name>
    <description>service</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--添加子模块依赖  -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
<!--dao的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">
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.example</groupId>
    <artifactId>dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dao</name>
    <description>dao</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
<!--common的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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>common</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>
</project>
<!--api的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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api</name>
    <description>api</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        
    </dependencies>
</project>
删除各个模块的Application和ApplicationTest文件(留下Web的入口文件)

因为每创建一个springboot工程项目时,都会以个入口文件和springboot的测试文件,将其都删掉,保留一个Web模块的入口文件即可。删完后,同时把入口文件放入到com.example 的目录下(要不然springboot找不到入口文件在哪了)。以下是工程目录结构如图:

如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_18


如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring_19

点击root项目的maven的clean和package

如图,即为构建成功!BUILD SUCCESS!

如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring_20


如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_21

4. 总结

在构建多模块springboot项目时,我们可以将父子项目的关系归结于如下图:

如何将一个springboot 项目作为一个模块快速合并到另外一个springboot项目里 springboot 多模块项目_spring boot_22