求助:如何在VSCode中搭建多模块Spring Boot Maven项目?
在VSCode中创建多模块Spring Boot Maven项目
1. 初始化父级项目
- 新建一个空文件夹(比如
spring-multi-demo),用VSCode打开该文件夹 - 在根目录手动创建
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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spring-multi-demo</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>Spring Multi Module Parent</name> <!-- 引入Spring Boot父依赖统一管理版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <!-- 替换为你需要的版本 --> <relativePath/> </parent> <!-- 子模块声明区域(添加子模块后会自动填充) --> <modules> </modules> <!-- 统一管理依赖版本,避免子模块重复声明 --> <dependencyManagement> <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> </dependency> </dependencies> </dependencyManagement> </project>
2. 创建Web子模块(作为启动模块)
- 在VSCode终端切换到父项目根目录,执行命令生成子模块:
mvn archetype:generate -DgroupId=com.example -DartifactId=web-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 进入
web-module文件夹,修改pom.xml:- 添加父项目引用:
<parent> <groupId>com.example</groupId> <artifactId>spring-multi-demo</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>- 添加Spring Boot Web依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 若依赖其他子模块,可在此添加,比如后续的common-module --> <!-- <dependency> <groupId>com.example</groupId> <artifactId>common-module</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> --> </dependencies>- 添加Spring Boot打包插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.App</mainClass> <!-- 后续替换为你的启动类路径 --> </configuration> </plugin> </plugins> </build> - 在
web-module/src/main/java/com/example下创建Spring Boot启动类App.java:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
3. 创建Common子模块(通用工具/实体模块)
- 同样在父项目根目录执行命令生成子模块:
mvn archetype:generate -DgroupId=com.example -DartifactId=common-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 修改
common-module的pom.xml,添加父项目引用:
<parent> <groupId>com.example</groupId> <artifactId>spring-multi-demo</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
- 根据需求添加通用依赖(比如Lombok)即可。
4. VSCode内的操作与配置
- 安装Maven for Java和Spring Boot Extension Pack插件,方便依赖管理与项目运行
- 刷新Maven项目:按
Ctrl+Shift+P输入Maven: Reload Projects,确保所有模块被识别 - 运行项目:在左侧Maven面板找到
web-module->Plugins->spring-boot->spring-boot:run,点击运行 - 打包项目:在父项目根目录执行
mvn clean package,自动打包所有子模块
注意事项
- 父项目的
packaging必须设为pom,否则无法作为多模块父项目 - 子模块的
parent节点必须正确指向父项目的groupId、artifactId和version - 子模块间有依赖时,需确保依赖模块已在父项目
modules中声明,且版本与父项目一致 - 若出现依赖未识别情况,尝试执行
Maven: Reload Projects或重启VSCode
内容的提问来源于stack exchange,提问作者Habt
相关产品推荐
相关产品推荐

