In this spring boot tutorial, we will learn about spring-boot-starter-parent
dependency which is used internally by all spring boot projects. We will also learn what all configurations this dependency provides, and how to override them.
1. Quick Reference
For quick reference, the Spring Boot starter parent dependency for Maven is as follows:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Similarly, Spring Boot starter parent dependency for Gradle is as follows:
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.2'
id 'io.spring.dependency-management' version '1.1.2'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
2. What is Spring Boot Starter Parent Project?
Spring Boot is designed to simplify the process of building production-ready Spring applications with minimal effort on configuration. As part of this effort, a special parent project spring-boot-starter-parent
has been created that provides default configuration and dependencies for Spring Boot-based applications.
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava</groupId>
<artifactId>spring-webmvc</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-webmvc Maven Webapp</name>
<url>https://howtodoinjava.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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>
When we create a Spring Boot project, we use the spring-boot-starter-parent
as the parent of our project’s pom.xml
or build.gradle
. After adding it, our project inherits the default build, dependencies, and configuration from this parent project, so we don’t have to specify them manually.
By using spring-boot-starter-parent
, we can jumpstart the Spring Boot application development with a solid foundation and reduce the effort needed for setting up common configurations and dependencies.
3. Spring Boot Dependencies Project
The ‘Spring Boot Starter Parent’ project further extends the spring-boot-dependencies project which is the central place for managing the default and compatible versions of Java libraries included, by default, in a Spring Boot project.
<properties>
<activemq.version>5.18.2</activemq.version>
<angus-mail.version>1.1.0</angus-mail.version>
<artemis.version>2.28.0</artemis.version>
<aspectj.version>1.9.19</aspectj.version>
<assertj.version>3.24.2</assertj.version>
<awaitility.version>4.2.0</awaitility.version>
...
...
</properties>
We can refer to the latest version of Spring Boot dependencies project and checkout its pom.xml for the latest version used for all dependencies.
4. Managing Dependencies in Our Projects
4.1. Including Dependencies with Default Version
Once we’ve declared the spring boot starter parent in the project, we can include any dependency with the default version specified in the parent by just declaring it in our dependencies tag. The version is resolved from the Spring boot dependencies project.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4.2. Including Dependencies with Different Versions
To include a dependency with a different version, we can include the dependency and its version in the <dependencyManagement>
section.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Alternatively, we can override the version number of the included libraries in the properties section.
<properties>
<junit.version>4.13.2</junit.version>
<junit-jupiter.version>5.9.3</junit-jupiter.version>
</properties>
5. Summary
This Spring Boot tutorial discusses the special projects spring-boot-starter-parent and spring-boot-dependencies, their hierarchy and the benefits they include in any Spring boot project we create. We also learned to include the default dependencies and also override the versions of those dependencies.
Drop me your questions in comments section.
Happy Learning !!
Comments