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

如何使用IntelliJ和Maven创建指定结构的Spring Boot多模块项目

How to Set Up a Multi-Module Spring Boot Project with Separate API and Core Services

Alright, let's walk through building this multi-module Spring Boot project step by step. I’ve set up similar architectures multiple times, so I’ll make sure to cover all the key details to get your independent JARs up and running in separate containers.

1. Create the Parent Project

First, we’ll make a parent Maven project (I’ll focus on Maven since it’s the most common for Spring Boot, but I’ll note Gradle equivalents briefly). This project will manage dependencies and plugin versions for all sub-modules, keeping everything consistent.

  • Using Maven CLI:
    Run this command to generate the parent project (skip archetype selection since we don’t need a default app structure):

    mvn archetype:generate -DgroupId=com.example -DartifactId=parent-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    

    Then, edit the pom.xml to set packaging to pom and inherit the Spring Boot 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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>3.2.0</version> <!-- Use the latest stable version -->
            <relativePath/>
        </parent>
    
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <name>parent-project</name>
        <description>Parent project for core and API modules</description>
    
        <!-- Manage dependencies common to all modules -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.example</groupId>
                    <artifactId>commons</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.example</groupId>
                    <artifactId>core</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.example</groupId>
                    <artifactId>api</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <!-- Add other shared dependencies here (e.g., Lombok, Spring Cloud, etc.) -->
            </dependencies>
        </dependencyManagement>
    
        <modules>
            <!-- We'll add these modules next -->
            <module>commons</module>
            <module>core</module>
            <module>api</module>
        </modules>
    </project>
    
  • Gradle Equivalent:
    Create a root build.gradle with plugins { id 'java' id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' } and define subprojects in settings.gradle.

2. Create the Sub-Modules

Now we’ll add the three required modules. You can create them via CLI or your IDE (IntelliJ/Eclipse have built-in support for adding Maven sub-modules).

2.1 Commons Module

This is your shared library for code used by both core and API modules (e.g., DTOs, utility classes, constants).

  • Maven CLI:
    From the parent project directory, run:
    mvn archetype:generate -DgroupId=com.example -DartifactId=commons -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    
    Edit its pom.xml to remove the Spring Boot starter (unless it needs Spring-specific utilities) and set it as a library:
    <?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">
        <parent>
            <groupId>com.example</groupId>
            <artifactId>parent-project</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>commons</artifactId>
        <name>commons</name>
        <description>Shared utilities and models for core and API modules</description>
    
        <!-- Add dependencies only needed for shared code (e.g., Lombok, Jackson) -->
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </project>
    
    Then create your package structure under src/main/java/com/example/commons.

2.2 Core Module

This is your core tooling service, which will run as an independent Spring Boot app.

  • Maven CLI:
    Run from parent directory:
    mvn archetype:generate -DgroupId=com.example -DartifactId=core -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    
    Edit its pom.xml to include Spring Boot starters and depend on the commons module:
    <?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">
        <parent>
            <groupId>com.example</groupId>
            <artifactId>parent-project</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>core</artifactId>
        <name>core</name>
        <description>Core tooling service</description>
    
        <dependencies>
            <!-- Spring Boot starters for your core service -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- Add other starters (e.g., data-jpa, messaging) as needed -->
    
            <!-- Depend on commons module -->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>commons</artifactId>
            </dependency>
        </dependencies>
    
        <!-- Configure Spring Boot plugin to generate executable JAR -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.example.core.CoreApplication</mainClass> <!-- Your main class -->
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    Create the main application class at src/main/java/com/example/core/CoreApplication.java:
    package com.example.core;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CoreApplication {
        public static void main(String[] args) {
            SpringApplication.run(CoreApplication.class, args);
        }
    }
    

2.3 API Module

This is your REST API service for configuring the core tooling, also an independent Spring Boot app.

  • Maven CLI:
    Run from parent directory:
    mvn archetype:generate -DgroupId=com.example -DartifactId=api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    
    Edit its pom.xml to include Spring Boot web starters, depend on commons and core 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <groupId>com.example</groupId>
            <artifactId>parent-project</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>api</artifactId>
        <name>api</name>
        <description>REST API for configuring core tooling</description>
    
        <dependencies>
            <!-- Spring Boot web starter for REST API -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- Add other starters (e.g., validation, actuator) as needed -->
    
            <!-- Depend on commons and core modules -->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>commons</artifactId>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>core</artifactId>
            </dependency>
        </dependencies>
    
        <!-- Configure Spring Boot plugin for executable JAR -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.example.api.ApiApplication</mainClass> <!-- Your main class -->
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    Create the main application class at src/main/java/com/example/api/ApiApplication.java:
    package com.example.api;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ApiApplication {
        public static void main(String[] args) {
            SpringApplication.run(ApiApplication.class, args);
        }
    }
    
    Add your API controllers under com.example.api.controller, using models from com.example.commons and calling services from com.example.core.

3. Build and Verify Independent JARs

To build all modules and generate executable JARs, run from the parent project directory:

mvn clean package

You’ll find the executable JARs in:

  • core/target/core-1.0.0-SNAPSHOT.jar
  • api/target/api-1.0.0-SNAPSHOT.jar

You can run each JAR separately with:

java -jar core/target/core-1.0.0-SNAPSHOT.jar
java -jar api/target/api-1.0.0-SNAPSHOT.jar

Each will run in its own JVM, ready to be deployed to separate containers (Docker, Kubernetes, etc.).

Key Notes

  • Dependency Scope: If your core module doesn’t need to expose its internals to the API, consider using compileOnly or adjusting component scanning to avoid unintended bean sharing.
  • Port Configuration: Make sure to set different server ports in application.properties for core and API (e.g., server.port=8080 for API, server.port=8081 for core) to avoid conflicts when running locally.
  • Testing: Add test modules or directories under each sub-module to keep tests isolated to their respective components.

内容的提问来源于stack exchange,提问作者conquester

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:07:03