Updated April 17, 2023
Introduction to Spring Boot Starter Parent
Spring boot starter parent is a special project used as a starter for applications with configurations set internally and used as default and management of any sort of dependencies required inside the spring applications. Using the boot starter parent package brings along with it many advantages such as the provision of the default configurations for your spring application, simplifying the building process as it creates a tree of dependencies which makes it easy to manage. It also comes up with other configurations which can be treated as a default for various plugins in Maven. Some of the maven plugins include the maven jar plugin, maven war plugin, maven failsafe plugin and maven surefire plugin for which default configurations are provided. Along with that, the starter parent also comes along with functionality to inherit the management of dependencies from the spring-boot-dependencies package which is internally the spring boot starter parent’s parent.
In this article, we will have a look at how we can use it in our project along with the help of some examples.
How to use it?
It is very easy to download or find the latest version and add it to our project. You can find its latest version inside the maven central using this link. In the above link, you can observe all the available versions as shown below –
https://search.maven.org/classic/#search%7Cga%7C1%7Ca%3A%22spring-boot-starter-parent%22
After choosing your required version, you will find the apache maven dependency tag as shown the below image.
https://search.maven.org/classic/#artifactdetails%7Corg.apache.servicecomb%7Cspring-boot-starter-parent%7C1.3.5%7Cpom
You can also see the corresponding pom.xml file. If you already have the pom.xml file in your project, you can copy the dependency information in the apache maven section shown in the highlighted manner and paste this set of tags in your pom.xml file.
Managing Dependencies –
After declaring the starter parent of spring boot inside our project, we are free to declare any of the dependency tags for pulling any required dependencies from the parent easily. One more advantage of having this functionality is that we have the privilege of not defining the versions of the required dependencies as it is the responsibility of the maven to refer to the versions defined in the parent tag of the starter parent and download the corresponding jar files. In order to understand it better, let us take one example where we will be building a web project in which we will try to add the dependency of spring boot starter web directly without specifying its version. For that, we will be making the use of following code snippet –
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Adding dependency using Dependency Management tag –
We can provide explicitly different versions of the dependency that we are adding by specifying it in the dependency Management tag. This helps in specifying any different version of the dependency from the default version specified. For example, if we need to add the dependency of spring boot starter data jpa having version 3.2.2 then, we can make the use of the following dependency tag in pom.xml.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Properties of starter parent –
We can declare the properties and their value that we need to change in the properties section provided if we want to assign any other value than the one mentioned inside the starter parent.
We can easily have control of changing the configurations by simply changing the properties because internally, the spring boot dependencies which is its parent makes the use of the properties for handing the configurations of maven plugin, java versions and other dependencies version.
Suppose that we have to change the version of some dependency which has a value other than that mentioned in the starter parent, then we can simply configure its property and add the dependency using the dependency tag shown below –
<properties>
<junit.version>3.18</junit.version>
</properties>
Example
Firstly let us observe the contents of the default dependencies and the corresponding version in the following configuration file of pom.xml –
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd;
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${revision}</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
<name>Spring Boot Starter Parent</name>
<description>Parent pom providing dependency and plugin management for applications built with Maven</description>
<properties>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
...
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
...
</project>
For a complete reference of the configuration pom.xml file of spring boot, starter parent refers this link.
We can observe that the spring boot starter parent has the parent of spring boot dependencies and all the properties, resources and other dependencies and their corresponding properties are mentioned there. Now, suppose we want to change the property of JDBC whose default version specified by the spring boot starter parent is 5.2.12 as shown below and we want to use the version of 5.3.3 JDBC in our project.
<jdbc.version>5.2.12</jdbc.version>
Now we can change the version specified in the spring boot starter parent of JDBC dependency by using the following property tag in our project’s pom.xml file –
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdbc.version>5.3.3 </jdbc.version>
</properties>
You can confirm the change of the version by going to your eclipse editor and checking the message when you navigate your mouse pointer over the dependency tag of JDBC. By using the above way, we can specify explicitly any version that we want in our project for any of the dependencies other than the one specified in the spring boot starter parent.
Conclusion
The spring boot starter parent is a dependency management tool and makes the specification of the versions of the dependencies of maven, java and also plugins easier by providing default versions. It provides the default configurations when used as a parent in any of the child projects. We can also override the properties easily by specifying the tags in our pom.xml file of the project.
Recommended Articles
This is a guide to Spring Boot Starter Parent. Here we discuss Introduction, How to use it, examples with code implementation for better understanding. You may also have a look at the following articles to learn more –