In this tutorial, we'll learn about spring-boot-starter-parent and how we can benefit from it for better dependency management, default configurations for plugins and quickly build our Spring Boot applications.
We'll also see how we can override the versions of existing dependencies and properties provided by starter-parent.
2. Spring Boot Starter Parent
The spring-boot-starter-parent project is a special starter project – that provides default configurations for our application and a complete dependency tree to quickly build our Spring Boot project.
It also provides default configuration for Maven plugins such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, maven-war-plugin.
Beyond that, it also inherits dependency management from spring-boot-dependencies which is the parent to the spring-boot-starter-parent.
We can start using it in our project by adding this as a parent in our project's pom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent>
We can always get the latest version of spring-boot-starter-parent from Maven Central.
3. Managing Dependencies
Once, we've declared the starter parent in our project, we can pull any dependency from the parent by just declaring it in our dependencies tag.
Also, we don't need to define versions of the dependencies, Maven will download jar files based on the version defined for starter parent in the parent tag.
For example, if we're building a web project, we can add spring-boot-starter-web directly, and we don't need to specify the version:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
4. The Dependency Management Tag
To manage a different version of a dependency provided by the starter parent we can declare dependency and its version explicitly in the dependencyManagement section:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.1.1.RELEASE</version> </dependency> </dependencies> </dependencyManagement>
To change the value of any property defined in the starter parent, we can re-declare it in our properties section.
The spring-boot-starter-parent via its parent spring-boot-dependencies uses properties for configuring all the dependencies versions, Java version, and Maven plugin versions.
Therefore, it makes it easy for us to control these configurations by just changing the corresponding property.
If we want to change the version of any dependency that we want to pull from the starter parent, we can add the dependency in the dependency tag and directly configure its property:
<properties> <junit.version>4.11</junit.version> </properties>
6. Other Property Overrides
We can also use properties for other configurations such as managing plugin versions or for even some base configuration like managing the Java version, source encoding.
We just need to re-declare the property with a new value.
For example, to change the Java version we can indicate it in the java.version property:
<properties> <java.version>1.8</java.version> </properties>
7. Spring Boot Project Without Starter Parent
Sometimes we have a custom Maven parent. Or, we may prefer to declare all our Maven configurations manually.
In that case, we may opt to not use the spring-boot-starter-parent project. But, we can still benefit from its dependency tree by adding a dependency spring-boot-dependencies in our project in import scope.
Let's explain this with a simple example in which we want to use another parent other than the starter parent:
<parent> <groupId>com.baeldung</groupId> <artifactId>spring-boot-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
Here, we have used parent-modules a different project as our parent dependency.
Now, in this case, we can still get the same benefits of dependency management by adding it in import scope and pom type:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Furthermore, we can pull in any dependency by just declaring it in dependencies as we did in our previous examples. No version numbers are needed for those dependencies.
In this tutorial, we have given an overview of spring-boot-starter-parent and the benefit of adding it as a parent in any child project.
Next, we learned how to manage dependencies. We can override dependencies in dependencyManagement or via properties.
Source code for snippets used in this tutorial is available on Github, one using the starter parent and the other a custom parent.