背景
使用Idea 创建一个模块化的SpringBoot项目,但是发现Idea 创建父子项目的方式较Eclipse 较为不同,且Idea 创建的过程较Eclipse创建父子项目的过程复杂。
 Eclipse 创建SpringBoot父子项目传送门
网上虽然有Idea创建SpringBoot父子项目,但是携带各种其他的功能,导致无法简单的搞懂如何创建。下面就一个例子进行说明一下。
构建简单SpringBoot父子项目
环境说明:jdk1.8
 Idea 版本:2019.3
 (1)打开Idea,如果是新安装的软件,不会有历史项目
 
 (2)点击 “Create New Project”,进入一个新的项目的创建界面。父项目直接创建一个Maven项目即可
 
 PS:不要勾选 “Create from archetype”
 点击下一步。
 (3)进入父项目的创建页面,填写项目名称和GoupId与坐标
 
 PS:可以在上图红圈标注的地方修改项目的地址
 (4)点击Finish 按钮,完成一个父项目的创建。
 下图是点击Finish 创建好的项目在Idea 软件中打开的情况:
 
 (5)在现有父项目的基础上新建SpringBoot子项目
 在父项目的名称的位置右键鼠标:
 
 (6)新建一个Module,进入新建页面。
 子项目直接选择Spring initializr,创建一个SpringBoot项目
 
 点击下一步,进入子项目的具体构建内容。
 
 填写坐标后,点击下一步。
 (7)进入新建项目选择插件,由于我们测试需要,所以直接简单的选择了web
 
 点击下一步,可以更改项目名称和地址
 
 可以看出,子项目的地址默认在父项目文件夹下。点击Finish完成子项目的创建。
 (8)可以删除一些子项目不需要的文件,不删也行
 
 P: 还有父项目的src文件夹,这个已经不需要了,所以可以直接删除
(9)经过 1-8 步的操作,我们创建了父子项目结构,但是这两个项目尚未实现父子关系。下面我们需要更改POM文件,来实现父子关系。
 
 这里我们先粘贴出还没有改变关系之前的父子结构项目原有的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version>
</project>
- 子项目
<?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.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo-redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo-redis</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-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
(10)首先将你认为子项目中是公共的部分拿到父项目中,包括spring-boot-starter-parent 和一些 properties 以及build 等公共配置
<!--1 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent>
<!--2 -->
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties>
<!--3-->
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
上面子项目中的配置文件都可以拿到父项目的POM文件中 。
 (11)将父项目的坐标,复制到 子项目的父坐标中
	<parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version></parent>
(12) 父项目 新增 pom
 P:这个必须
 
 (13)将子项目的moudle 关联到 父项目中
  <modules><module>springboot-demo-redis</module></modules>

 (14)我们再来看下新的父子项目的 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 http://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.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>springboot-demo-redis</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></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
- 子项目
<?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.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo-redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo-redis</name><description>Demo project for Spring Boot</description></project>
读者可以对比一下,没有改造之前的POM文件,有什么差距
 (15) 我们来验证一下,父子项目是否成功创建
 我们在子项目中新增一个Controller ,进行一下简单Web访问,查看是否成功
 
package com.codingdemo.springbootdemoredis.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String hello() {return "hello world!";}
}
启动一下SpringBoot 启动类,输入网址
 
 可以看到能够成功访问,我们已经成功搭建好简单的父子SpringBoot项目
 (16) 如果我修改 Controller的方法,需要重启项目,这样比较麻烦。下面是如何配置热部署SpringBoot项目
SpringBoot项目的热部署
(1)在上面现有的父子SpringBoot项目中,在父项目POM文件增加热部署的devtools依赖。
<!-- 热加载 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!--热加载必须配上以下内容--><configuration><fork>true</fork></configuration></plugin></plugins></build>
(2) 我们可以在配置文件中设置devtools
### 热部署配置
spring.devtools.restart.enabled=true 
spring.devtools.restart.additional-paths=src/main/java
spring.devtools.restart.poll-interval=1s
spring.freemarker.cache=false
(3)如果修改了Controller的函数但是还是没有热加载,可能是由于你是新的项目需要设置开启自动编译
 
 P:勾上
 重启项目后,然后再次修改文件后,便可以看到开始热部署了
 