June 21, 2022

spring-boot-starter-parent in Spring Boot

In this post we’ll learn about spring-boot-starter-parent and how it helps in quick and easy Spring Boot development by providing useful Maven defaults and dependency management. We’ll also see how to override default dependencies provided by Spring Boot Starter Parent.

Spring-boot-starter-parent configuration

Mostly all Spring Boot projects using Maven as build tool inherit from the spring-boot-starter-parent project to obtain sensible defaults.

To configure your Spring Boot project to inherit from the spring-boot-starter-parent, in your pom.xml set the parent as follows:

<!-- Inherit defaults from Spring Boot -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
</parent>

Spring Boot starter parent provides the following features-

  1. Compatible Java version as the default compiler level. For Spring Boot 2.1.6 release Java 1.8 is the default.
  2. UTF-8 source encoding.
  3. Dependency Management- This section is inherited from the spring-boot-dependencies pom and it manages the versions of common dependencies. This dependency management provides a curated list of dependencies that it supports so that you do not need to provide a version for any of these dependencies in your build configuration, Spring Boot manages that for you. The curated list contains all the spring modules that you can use with Spring Boot as well as a refined list of third party libraries. The list is available as a standard Bills of Materials (BOM) manifests (spring-boot-dependencies).
  4. Execution of spring-boot:repackage goal which repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar.

Spring-boot-starter-parent composition

spring-boot-starter-parent itself inherits from spring-boot-dependencies, given below is the part of pom.xml for spring-boot-starter-parent you can get the complete configuration here- https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml

<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>
  <main.basedir>${basedir}/../../..</main.basedir>
  <java.version>1.8</java.version>
  <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <maven.compiler.source>${java.version}</maven.compiler.source>
  <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

In the properties section of this pom.xml you can see the configuration for Java version and UTF encoding.

But it is spring-boot-dependencies which provides the curated list of dependencies, in the properties section of pom.xml for spring-boot-dependencies you can see the default version for various dependencies. Complete pom.xml here- https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

<properties>
  <main.basedir>${basedir}/../..</main.basedir>
  <!-- Dependency versions -->
  <activemq.version>5.15.9</activemq.version>
  <antlr2.version>2.7.7</antlr2.version>
  <appengine-sdk.version>1.9.76</appengine-sdk.version>
  <artemis.version>2.9.0</artemis.version>
  <aspectj.version>1.9.4</aspectj.version>
  <assertj.version>3.13.1</assertj.version>
  <atomikos.version>4.0.6</atomikos.version>
  <bitronix.version>2.1.4</bitronix.version>
  <byte-buddy.version>1.9.16</byte-buddy.version>
  <caffeine.version>2.7.0</caffeine.version>
  ...
  ...


</properties>

Overriding dependency version in spring-boot-starter-parent

As we have seen spring-boot-dependencies provides a list of compatible default dependencies for the Spring Boot version used. If you want to change the version of any dependency then you can define the version in the property section of pom.xml used in your project.

For example default versions for mongodb and log4j2 in spring-boot-dependencies are as following-

<mongodb.version>3.11.0-rc0</mongodb.version>
<log4j2.version>2.12.0</log4j2.version>

If you want to provide different version then in the properties section of your Spring Boot project’s pom.xml change it as given below.

<properties>
  <java.version>12</java.version>
  <mongodb.version>3.10.2</mongodb.version>
  <log4j2.version>2.8.2</log4j2.version>
</properties>

Using Spring Boot without the Parent POM

If you do not want to inherit from spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency, as follows:

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

That's all for the topic spring-boot-starter-parent in Spring Boot. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment