Spring Boot Starter Parent is a starter project that provides the default configuration for spring-based applications. It is added as a parent in the pom.xml file. The spring-boot-starter-parent defines spring-boot-dependencies as its parent. The spring-boot-starter-parent inherits dependency management from spring-boot-dependencies. Every Spring Boot release provides a list of dependencies and the latest versions it supports. The dependency management feature of the starter parent allows the common dependencies to omit the <version> tag in the pom.xml file. If a dependency requires a specific version other than the version configured by the starter parent, that can be added using the <version> tag. The following are the features of the starter parent project:
- The dependency management feature manages the versions of common dependencies.
- Provide the default compiler level as Java 1.8 and UTF-8 source encoding.
- Provides a default configuration for Maven plugins such as maven-surefire-plugin, maven-jar-plugin, and maven-failsafe-plugin.
- Executes a repackage goal with a repackage execution id.
- Resource filtering and configuring profile-specific files.
The parent of spring-boot-starter-parent looks like the following
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.0.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
The following code has to be added in the pom.xml to use spring-boot-starter-parent as a parent project.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <!-- spring boot version number --> <relativePath/> <!-- lookup parent from repository --> </parent>
Managing dependencies
Any spring boot starter can be included under the dependencies section. If we omit the version for a particular starter, Maven will download the jar files based on the version number defined in the parent section. For example, if we require a data access layer, then we should add spring-data-jpa dependency starter in the pom.xml file.
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency> <dependencies>
Dependency Management Tag
If you require a different version of dependency provided by the spring-boot-starter-parent, we can add a particular version inside the <version></version> tag and include the dependency and its version inside the dependencyManagement section.
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.0</version> </dependency> </dependencies> </dependencyManagement>
Properties
We know that spring-boot-starter-parent configures the java compiler version, Maven plugins version, and Dependencies version using the properties defined inside it. We can override those property values inside the pom.xml file under the properties section. Suppose our project requires a different version of the sl4j library and a different java version.
<properties> <java.version>1.8</java.version> <slf4j.version>1.7.30</slf4j.version> </properties>
Using Spring Boot Without a Starter Parent
If we want to inherit from the custom parent POM or define all the Maven configurations manually, we don’t inherit from the spring-boot-starter-parent pom. However, we can still benefit from the dependency management feature(not the plugin management) provided by spring-boot-dependencies, by including the dependency inside the dependencyMangement section as an import scope.
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.5.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Note: To override the properties of individual dependencies, we need to add those dependencies prior to the addition of spring-boot-dependencies.