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的情况下正常运行。