Sub topic 8
Reading the Build File and Managed Dependencies
This chapter assumes you know basic Maven usage (pom.xml, coordinates, scopes) and have generated a project with Spring Initializr. Here you will learn how the build file keeps dependency versions consistent, and what each part does.
What the build file does in a Boot project
The build file (pom.xml for Maven) declares three things: which Spring Boot version you use, which starters you need, and how the project is packaged. Spring Boot's main build-time feature is dependency management. It publishes a tested set of library versions, so you rarely write a version number yourself.
The parent and the Boot plugin
A generated Maven project starts with a parent element that points at spring-boot-starter-parent. This is the only place you set the Boot version.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/>
</parent>The parent supplies several things:
- A dependency management section that fixes versions for hundreds of libraries, such as Spring, Jackson, Hibernate, and Tomcat.
- Default plugin configuration, including the Java compiler settings and UTF-8 encoding.
- Resource filtering for
application.propertiesandapplication.yml.
The spring-boot-maven-plugin in the build section adds the Boot goals. spring-boot:run starts the app. During package, it repackages the normal JAR into an executable one.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Managed dependencies
Compare this with a plain Maven project, where each dependency needs a version. Here, the versions come from the parent.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Neither dependency has a <version>. Maven looks it up in the parent's dependency management. The first starter brings in Spring MVC, JSON support, and an embedded Tomcat. The second brings in JUnit, Mockito, and Spring's test support. The test scope keeps these libraries out of the packaged application.
Overriding a managed version
You can override a managed version by setting a property in your pom.xml. The property names come from the Boot dependency list.
<properties>
<java.version>21</java.version>
<jackson-bom.version>2.17.2</jackson-bom.version>
</properties>Use overrides sparingly. Boot tests its version set as a whole. If you replace one library, you take responsibility for compatibility. A common reason is to pick up a security fix before the next Boot release.
When you cannot use the parent
Your organization may require its own parent POM. Maven allows only one parent. In that case, import the Boot version table through the dependencyManagement section 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 keep the version management, but you must configure the plugin and compiler settings yourself.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `dependencies.dependency.version is missing` | The library is not in the Boot version table | Add an explicit `<version>` |
| Class not found at runtime in the JAR | Dependency has `provided` or `test` scope | Change the scope |
| `java -jar` reports "no main manifest attribute" | The Boot plugin is missing, so the JAR was not repackaged | Add `spring-boot-maven-plugin` |
| Two versions of one library on the classpath | A version was pinned that conflicts with the managed one | Remove the pin or use the managed property |
Exercise
Your pom.xml declares spring-boot-starter-web with no <version>. Where does Maven get the version, and what is one way to change the version of a single managed library?
Show answer
Maven gets it from the dependency management in spring-boot-starter-parent, which inherits it from spring-boot-dependencies. To change one library, set its version property in the <properties> section, for example <jackson-bom.version>2.17.2</jackson-bom.version>. You could also put an explicit <version> on that dependency.
Exercise
Why would a team import spring-boot-dependencies in dependencyManagement instead of using spring-boot-starter-parent?
Show answer
Maven allows only one parent. If the team must inherit from a company parent POM, importing the Boot version table is the way to keep managed versions. They must then configure the plugin and compiler settings themselves.