一、父工程创建
1、新建一个工作空间,并用idea打开,点击左上角file 》new》project新建一个工程
选择maven,设置jdk,点击next
输入groupId,artifactId,点击next
输入project-name及location,点击finish,完成父工程的创建
删除里面的src文件夹,得到一个空的工程
二、子工程的创建
1、右击创建的父工程,选择new 》module
选择spring initializr,选择jdk,选择初始化服务的地址(如果默认的地址连接不上,点击这里)
填写group、artifact等信息后点击next
选择web 》 spring web,点击next
填写模块名称,content root路径要填写在父工程路径下面,点击finish完成子工程创建
删除一些不必要的文件
三、建立父子关系
1、修改父工程pom文件,将子工程的dependencies模块,打包模块,全局变量模块移动至父工程,并新增modules模块,如下:
<?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.4.0</version>
<relativePath/>
</parent>
<groupId>com.test.hm</groupId>
<artifactId>hm-test</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>hm-test-demo</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.test.hm.demo.HmTestDemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2、修改子工程模块pom文件,移除公共依赖,变量,打包等模块信息,新增parent模块,如下:
<?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.test.hm</groupId>
<artifactId>hm-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.test.hm.demo</groupId>
<artifactId>hm-test-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hm-test-demo</name>
<description>Demo project for Spring Boot</description>
</project>
至此,完整的maven+springboot父子项目创建完成。