Spring Boot技术咨询:spring-boot-starter-parent是否为pom.xml必填父依赖?
Great questions—let's break this down clearly for you:
1. Is spring-boot-starter-parent required as the parent dependency in pom.xml?
No, it's not mandatory, but it’s absolutely the recommended default for most Spring Boot projects.
The spring-boot-starter-parent does a ton of heavy lifting out of the box to simplify your setup:
- It manages all Spring Boot dependency versions (so you don’t have to specify versions for starters like
spring-boot-starter-web) - Configures default Maven plugins (like the Spring Boot repackage plugin, resource filtering, and compiler settings)
- Enforces standard project structure conventions (like recognizing
src/main/resources/application.propertiesas a valid config file)
2. Why did using spring-boot-starter-web as the parent pom cause resource filtering errors?
That makes total sense—spring-boot-starter-web is not designed to be a parent pom.
It’s a starter dependency that pulls in all the libraries you need for a web application (Spring MVC, Tomcat, etc.), but it doesn’t include the plugin configurations or resource handling rules that spring-boot-starter-parent provides. The missing application.properties error you saw happens because without the parent’s resource filtering setup, Maven wasn’t correctly copying your config file into the final build’s classpath.
3. Does a Maven pom.xml always need a parent dependency?
Nope. Maven projects can exist without a parent pom entirely. For Spring Boot specifically, if you can’t use spring-boot-starter-parent (e.g., your organization enforces its own corporate parent pom), you can still use Spring Boot by importing the dependency management bill of materials (BOM) directly.
Here’s how to set that up:
<project> <groupId>com.example</groupId> <artifactId>my-spring-boot-app</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Import Spring Boot's dependency management to handle versioning --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.2.0</version> <!-- Use your target Spring Boot version --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- Add your Spring Boot starters (no version needed thanks to the BOM) --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Manually configure the Spring Boot Maven plugin (since we're not using the parent) --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.2.0</version> <!-- Match the Spring Boot version --> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Quick Recap
- Use
spring-boot-starter-parentif you can—it simplifies your pom and cuts down on boilerplate - Never use starters like
spring-boot-starter-webas parent poms; they’re not built for that purpose - If you can’t use the official parent, use the
spring-boot-dependenciesBOM independencyManagementplus manual plugin configurations
内容的提问来源于stack exchange,提问作者Manish

