父工程创建
1.新建一个spring项目
2.选择合适的springboot版本,点击【完成】,即创建父工程完毕
3.删除父工程中无用文件:src
创建子工程模块
1.右键项目名->新建(news)->模块(Module)
2.选择新建Maven工程(注意此处的SDK需与父工程保持一致)
3.填写子工程模块信息(此处组id与父工程保持一致)
当前项目结构
父工程
pom.xml
<?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.cyfy</groupId><artifactId>fast-demo</artifactId><packaging>pom</packaging><version>0.0.1-SNAPSHOT</version><modules><module>fast-demo-web</module></modules><name>fast-demo</name><description>fast-demo</description><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.6.13</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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>${spring-boot.version}</version><configuration><!-- 启动类位置,后续修改 --><mainClass>com.cyfy.fastdemo.FastDemoApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
子工程
pom.xml
<?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"><parent><artifactId>fast-demo</artifactId><groupId>com.cyfy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fast-demo-web</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
测试
1.父工程添加spring web
依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.子工程编写启动类及controller类
FastDemoWebApplication
@SpringBootApplication
public class FastDemoWebApplication {public static void main(String[] args) {SpringApplication.run(FastDemoWebApplication.class,args);}
}
HelloController
@Controller
public class HelloController {@RequestMapping("/hello")@ResponseBodypublic String hello(){return "hello world!";}
}
3.运行项目,访问localhost:8080/hello
,能正常输入内容,则表示父子工程无问题
可以在idea右侧页签栏中点击【Maven】查看父子工程结构及其拥有依赖
如果显示如下图
可点击右上角设置图标,点击选择【将模块分组】即可