Springboot 2.x 构建多模块项目

公司一站通项目 由于使用springboot 开发后端业务,涉及的模块比较多。 大模块(需要独立打包部署的)分为 Web, Websocket , Task 模块。。 而这三个模块,又有一些公共业务,包括都需要使用到 数据库mysql 操作, rabbitmq操作,等等一系列相同业务Service.. 如果使用一个springboot 开发,那么不利于业务解耦,, 重新修改某一块的业务的时候,需要启动所有的模块。从而影响到其他业务,这样不利于业务的开发。。

本次搭建使用 idea开发环境。案例将以打包web模块为例, (websocket模块和task模块原理相同) 模块分层如图:


image.png

创建项目流程:

  1. (1).通过idea 创建一个空的 maven项目做为springboot顶级父项目,由于父项目不做任何代码编写,可以删除src目录。仅仅保留 pom.xml 文件和一些 idea的索引文件.
    (2). 修改pom.xml, 案例再下面

  2. 在idea中创建子模块流程


    image.png

    image.png

    依次类推 创建 所有的子模块 最终idea如图:


    image.png

3.1 修改顶级父 的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>

    <groupId>com.us764</groupId>
    <artifactId>us764-boot</artifactId>
    <version>1.0.0</version> <!-- 子项目的的<parent>标签中的<version>标签必须与该配置一致 -->
    <packaging>pom</packaging><!-- 父项目这里必须是 pom -->

    <name>us764-boot</name>
    <description>一站通主boot项目</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 所有的子模块 -->
    <modules>
        <module>us764boot-core</module> <!-- 核心模块 -->
        <module>us764boot-data</module> <!-- 持久层模块 -->
        <module>us764boot-common</module> <!-- 通用工具模块 -->
        <module>us764boot-opex</module> <!-- 询盘模块 -->
        <module>us764boot-task</module> <!-- 同步管理模块 -->
        <module>us764boot-web</module> <!-- web管理模块 -->
        <module>us764boot-websocket</module> <!-- socket.io 广播消息模块 -->
        <module>us764boot-mq</module><!-- rabbitmq 模块 -->
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <skipTests>true</skipTests>
        <us764boot.version>1.0.0</us764boot.version>

        <us764boot-core.version>1.0.0</us764boot-core.version>
        <us764boot-opex.version>1.0.0</us764boot-opex.version>
        <us764boot-common.version>1.0.0</us764boot-common.version>
        <us764boot-data.version>1.0.0</us764boot-data.version>
        <us764boot-mq.version>1.0.0</us764boot-mq.version>
        <us764boot-task.version>1.0.0</us764boot-task.version>
        <us764boot-websocket.version>1.0.0</us764boot-websocket.version>
        <us764boot-web.version>1.0.0</us764boot-web.version>

        <ganymed-ssh2.version>1.2.0</ganymed-ssh2.version>
        <mybatis-plus.version>3.3.2</mybatis-plus.version>
        <druid.version>1.1.23</druid.version>
        <netty-socketio.version>1.7.18</netty-socketio.version>
        <springdoc-openapi-ui.version>1.5.0</springdoc-openapi-ui.version>
        <ip2region.version>1.7.2</ip2region.version>
        <fastjson.version>1.2.73</fastjson.version>
        <commons-lang3.version>3.11</commons-lang3.version>
        <commons-collections4.version>4.4</commons-collections4.version>
        <java-jwt.version>3.10.3</java-jwt.version>
        <jmustache.version>1.15</jmustache.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.2 修改不需要启动的模块(不需要单独打包的模块) 的pom文件,本文将以 us764boot-common 的 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.us764</groupId>
        <artifactId>us764-boot</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>us764boot-common</artifactId>
    <name>us764boot-common</name>
    <version>${us764boot-common.version}</version>
    <description>公共核心模块</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>${ip2region.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-core</artifactId>
            <version>${us764boot-core.version}</version>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${java-jwt.version}</version>
        </dependency>

        <dependency>
            <groupId>com.samskivert</groupId>
            <artifactId>jmustache</artifactId>
            <version>${jmustache.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>${commons-collections4.version}</version>
        </dependency>


    </dependencies>

    <!--<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->

</project>

3.3 修改 需要打包的模块 pom 文件,本案例以 web模块 (websocket模块和task模块一样) 为例

<?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.us764</groupId>
        <artifactId>us764-boot</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>us764boot-web</artifactId>
    <version>1.0.0</version>
    <name>us764boot-web</name>
    <description>项目对外api模块</description>
    <packaging>jar</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>${springdoc-openapi-ui.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-data</artifactId>
            <version>${us764boot-data.version}</version>
        </dependency>

        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-common</artifactId>
            <version>${us764boot-common.version}</version>
        </dependency>

        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-core</artifactId>
            <version>${us764boot-core.version}</version>
        </dependency>

        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-opex</artifactId>
            <version>${us764boot-opex.version}</version>
        </dependency>

        <dependency>
            <groupId>com.us764</groupId>
            <artifactId>us764boot-mq</artifactId>
            <version>${us764boot-mq.version}</version>
        </dependency>

    </dependencies>

    <!-- 需要打包的 web 模块必须加上 build打包插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 打包成可执行jar的时候,可执行jar的名字会以这个做为后缀   我的案例会生成一个us764boot-web-1.0.0-exec.jar  这个jar就是放到生产环境使用的jar-->
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>


        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 打包排除配置文件
                <excludes>
                    <exclude>*.properties</exclude>
                    <exclude>*.yml</exclude>
                </excludes>
                -->
            </resource>
        </resources>

    </build>

</project>

3.4 由于web模块需要依赖 其他5个小模块,故需要配置web模块的包扫描, 修改web模块的入口启动类文件。配置包扫描

package com.us764.us764bootweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.Banner;

@SpringBootApplication(scanBasePackages = {
    "com.us764.us764bootcommon",
    "com.us764.us764bootcore",
    "com.us764.us764bootdata",
    "com.us764.us764bootmq",
    "com.us764.us764bootopex"
}) // 这里需要配置包引入扫描,需要引入哪些模块,就需要填写那些模块的包路径。 也可以缩写成 scanBasePackages="com.us764" 包含所有的子模块
// 但是由于 本例的websocket和task模块, 不在web模块内。故一个个加载
public class Us764bootWebApplication {

    public static void main(String[] args) {
      
        SpringApplication app = new SpringApplication(Us764bootWebApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        //app.setWebApplicationType(WebApplicationType.NONE);
        app.run(args);
    }

}

  1. 删除 公共业务模块 作为web模块依赖的,无需打包模块的启动类文件。 本项目,则删除 us764boot-common, us764boot-core,us764boot-mq,us764boot-opex 等模块的启动类文件。。。由于 us764boot-data模块虽然也不需要单独启动,但是由于它引入了spring-boot-starter-data-mongodb 操作mongodb 故而不能删除启动类。 我也没找到原因

5.在 父亲级项目下执行 mvn clean install 即可打包所有的子项目依赖jar , 然后在 web模块的根目录 执行 mvn package , 则可完成web模块的打包。。。打包后会将 所有需要依赖模块 (common,data,core等子jar 集成到web模块的 jar文件类)


image.png

打开 us764boot-web-1.0.0-exec.jar 内的 BOOT-INFO/lib 文件夹内可看到 其他模块的依赖jar


image.png

项目构建源码的下载地址: https://gitee.com/huaiyuanwangjun/springboot/raw/master/us764-boot.zip

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