Spring Boot Dependency Management with a Custom Parent

Spring Boot provides the parent pom which help us to quick boot strap a Spring Boot application. In this article we will see how to do the Spring Boot dependency management with a custom parent.

 

Introduction

Spring Boot starter parent is a convenient and easy way for dependency management. With each release, Spring Boot provides a curated list of dependencies it supports. While working on the Spring Boot application, you may not need to provide the version number for the dependencies as these are automatically taken care by Spring Boot. There are certain cases where you may not able to use the Spring Boot starter parent directly in our project:

  1. While working on enterprise application, you may need to use the organizational parent pom (enterprise/ company wide common parent).

Let’s see what are our options to handle these use cases:

 

1. Spring Boot Without Parent POM

Including the Spring Boot starter parent in your application provides several build in features which includes:

  1. Dependency management–Spring Boot automatically takes care of the version for you.
  2. Sensible Defaults
    1. Default Java version
    2. Source encoding
    3. Resource filtering, etc.
  3. Plugin management

Not everyone enjoys inheriting from the spring-boot-starter-parent POM.Spring Boot is flexible and provide option if you do not want to use the spring-boot-starter-parent,In case you don’t want to use the parent pom but still like to keep the benefit of the dependency management, we can use the scope=import dependency:

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>latest version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies&gt;
</dependencyManagement>

once we add the above entry in the pom.xml, we can start adding Spring Boot dependencies as usual (adding no version information etc.)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

You will lose the Spring Boot plugin management feature without parent pom

If you like to use the spring-boot-maven-plugin, we need to add it explicitly in our pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

 

2. Overriding Dependency

Using  scope=import dependency will not allow us to override individual dependencies (In case we want to use a different version than what managed by Spring Boot). To use a different version than what managed by Spring Boot, we need to declare it on the dependencyManagement section, before spring-boot-dependencies is declared:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

Summary

In this post, we saw how to do the Spring Boot dependency management with a custom parent. We discussed the various benefits of using the Spring Boot starter parent and the options to keep these benefits while using a custom parent pom.

Manish Sharma

Manish's primary interests are Java, Spring Boot and Spring. His focus is more toward the automations and testing.Manish love travelling and when not working, he might be exploring some new destination.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of