Saturday, September 3, 2022

Spring Boot - spring-boot-starter-parent

In the Spring Boot Hello World Web Application Example you would have seen that spring-boot-starter-parent has been added as a parent dependency in the pom.xml. In this post we’ll discuss this spring-boot-starter-parent dependency in detail.

Dependency management in Spring Boot

Each release of Spring Boot provides a curated list of dependencies that it supports. So, you don't need to provide version number for any of the required dependencies in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way.

The list is available as a standard BOM (Bills of Materials), dependency for it is spring-boot-dependencies that can be used with both Maven and Gradle.

spring-boot-starter-parent in Spring Boot

Spring Boot users using Maven as build tool can inherit from the spring-boot-starter-parent project to obtain sensible defaults. This parent dependency can be added in the pom.xml as given below-

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
</parent>

spring-boot-starter-parent provides the following features-

  • Java 1.8 as the default compiler level. Note that Spring Boot 2.x requires Java 8 as the minimum Java version
  • UTF-8 source encoding.
  • A Dependency Management section which is inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom.
  • An execution of the repackage goal (spring-boot:repackage) with a repackage execution id.
  • Sensible resource filtering.

spring-boot-starter-parent pom

If you look at the pom.xml of spring-boot-starter-parent (Available here) you will see that it inherits from spring-boot-dependencies itself. In the properties section you can also see the configuration for Java version and UTF encoding.

Relevant section of the pom.xml is shown here.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <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>

The curated list of dependencies is configured in spring-boot-dependencies. You can see its pom.xml here

Compatible dependencies are enclosed with in the properties tag.

<properties>
    <main.basedir>${basedir}/../..</main.basedir>
    <!-- Dependency versions -->
    <activemq.version>5.15.10</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.76</appengine-sdk.version>
    <artemis.version>2.10.1</artemis.version>
    <aspectj.version>1.9.4</aspectj.version>
    ...
    ...

Using Spring Boot without the Parent POM

If you do not want to use the 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>

Customizing managed versions

To customize a managed version you set its corresponding property. For example, to customize the version of Jersey which is controlled by the jersey.version property:

<properties>
    <jersey.version>2.28</jersey.version>
</properties>

Then add dependency-

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
</dependency>

That's all for this topic Spring Boot - spring-boot-starter-parent. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Spring Boot Spring Initializr
  2. Spring Boot StandAlone (Console Based) Application Example
  3. Spring MVC @RequestParam Annotation Example
  4. Spring MVC Pagination Example Using PagedListHolder
  5. How to Read Properties File in Spring Framework

You may also like-

  1. Spring util-namespace Example For Wiring Collection
  2. Spring Asynchronous Method Execution Support Using @Async Annotation
  3. @Required Annotation in Spring Framework
  4. Spring JdbcTemplate Insert, Update And Delete Example
  5. Buffered Streams in Java IO
  6. Switch Expressions in Java 12
  7. Creating Temporary File in Java
  8. Java String Interview Questions And Answers