springBoot多模块

如何构建springBoot多模块项目

前言

首先,这里是骂人的,对,就是骂人。网站里的“大神”,麻烦能自己测试一遍吗?能自己写东西吗?能瞅瞅官网的说明吗?本来想看看您们的经验贴,发现都是屁话一堆,要说明的东西根本说不清楚,简直在害人。幸好我自力更生,找到了解决的办法,特仔细地写下这篇博客让大家别再浪费时间看一堆没用的帖子耽误进度。此外,评论区是留给需要的人,如果有想反驳我的人,我可以提供邮箱地址,欢迎你来反驳。
接收反驳回复的邮箱:zerotower@163.com
最后在邮件标题打上“反驳”两字,否则我可能因为邮件过多过滤掉

1.项目结构

我最近做了一个购物平台,主要想把业务拆分为以下几块:页面服务、商品服务、订单服务等。按照之前的学习,你要么把所有的服务放入一个进程(springBoot)中,要么每一个都用一个springBoot。如果是后者,当然希望多个springBoot每一个都是一个进程,只要主页面不挂,整个网站看上去还是"正常"的。但是最重要的是,我们难道每个springBoot项目都是独立的,然后只要放在同一个文件夹里?

不不不,这样是N个项目而不是一个了,我们当然希望通过构建一个springBoot,之后通过添加模块的方式扩展我们的项目。

下图是我希望的项目结构:
在这里插入图片描述

parent是统一管理的父模块,carts和product都是子模块。我们希望carts和product都有独立的启动类和配置,打包时它们也是分开的jar包。关于所有的子模块打包,网络的教程大多的意思还是我虽然是子模块,但是我是web资源、实体、映射、服务接口、控制每个都是一个模块。但明明有不同的package可以划分,为什么要靠模块设计完成?在不考虑实际多jar带来的额外内存开销的情况下,模块应该是不同的springBoot服务(仅仅代表目前我的个人理解,如果不对,欢迎评论区留言)。

我这里的打包是直接在父工程的maven管理下,可以直接对两个模块进行打包。但要同时启动这两个jar包或者多个jar包,windows环境下还是写一个批处理程序来启动。

2.构建过程和pom.xml

首先,父工程是一个springBoot。

构建好之后,把无用的src删除,应为父工程只涉及管理不涉及代码。

在这里插入图片描述

父项目的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>
    <packaging>pom</packaging>  <!--这里说明父项目的打包成pom-->
    <!--这里是整个项目包含的模块-->
    <modules>
        <module>carts</module>
        <module>product</module>
    </modules>
    <!--继承spring boot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!--项目的gav-->
    <groupId>zero.learning.mul</groupId>
    <artifactId>parent</artifactId>
    <!--定义我的开发版本-->
    <version>1.0.0</version>
    <name>parent</name>
    <!--描述信息  -->
    <description>测试的父功能模块。</description>
    <!--一些参数配置,一旦设置了将被所有的子模块所继承-->
    <properties>
        <java.version>1.8</java.version>
        <!-- 版本控制,所有的jar包版本都将在这里控制-->
        <mysql-version>8.0.21</mysql-version>
        <lombok.version>1.18.12</lombok.version>
        <mybatis-plus.version>3.3.2</mybatis-plus.version>
    </properties>
    <!--  原有的web,test依赖全部要删除 -->
    <!-- 引入所有项目需要的jar包-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
    </dependencies>
    <!--父项目中没有插件-->
</project>

接着,我们来建立两个子模块,由于我已经建立好,这里只给出大概的说明。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>zero.learning.mul</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test</artifactId>
    <!--后续的新配置从这里开始-->
</project>
<!------------------加入新的配置-------------------------->
 <name>test</name>
    <!--加入模块的描述  -->
    <description>我是模块test</description>
    <!--打包打为jar-->
    <packaging>jar</packaging>

    <!--引入需要的依赖  -->
    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <!--加入打包的配置  -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

