Spring Boot Starter Parent: Configure a Spring Boot Project

Spring Boot Starter Parent

The Spring Boot Starter Parent is a project starter used to import default configuration for Spring Boot applications. It is a parent starter for all Spring Boot configurations, which means we have to define the Spring Boot Starter in our project in our project’s pm.xml file. All the dependencies in Spring Boot internally uses this starter.

The spring-boot-starter-parent imports all the default configurations of the Spring Boot project and provides a complete dependency tree to quickly bootstrap our Spring Boot project.

It also facilitates with the default configuration for the Maven plug-ins such as maven-jar-plugin, maven-surefire-plugin, maven-war-plugin, and maven-failsafe-plugin.

Apart from the above configurations, it also inherits the dependency management from spring-boot-dependencies, which is parent dependency for the spring-boot-starter-parent.

In order to use it, we have to add the following configuration code in our pom.xml file.

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

As of now, the latest version is 2.4.2, but, we can always get the latest version of the starter Parent from Here.

In this section, we have included:

Features of Starter Parent

The Spring Boot Parent Starter manages the following things for multiple child projects and modules:

Configuration: It allows us to configure the Java version and other project related configurations.

Dependency Management: It provides easy control and management for the versions of dependencies to avoid conflict.

Source encoding

Default Java Version

Resource filtering

Plug-in Management: It also provides the control and management for the default plugin configuration.

Apart from all the above features, it also inherits dependency management from the spring-boot-dependencies, All we need to specify the Spring Boot version. In case if you have any additional requirements for the starter, you can safely omit the version number.

Dependency Management in Spring Boot Starter Parent

Once, we’ve declared the spring-boot-starter-parent in our project, we can easily pull any dependency from the parent; all we need to declare it in a <dependencies> tag. There is no need to define the dependencies version number, the Maven will download the required jar files on the basis of the defined version number for the starter parent in the parent tag.

For example, to add the spring-boot-starter-web dependency ( For a web project), put the below configuration code in our pom.xml file:

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

The above configuration will automatically import the required jar files for the specified starter parent version.

The <dependencyManagement> Tag

If you want to manage a different version of a dependency provided by the starter parent, we can specify the dependency version with a specific dependency in the <dependencyManagement> tag.

Consider the below configuration code:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

The above code will explicitly import the spring-boot-starter-data-jpa configurations files for version 2.4.2.

Default Parent POM (Properties)

The <properties> tag defines the default values of the Spring Boot application. Below is the default configuration code of the pom.xml:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <java.version>1.8</java.version> 
  </properties>

In the above default configuration code, we can explicitly define the specific properties. For example, the default version of Java is 1.8, but, we can override the Java version by defining it in the project pom. The parent pom also holds few other settings related to encoding and source.

Spring Boot will use these defaults if we do not explicitly specify other properties in the application.properties file.

Plug-in Management

The Spring Boot Parent Starter provides the default configuration for a host of plug-ins including maven-jar-plugin, maven-failsafe-plugin, and maven-surefire-plugin.

We can find the below default configuration code in our pom.xml:

<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>

Spring Boot Project without the Starter Parent

If you want to provide a custom Maven parent, or, may prefer to declare all the Maven configuration manually. In such a case, we can omit the use of the spring-boot-starter-parent. It does not mean that we can not take advantage of its dependency management tree. Still, we can take advantage of its dependency tree by adding a dependency spring-boot-dependencies in our project under import scope (<scope> tag).

Let’s consider the below code, which we will use other starter parent than spring-boot-starter-parent:

<parent>
    <groupId>com.javasterling</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

in the above configuration code, we have used spring-boot-parent modules in a different project as our parent dependency.

Now, in such a scenerio, we can also take advantage of dependency management by adding a dependency in import scope of our pom.xml file:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Furthermore, we can add some additional dependencies under the <dependencies> tag. There is no need to specify the version number for these dependencies.

What does Starter Parent inherit from spring-boot-dependencies?

Spring Boot Dependencies provides the default dependency management for all Spring Boot projects. If we want to specify a new version of an individual dependency, we can override it by defining it under the properties tag.

Since the Starter Parent inherits dependencies from the spring-boot-dependencies, it holds all these characteristics as well.

The following are some useful dependencies that are managed by Spring Boot by defaut:

<properties>
	<activemq.version>5.13.4</activemq.version>
	...
	<ehcache.version>2.10.2.2.21</ehcache.version>
	<ehcache3.version>3.1.1</ehcache3.version>
	...
	<h2.version>1.4.192</h2.version>
	<hamcrest.version>1.3</hamcrest.version>
	<hazelcast.version>3.6.4</hazelcast.version>
	<hibernate.version>5.0.9.Final</hibernate.version>
	<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
	<hikaricp.version>2.4.7</hikaricp.version>
	<hikaricp-java6.version>2.3.13</hikaricp-java6.version>
	<hornetq.version>2.4.7.Final</hornetq.version>
	<hsqldb.version>2.3.3</hsqldb.version>
	<htmlunit.version>2.21</htmlunit.version>
	<httpasyncclient.version>4.1.2</httpasyncclient.version>
	<httpclient.version>4.5.2</httpclient.version>
	<httpcore.version>4.4.5</httpcore.version>
	<infinispan.version>8.2.2.Final</infinispan.version>
	<jackson.version>2.8.1</jackson.version>
	....
	<jersey.version>2.23.1</jersey.version>
	<jest.version>2.0.3</jest.version>
	<jetty.version>9.3.11.v20160721</jetty.version>
	<jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
	<spring-security.version>4.1.1.RELEASE</spring-security.version>
	<tomcat.version>8.5.4</tomcat.version>
	<undertow.version>1.3.23.Final</undertow.version>
	<velocity.version>1.7</velocity.version>
	<velocity-tools.version>2.0</velocity-tools.version>
	<webjars-hal-browser.version>9f96c74</webjars-hal-browser.version>
	<webjars-locator.version>0.32</webjars-locator.version>
	<wsdl4j.version>1.6.3</wsdl4j.version>
	<xml-apis.version>1.4.01</xml-apis.version>
</properties>

The minimum required version for the Maven is 3.2.1.

How to override the default dependency version?

As we have discussed, the Spring Boot has the default version to use for most of the dependencies. But, we can override any version if needed, in the properties tag in your project’s pom.xml file.

For example, the default version of the google GSON library is 2.8.2.

<groovy.version>2.4.14</groovy.version>
<gson.version>2.8.2</gson.version>
<h2.version>1.4.197</h2.version>

But, if you want to use the version 2.7, so specifiy it in the properties tag as follows:

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gson.version>2.7</gson.version>
</properties>

Summary:

In this tutorial, we have discussed spring-boot-starter-parent and the benefit of adding it as a parent in any child project.

Now, we can easily manage and override the dependencies.

See Spring Boot Starters for the list of Spring Boot Starters.