公司一站通项目 由于使用springboot 开发后端业务,涉及的模块比较多。 大模块(需要独立打包部署的)分为 Web, Websocket , Task 模块。。 而这三个模块,又有一些公共业务,包括都需要使用到 数据库mysql 操作, rabbitmq操作,等等一系列相同业务Service.. 如果使用一个springboot 开发,那么不利于业务解耦,, 重新修改某一块的业务的时候,需要启动所有的模块。从而影响到其他业务,这样不利于业务的开发。。
本次搭建使用 idea开发环境。案例将以打包web模块为例, (websocket模块和task模块原理相同) 模块分层如图:
创建项目流程:
(1).通过idea 创建一个空的 maven项目做为springboot顶级父项目,由于父项目不做任何代码编写,可以删除src目录。仅仅保留 pom.xml 文件和一些 idea的索引文件.
(2). 修改pom.xml, 案例再下面-
在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);
}
}
- 删除 公共业务模块 作为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文件类)
打开 us764boot-web-1.0.0-exec.jar 内的 BOOT-INFO/lib 文件夹内可看到 其他模块的依赖jar
项目构建源码的下载地址: https://gitee.com/huaiyuanwangjun/springboot/raw/master/us764-boot.zip