看到这里,我们为了进一步添加代码,在java和resources中加入我们的代码和资源。

在这里插入图片描述
在这里插入图片描述

配置文件我就不说了每个模块不同的配置就好了。

我的项目里的具体结构:

在这里插入图片描述
在这里插入图片描述

3.关于打包

两个模块如何打包?如同上面所说,你只需要在子模块加入插件。

注意:父工程不需要插件

<build>
  <plugins>
     <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
  </plugins>
</build>
<!--根据实际的需要可能有所改动-->

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l38T5JhQ-1602240441565)(images/image-20201009171951502.png)]

选择父工程的maven,同时选择这四个选项。
在这里插入图片描述
在这里插入图片描述
关闭测试,把测试单元里的排除掉。

点击后,控制台的输出为:

F:\JAVA\jdk1.8\bin\java.exe -Dmaven.multiModuleProjectDirectory=C:\Users\HLxwghwr\Desktop\parent "-Dmaven.home=F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\plugins\maven\lib\maven3" "-Dclassworlds.conf=F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\plugins\maven\lib\maven-event-listener.jar" "-javaagent:F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=60052:F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath "F:\JAVA\IDE\IDEA 2019.3.5\IntelliJ IDEA 2019.3.5\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.5 -DskipTests=true clean validate compile package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] parent                                                             [pom]
[INFO] carts                                                              [jar]
[INFO] product                                                            [jar]
[INFO] 
[INFO] ----------------------< zero.learning.mul:parent >----------------------
[INFO] Building parent 1.0.0                                              [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ parent ---
[INFO] 
[INFO] ----------------------< zero.learning.mul:carts >-----------------------
[INFO] Building carts 1.0.0                                               [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ carts ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ carts ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ carts ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\HLxwghwr\Desktop\parent\carts\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ carts ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ carts ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\HLxwghwr\Desktop\parent\carts\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ carts ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\HLxwghwr\Desktop\parent\carts\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ carts ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ carts ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ carts ---
[INFO] Building jar: C:\Users\HLxwghwr\Desktop\parent\carts\target\carts-1.0.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ carts ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] ---------------------< zero.learning.mul:product >----------------------
[INFO] Building product 1.0.0                                             [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ product ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ product ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ product ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\HLxwghwr\Desktop\parent\product\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ product ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ product ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\HLxwghwr\Desktop\parent\product\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ product ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\HLxwghwr\Desktop\parent\product\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ product ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ product ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ product ---
[INFO] Building jar: C:\Users\HLxwghwr\Desktop\parent\product\target\product-1.0.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ product ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for parent 1.0.0:
[INFO] 
[INFO] parent ............................................. SUCCESS [  0.680 s]
[INFO] carts .............................................. SUCCESS [ 10.406 s]
[INFO] product ............................................ SUCCESS [  4.641 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.532 s
[INFO] Finished at: 2020-10-09T18:20:21+08:00
[INFO] ------------------------------------------------------------------------

在这里插入图片描述
在这里插入图片描述

如果仔细查看控制台,你还可以看到详细的编译信息,复制编译了哪些源文件,加入到classpath的文件的具体信息等等。

4.安装到本地的仓库和批处理运行

在idea的settings里可以看到你的本地仓库路径,也可以设置为别的,以后再说。
在这里插入图片描述
在这里插入图片描述

刚才打包就应该一起选择,我这里为了做说明就拆开来做了。安装到本地仓库后就到仓库里查看你的jar包。
在这里插入图片描述
在这里插入图片描述
安装的好处是以后随时可用。其实你也可以在target目录下查看。

在这里插入图片描述

关于批处理,由于批处理只是使用过一些,没有系统去学,为了不误人子弟,我这几天先吃透了再回来补充。

更多内容,等待后续补充完善,希望csdn“大神”们多多测试,写清楚自己的流程和原理,别误人子弟了!!!

Good Bye~

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值