Spring boot 多模块项目搭建、打包、拆分

前言

整篇文章包含了、基础搭建、项目打包、项目拆分运行、项目联合运行,着重查看

基础搭建

1.创建一个spring boot项目(保证创建项目能够运行)

步骤一

步骤二(只做demo演示)

步骤三(选中web,看个人项目架构)

2.新建Spring boot module


步骤一

3.新建module重复上述步骤(步骤三不勾选),建三个module演示(称为子级项目)


目录展示

4.删除多余文件


删除Src目录(父级项目下,其他如.mvn文件等看个人需不需要版本控制)

5.重点。配置pom文件(我们接下来以module1作为入口项目,module2/3作为依赖项目,如何依赖看项目架构是什么样的,接下来叙述)


展示pom

6.先修改父级项目pom

  • 修改前pom
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 修改后pom
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
        </dependency>
    </dependencies>


    <!--删除了build,添加了packaging并且改为pom-->
    <packaging>pom</packaging>

   <!--添加module-->
    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    </modules>

</project>

  • 主要
    <!--删除了build,添加了packaging并且改为pom-->
    <packaging>pom</packaging>

   <!--添加module-->
    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    </modules>

7.修改module1(注意、此处比较特殊,因为是作为入口)

  • 修改前
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>module1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 修改后
<?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>

    <!--修改为父级-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>


    <groupId>com.example</groupId>
    <artifactId>module1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--删除了多余依赖、由父级pom管理-->


        <!--添加了module1依赖于module2依赖(此处注意,如果是个人框架mvc模式区分,module2可以看做service层,)-->
        <!--module3可以看做dao层,那么此处仅有依赖module2,而module2依赖于module3。我们此处案列是需要拆分的所以看成是多人聚合项目)-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module3</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


  • 主要
    <!--修改为父级-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

      <dependencies>

        <!--删除了多余依赖、由父级pom管理-->


        <!--添加了module1依赖于module2依赖(此处注意,如果是个人框架mvc模式区分,module2可以看做service层,)-->
        <!--module3可以看做dao层,那么此处仅有依赖module2,而module2依赖于module3。我们此处案列是需要拆分的所以看成是多人聚合项目)-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module3</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

8.修改module2(多人子项目2)

  • 修改前pom
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>module2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • 修改后pom
<?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>

    <!--修改为父级-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>module2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--删除了多余依赖、由父级pom管理-->

    </dependencies>

       <!--删除build、或者改成不可执行jar包、二选一-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--此处修改-->
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

  • 主要
 <!--修改为父级-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

  <dependencies>

        <!--删除了多余依赖、由父级pom管理-->

    </dependencies>

      <!--删除build、或者改成不可执行jar包、二选一-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--此处修改-->
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

9.修改module3,步骤与module2相同。

10.运行项目,删除其他入口(个人洁癖)


步骤一

步骤二(代表运行成功)

11.增加一个基础接口,在入口子项目添加一个接口


步骤一
@RestController
public class Main1 {

    @RequestMapping(value = "/")
    public String _Start() {
        return "项目已成功运行";
    }
}

12.添加端口(端口是在入口项目添加)


步骤一

13.启动(端口被占用了改到了8001就不重新截图了)


步骤一

14.这一步、基础搭建就已经完成了

项目打包

1.为了保证项目能够正常打包,请按照基础搭建一步一步来。(网上很多文章搭建不一样,最终很少有能一次成功就打包的,所以避免踩坑)

2.依次完成搭建后,项目右侧maven


步骤一(运行package)

3.一堆测试后、项目成功打包


步骤一

4.因为module1是入口,所以jar包在module1子目录下


步骤一

5.cmd命令运行

打包成功运行

6.打包测试这里就结束了

项目拆分

1.既然是多人聚合项目(这里先不谈mvc这些关联模式),解耦性是非常高的,每一个子项目就是一个系统,比如登录系统,数据库管理系统等等,基本就是跟微服务大体一致。

2.既然没有很高的耦合性,那么这些module就是独立的jar包,以module2作为实列

3.在module2中新建一个接口

步骤一

4.运行项目,发现访问http://localhost:8001/module并不能获取结果。

5.返回module1(入口项目)的启动模块中,替换@SpringBootApplication,将依赖模块也加入托管容器

@SpringBootApplication(scanBasePackages = "com.example")

6.再次运行项目,可以进行正常访问


image.png

7.既然要进行项目拆分,那么我们必然会对module2进行打包,进入module2的pom文件。

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--此处修改-->
<!--                <configuration>-->
<!--                    <classifier>exec</classifier>-->     
<!--                </configuration>-->
            </plugin>
        </plugins>
    </build>

8.将build恢复原状,这时候我们在使用maven进行打包


步骤一
步骤二

9.这里不再演示,(上述步骤记得在module2中配置新端口),该module2如果是但是系统模块,是非常方便单独拆分的,已经直接作为上线项目也可以直接引用到下一个项目(比如工具模块)

10.项目拆分也到此结束

单人项目,设计模式MVC

1.既然是单人项目(当然这里也可以不是),将项目拆分成server、dao、controller是不可避免的。

2.基础搭建上注释了依赖关系,这里重新讲解一下controller连接server,则server是依赖对象。dao被server所依赖。

3.修改基础搭建中的依赖关系(不做演示)

4.每个模块中写好逻辑(dao层处理数据库,server处理数据库逻辑,已经下发的数据等,controller处理下发)

5.跟一个项目一样@Autowired注入,注意: 每个启动入口都要添加依赖的名称,不然@Autowired注入是找不到的

//演示
@SpringBootApplication(scanBasePackages = "com.example")

6.正常逻辑写项目(这里已经不能被拆分了,但是可以部分逻辑可以抽离用于下一个项目)

7.打包、上线、部署

结尾

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354