如何使用IntelliJ和Maven创建指定结构的Spring Boot多模块项目
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=falseThen, edit the
pom.xmlto set packaging topomand 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 rootbuild.gradlewithplugins { id 'java' id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' }and define subprojects insettings.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:
Edit itsmvn archetype:generate -DgroupId=com.example -DartifactId=commons -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falsepom.xmlto remove the Spring Boot starter (unless it needs Spring-specific utilities) and set it as a library:
Then create your package structure under<?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>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:
Edit itsmvn archetype:generate -DgroupId=com.example -DartifactId=core -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falsepom.xmlto include Spring Boot starters and depend on the commons module:
Create the main application class at<?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>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:
Edit itsmvn archetype:generate -DgroupId=com.example -DartifactId=api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falsepom.xmlto include Spring Boot web starters, depend on commons and core modules:
Create the main application class at<?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>src/main/java/com/example/api/ApiApplication.java:
Add your API controllers underpackage 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); } }com.example.api.controller, using models fromcom.example.commonsand calling services fromcom.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.jarapi/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
compileOnlyor adjusting component scanning to avoid unintended bean sharing. - Port Configuration: Make sure to set different server ports in
application.propertiesfor core and API (e.g.,server.port=8080for API,server.port=8081for 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

