能否创建兼具Spring Boot自定义父POM的多模块项目?及配置咨询
Absolutely! You can absolutely build a multi-module Maven project where the parent POM serves two key roles: managing Spring Boot dependency versions and acting as a container for your custom client/util JARs. Let me break down the correct setup with sample configurations, plus troubleshoot common issues you might be facing.
Step 1: Define Your Project Structure
First, lay out a clean module hierarchy to keep things organized:
my-shared-parent-project/ ├── pom.xml # Parent POM (packaging: pom) ├── client-jar-1/ # Custom utility/client library (packaging: jar) ├── spring-app-1/ # Independent Spring Boot app (packaging: jar) └── spring-app-2/ # Another independent Spring Boot app (packaging: jar)
Step 2: Configure the Parent POM
This POM will inherit Spring Boot's dependency management, declare all child modules, and centralize version control for your custom JARs.
<?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 POM Metadata --> <groupId>com.yourcompany</groupId> <artifactId>shared-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>Shared Parent POM</name> <description>Manages Spring Boot dependencies and custom client modules</description> <!-- Inherit Spring Boot's official dependency management --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <!-- Use your preferred Spring Boot version --> <relativePath/> <!-- Pull from Maven repo instead of local files --> </parent> <!-- List all child modules so Maven builds them in order --> <modules> <module>client-jar-1</module> <module>spring-app-1</module> <module>spring-app-2</module> </modules> <!-- Centralized dependency management for all child modules --> <dependencyManagement> <dependencies> <!-- Declare your custom client JAR so children can reference it without versions --> <dependency> <groupId>com.yourcompany</groupId> <artifactId>client-jar-1</artifactId> <version>${project.version}</version> </dependency> <!-- Optional: Override Spring Boot's default dependency versions if needed --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.3</version> </dependency> </dependencies> </dependencyManagement> <!-- Shared build properties for all modules --> <properties> <java.version>17</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> </project>
Step 3: Configure the Custom Client JAR Module
This is a standard JAR (not a Spring Boot app) containing your shared utilities or client code. It only inherits from your parent POM.
<?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>com.yourcompany</groupId> <artifactId>shared-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>client-jar-1</artifactId> <name>Client Utility Jar</name> <description>Shared client code/utilities for Spring Boot apps</description> <packaging>jar</packaging> <!-- Add dependencies specific to this utility module --> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
Key Note: Don't add the Spring Boot Maven plugin here—this module should be a standard, non-executable JAR.
Step 4: Configure a Spring Boot Child Application
This module is an independent Spring Boot app that inherits from your parent POM. It can optionally include your custom client JAR.
<?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>com.yourcompany</groupId> <artifactId>shared-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>spring-app-1</artifactId> <name>Spring Boot App 1</name> <description>Independent Spring Boot app using shared parent and client module</description> <packaging>jar</packaging> <!-- Dependencies: Spring Boot starters + optional custom client JAR --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Optional: Only include if this app needs the client library --> <dependency> <groupId>com.yourcompany</groupId> <artifactId>client-jar-1</artifactId> </dependency> </dependencies> <!-- Spring Boot plugin to create an executable JAR --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Common Pitfalls & Fixes
If you're hitting issues, here are the most likely culprits:
Child app can't resolve the client JAR
- Fix: Ensure the client module is listed in the parent's
<modules>section. Runmvn clean installon the parent project first to install the client JAR to your local Maven repo.
- Fix: Ensure the client module is listed in the parent's
Spring Boot dependency versions aren't being applied
- Fix: Make sure your parent POM inherits from
spring-boot-starter-parentas its direct parent. If you need to override a version, declare it in the parent's<dependencyManagement>section (this takes precedence over Spring Boot's defaults).
- Fix: Make sure your parent POM inherits from
Client module is packaged as an executable JAR
- Fix: Remove the Spring Boot Maven plugin from the client module's POM. It should only exist in your Spring Boot application modules.
Version conflicts between parent and Spring Boot
- Fix: If your parent POM needs to inherit from another non-Spring Boot parent, use Spring Boot's dependency BOM instead of inheriting the starter parent:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.2.0</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Your custom dependencies here --> </dependencies> </dependencyManagement>
- Fix: If your parent POM needs to inherit from another non-Spring Boot parent, use Spring Boot's dependency BOM instead of inheriting the starter parent:
内容的提问来源于stack exchange,提问作者sdc

