SpringBoot构建Multi-Module(多模块)时遇到的那些坑

1.安装Spring Tool Suit(STS) , 网上有各种教程,不做赘述

2.新建Project->Spring Starter Project, 填好必填项,依赖库暂时都不需要选。

3.新建好项目,根目录只保留.mvn/ .settings/ .project pom.xml ,删掉src等文件夹与各种java和maven依赖

4.与根Project新建Maven Project : ModuleA (公有模块), ModuleB , ModuleC

4.根目录 pom.xml 加入以下配置 

    ⅰ指定打包方式

<packaging>pom</packaging>

    ⅱ加入依赖SpringBoot基础依赖 spring-boot-starter-parent

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

    ⅲ指定项目公有模块

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.test.service</groupId>
			<artifactId>ModuleA</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</dependencyManagement>

    ⅳ指定项目所有内部模块

<modules>
	<module>ModuleA</module>
	<module>ModuleB</module>
	<module>ModuleC</module>
</modules>

5.如ModuleA为Common模块,ModuleB,C为业务模块,ModuleB,C依赖ModuleA。所以ModuleA并不需要编译出jar文件,仅需要作为依赖包的形式编入ModuleB,C 编译产物jar包即可

    针对以上情况,ModuleA中无需添加任何打包相关配置,仅定义ModuleA信息相关配置,ModuleB,C中添加

<packaging>jar</packaging>
<dependencies>
	<dependency>
		<groupId>com.test.service</groupId>
		<artifactId>ModuleA</artifactId>
	</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<mainClass>com.test.service.xxxx.ModuleBStartApplication</mainClass>
				<layout>ZIP</layout>
			</configuration>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

即可

6.项目中有时会出现ModuleB需要编译产物jar文件(放到服务器运行or其他用处), 同时ModuleC又依赖ModuleB,ModuleC也需要编译产物jar文件

针对这种情况只需要在ModuleB的pom.xml配置下添加如下红字配置

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<mainClass>com.test.service.xxxx.ModuleBStartApplication</mainClass>
				<layout>ZIP</layout>
				<classifier>exec</classifier>
			</configuration>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

这样编译后在ModuleB的target文件夹内会编译生成ModuleB-0.0.1-SNAPSHOT-exec.jar ,该文件可以通过jar -jar形式正常运行了,同时ModuleC的产物也可以在包含ModuleB的情况下正常运行。


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值