Spring Boot 之 多Module项目
创建父项目
创建maven类型项目
- 创建成功后保留pom.xml、README.md文件,其余全部删除
- 修改pom文件,将packaging值改为pom类型
- 定义公共properties以及子模块可能用到的依赖
<?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>
<!-- 继承springboot 父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 本项目信息 -->
<groupId>com.icodesoft.modules</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo-parent</name>
<description>This is a demo parent project</description>
<!-- 子modules 使用IDEA创建子模块时会自动添加-->
<modules>
<module>demo-api</module>
<module>other-service</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.version>2.3.1</mybatis.version>
</properties>
<!-- 统一管理依赖的版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
创建子Module
-
右键父工程,选择 New -> Module 创建子模块
-
子模块也可以不继承父项目,可以直接是一个单独的springboot项目类型,只是不需要创建启动类。
<?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.3.11.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.icodesoft.modules</groupId> <artifactId>other-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>other-service</name> <description>other service module</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> </dependencies> </project>
-
创建API主项目时要引入
spring-boot-starter-web
依赖,其他子模块不需要 -
有关maven的编译插件只在主项目中引入,其他子模块不需要,主项目pom配置如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 对父项目 的依赖 -->
<parent>
<groupId>com.icodesoft.modules</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>demo-api</artifactId>
<name>demo-api</name>
<description>Main project</description>
<!-- 打包类型 -->
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.icodesoft.modules</groupId>
<artifactId>other-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- build maven插件依赖 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
主项目 引入子模块
<dependency>
<groupId>com.icodesoft.modules</groupId>
<artifactId>other-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
如何使用子模块中的类
// 直接注入子模块中的类即可
@Autowired
private HelloService helloService;
添加Dockerfile
From openjdk:8-jre-alpine
WORKDIR /app
COPY ./demo-api/target/*.jar ./app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
因为是多项目引用所以我们使用了git submodule
Git submodule
即 Git 子模块,子模块允许将一个 Git 仓库作为另一个 Git 仓库的子目录。同一个项目目录,但是独立的 git 记录和提交。
添加子项目
git submodule add -b <子模块分支> <子模块git地址> <存放的文件路径以及文件名>
-
进入父项目 并 clone子项目
cd demo-parent git submodule add -b master git@gitlab.com:icodesoft/other-service.git other-service ## 执行完上面命令后会新增.gitmodules和other-service两个文件
-
提交.gitmodules与other-service两个文件
[submodule "other-service"] path = other-service url = git@gitlab.com:icodesoft/other-service.git branch = master
Getting the code
git clone --recurse-submodules git@gitlab.com:icodesoft/demo-parent.git
- After you have an existing clone
git clone git@gitlab.com:icodesoft/demo-parent.git
git submodule update --init --recursive
当子项目更新后一定要让主项目开发人员执行以下命令更新
git submodule update