敏捷开发以用户的需求进化为核心,采用迭代、循序渐进的方法进行软件开发。在敏捷开发中,软件项目在构建初期被切分成多个子项目,各个子项目的成果都经过测试,具备可视、可集成和可运行使用的特征。换言之,就是把一个大项目分为多个相互联系,但也可独立运行的小项目,并分别完成,在此过程中软件一直处于可使用状态。
Maven 多模块项目通常由一个父模块和若干个子模块构成,每个模块都对应着一个 pom.xml。它们之间通过继承和聚合(也称作多模块)相互关联。多模块适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。下文将通过构建一个包含 两 个子模块的项目,来演示 SpringBoot 在 Maven 环境的多模块构建过程。
1、创建父工程
(1)新建一个SpringBoot项目
File - New - Project - Spring Initializr
(2)Next,填写项目信息,然后一路Next完成创建
(3)由于父工程只用来做依赖管理,不需要编写代码,所以如截图所示,删除红框外的其余文件:
删除后如下图所示:
2、创建子工程
(1)右键点击父工程,New - Module,新建两个子工程es(后面用来写SpringBoot集成ES的相关代码)、pms(整个项目的入口,项目的后台管理用户管理都在这个模块),删除子工程的 .mvn目录、mvnw、mvnw.cmd 文件
(2)子工程的src里面的内容,只保留pms的启动类和配置文件,其他子工程的启动类和配置文件都删掉
3、编辑pom.xml 文件(注意看注释部分,都是踩过的坑)
(1)父工程quick-platform-test的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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.test</groupId>
<artifactId>quick-platform-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>quick-platform</name>
<description>Demo project for Spring Boot</description>
<!-- 配置打包类型,不写默认是jar,一般来说所有的父级项目的packaging都为pom -->
<packaging>pom</packaging>
<!-- 添加管理的包 -->
<modules>
<module>es</module>
<module>pms</module>
</modules>
<!-- 版本说明:这里统一管理依赖的版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>es</artifactId>
<!-- 版本号(如果此处不统一声明版本号,需要在各模块添加依赖时除了添加groupId、artifactId,
还要声明版本号,否则会报错:该依赖unknown) -->
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>pms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
(2)子工程es的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.test</groupId>
<artifactId>es</artifactId>
<parent>
<groupId>com.test</groupId>
<artifactId>quick-platform-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<version>0.0.1-SNAPSHOT</version>
<name>es</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
</project>
(3)子工程pms的pom(pms作为入口,后续子工程需要在pms的pom文件中配置,否则会导致其他子工程的controller访问失败)
<?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.test</groupId>
<artifactId>pms</artifactId>
<parent>
<groupId>com.test</groupId>
<artifactId>quick-platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<version>0.0.1-SNAPSHOT</version>
<name>pms</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Spring Security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--添加es模块依赖 此处应该添加es模块版本号<version>0.0.1-SNAPSHOT</version>,由于在父项目中已声明,此处可省略-->
<dependency>
<groupId>com.test</groupId>
<artifactId>es</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 当所依赖的其他模块,有启动类的时候,需要以下配置,指定唯一启动类-->
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.test.PmsApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4、移动pms模块的启动类位置
pms中的启动类PmsApplication在com.test.pms下面,需要移动到com.test下面, 如果不移动启动类的话,在多模块项目中会碰到一个模块无法通过 @Autowired 注入其他模块里的对象的问题。
右键点击PmsApplication,选择Refactor - Move,点击Refactor移动
5、编码调试,可以在pms中编写代码调试,此处我已经将之前一个做的后台管理项目代码放到pms中,下一章介绍SpringBoot集成ES就是在此后台管理系统基础上编码。