Sub topic 8
Reading the build file and managed dependencies
This chapter explains how a Spring Boot build file works and why most dependencies in it have no version number. It assumes you can read a Maven pom.xml (or a Gradle script), know what a transitive dependency is, and have a project generated by Spring Initializr. Maven is the main example. Gradle appears where it differs.
What the build file does
The build file in a Boot project has three jobs:
- It chooses a Spring Boot version.
- It lists the starters your code needs.
- It adds the plugin that turns the compiled project into a runnable application.
Boot's dependency management makes the first job do most of the work. Choosing one Boot version fixes the versions of hundreds of libraries, so you never pick them one by one.
A complete generated pom.xml
This is the file Initializr produces for a Maven project with the Web dependency, trimmed to the essentials.
<?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.4.1</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Initializr writes whatever Boot version is current when you generate the project. 3.4.1 here is only an example.
The parent section
The <parent> element is the only place a Boot version appears. spring-boot-starter-parent supplies four things:
- A
dependencyManagementsection that fixes versions for Spring libraries and common third-party ones, such as Jackson, Hibernate, Logback, and JUnit. - Default compiler settings. The
java.versionproperty sets the Java release, and source files are read as UTF-8. - Resource filtering, so
application.propertiescan use@property@placeholders. - Plugin configuration, including the
repackagegoal forspring-boot-maven-plugin.
<relativePath/> is empty on purpose. It tells Maven to fetch the parent from a repository instead of looking for a pom.xml in the folder above.
The dependencies section
Neither starter has a <version>. Maven finds the version in the parent's dependency management. The test scope on spring-boot-starter-test keeps JUnit, Mockito, and AssertJ out of the packaged application. They are on the classpath only while tests compile and run.
Each starter is a small POM that pulls in other libraries. spring-boot-starter-web brings in Spring MVC, Jackson for JSON, validation support, and spring-boot-starter-tomcat for the embedded server. Auto-configuration, covered in an earlier chapter, reacts to what these transitive libraries put on the classpath.
How versions are resolved
The version list lives in spring-boot-dependencies, a BOM (bill of materials). A BOM is a POM that holds only version declarations. The starter parent inherits from it, so you receive the same list.
Overriding a managed version
Boot exposes each managed version as a property. Setting the same property in your <properties> block replaces the managed value.
<properties>
<java.version>17</java.version>
<postgresql.version>42.7.4</postgresql.version>
</properties>Property names come from the spring-boot-dependencies POM for your Boot version. Look them up there or in the reference documentation instead of guessing. Writing <version> on a dependency also overrides the managed version. That works, but the property approach is better. It changes the version everywhere the library appears, including transitive uses.
Using the BOM without the parent
Many organizations require their own corporate parent POM, and Maven allows only one parent. In that case you import the BOM instead.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.4.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>You get the version management but not the other parent features. You must set the Java release yourself, and you must configure spring-boot-maven-plugin with an explicit repackage execution. Property overrides also work differently. To override a version here, declare the dependency in dependencyManagement ahead of the import.
The Gradle equivalent
Gradle uses plugins in place of a parent.
plugins {
java
id("org.springframework.boot") version "3.4.1"
id("io.spring.dependency-management") version "1.1.7"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}The org.springframework.boot plugin provides bootRun and bootJar. The io.spring.dependency-management plugin applies the Boot BOM, so the starters again need no versions.
| Concern | Maven | Gradle |
|---|---|---|
| Boot version set in | `<parent>` | `org.springframework.boot` plugin |
| Version management from | Parent's `dependencyManagement` | `io.spring.dependency-management` plugin |
| Run in development | `./mvnw spring-boot:run` | `./gradlew bootRun` |
| Build executable JAR | `./mvnw package` | `./gradlew bootJar` |
| Test-only dependency | `<scope>test</scope>` | `testImplementation` |
Inspecting what you actually get
The build file shows only the dependencies you declared. To see the full set, print the tree.
./mvnw dependency:treeThe output shows spring-boot-starter-web followed by its transitive dependencies, each with the resolved version. Use it to check which Jackson or Logback version you received, or to find which starter brought in an unexpected library. In Gradle, ./gradlew dependencies --configuration runtimeClasspath gives the same information.
Swapping a transitive dependency
Starters can be adjusted. To run on Jetty rather than Tomcat, exclude the Tomcat starter from the web starter and add the Jetty one.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>Auto-configuration then finds Jetty on the classpath and starts it. The code does not change.
The Boot Maven plugin
spring-boot-maven-plugin has two roles. The spring-boot:run goal starts the application from your project during development. The repackage goal runs during package and rewrites the plain JAR into an executable one. That JAR contains your classes, all dependency JARs, and a launcher that Java's -jar option can start. The next chapters cover packaging in detail. For now, note that the plugin needs no version because the parent manages it.
Common errors
'dependencies.dependency.version' is missing. The library is not in Boot's managed list. Either add a<version>yourself or choose a starter that covers it. Many less common libraries are unmanaged.no main manifest attribute, in app.jar. The JAR was not repackaged. The plugin is probably missing from<build>, or you imported the BOM and did not configure therepackageexecution.- Two embedded servers on the classpath. Adding a second server starter without excluding Tomcat leads to confusing startup behavior. Check
dependency:treeand exclude one. release version 21 not supportedor similar compiler errors. Thejava.versionproperty asks for a newer Java than the JDK running Maven. Match the property to your installed JDK, or install the right one.- Version conflicts after a manual override. If a runtime error mentions a missing method in a library you overrode, remove the override and test with the managed version first.
- Upgrading by editing only one starter's version. Starters should share the Boot version. Change the parent version, and leave starters without versions.
Exercise
A teammate adds <version>6.1.0</version> to the spring-boot-starter-web dependency to "pin" it. What is wrong with this, and what is the correct way to move to a different Boot release?
Show answer
Starters are managed by the parent, so a version on one starter can make it disagree with the Boot version used by every other starter and managed library. The correct approach is to remove the version from the starter and change the <version> inside the <parent> element. All starters and managed libraries then move together.
Exercise
Your project must use a corporate parent POM, so you cannot inherit from spring-boot-starter-parent. How do you still get Boot-managed versions, and what extra setup do you need for the executable JAR?
Show answer
Import spring-boot-dependencies in dependencyManagement with <type>pom</type> and <scope>import</scope>. You must then set the Java release yourself and configure spring-boot-maven-plugin with an explicit repackage execution, because the parent no longer supplies that configuration.