You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:35:25