Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this tutorial, we'll learn about spring-boot-starter-parent. We'll discuss how we can benefit from it for better dependency management, default configurations for plugins, and to 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 configurations for Maven plugins, such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, and 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 it as a parent in our project's pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0</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. We also don't need to define versions of the dependencies; Maven will download jar files based on the version defined for the 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.4.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

5. Properties

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 even some base configurations, like managing the Java version, and 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 prefer to declare all our Maven configurations manually.

In that case, we can 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 illustrate 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 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.6.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.

8. Conclusion

In this article, we gave an overview of spring-boot-starter-parent, and the benefits 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.

The source code for the snippets used in this article is available over on Github, one using the starter parent and the other a custom parent.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!