Sub topic 1
Dependency management: the Boot parent and BOM
Every Spring Boot project starts with one question: where do the dependency versions come from? This chapter shows how Boot answers it, so that later chapters can add starters without version numbers.
Prerequisites
This chapter assumes you can:
- Read a Maven
pom.xmlor a Gradle build file. - Run a simple Spring Boot application.
- Explain what a bean and the application context are.
The problem Boot solves
A web application needs Spring MVC, Jackson, Tomcat, Logback, and more. Each library has its own release schedule, and not every combination works. Choosing compatible versions by hand is slow and error-prone.
Boot solves this with a curated list of versions that are tested together. The list lives in a Bill of Materials (BOM). A BOM is a special POM that declares versions for many libraries but adds none of them to your build.
Two ways to get the BOM
In Maven you can inherit the BOM through the Boot parent, or import it directly.
Option 1: the starter parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/>
</parent>spring-boot-starter-parent itself inherits from spring-boot-dependencies, which holds the BOM. The parent also sets sensible build defaults:
- The Java release level.
- UTF-8 source encoding.
- Resource filtering for
application.properties. - Configuration for the Boot Maven plugin, so
mvn spring-boot:runand executable JARs work.
Option 2: import the BOM
Your project may already need a different parent, such as a company-wide one. Maven allows only one parent, so import the BOM instead:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>You get the managed versions but not the plugin and build defaults. You must configure the Boot Maven plugin yourself.
Gradle
Gradle gets the same result from the Boot plugin and the dependency management plugin:
plugins {
java
id("org.springframework.boot") version "3.3.5"
id("io.spring.dependency-management") version "1.1.6"
}Declaring a dependency without a version
With the parent or BOM in place, a starter needs no <version>:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>The BOM supplies the version, and it also manages the versions of the libraries that starter pulls in. Upgrading Boot means changing one version number. Every managed library moves with it.
Overriding a managed version
Sometimes you need a different version, for example to pick up a security fix before Boot ships it. The BOM exposes version properties. With the starter parent, set the property:
<properties>
<jackson-bom.version>2.17.2</jackson-bom.version>
</properties>Treat overrides as exceptions. Each one takes a library outside the tested combination, and you become responsible for compatibility. Remove the override once Boot's own version catches up.
Check what you actually got
Run mvn dependency:tree or gradle dependencies to see the resolved versions. Managed and overridden versions both appear there, so it is the quickest way to confirm that an override took effect.
Exercise
Your company requires every project to inherit from its own parent POM. How do you still get Boot's managed versions, and what do you lose compared with using spring-boot-starter-parent?
Show answer
Import spring-boot-dependencies in dependencyManagement with type pom and scope import. You lose the parent's build defaults, such as the Java release setting, encoding, resource filtering, and the Boot Maven plugin configuration. Version properties also no longer override managed versions. You must declare the version explicitly in dependencyManagement before the import.
Exercise
A pom.xml declares spring-boot-starter-web with no version and uses the Boot 3.3.5 parent. Where does Maven find the version?
Show answer
In the spring-boot-dependencies BOM, which spring-boot-starter-parent inherits. The BOM's managed version for the starter is applied.