Spring Boot Starter Parent Example

Published
Updated

The spring-boot-starter-parent is a unique project starter for rapid application development that offers a number of default configurations and provides:

  • a Spring Boot maven build plugin
  • Structured default configurations for Maven
  • Defines the dependency-management in Maven so no version numbers are required on included Spring dependencies

You can start configuring your Spring Boot project by including this as a parent in the pom file of your application:

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

The provided version number is dynamically updated from Maven Central

Spring Boot Dependencies

The spring-boot-starter-parent dependency does not include all necessary dependencies. As such, classes such as the CommandLineRunner, the @SpringBootApplication annotation and other configurations will not be included unless one of the following dependencies are used. These should be included in the <dependencies> block within your project pom xml file.

For command-line applications:

The spring-boot-starter dependency provides imports for invoking your Spring Boot Application via the command line runner and should be used for applications which are not intended to be used as web applications.

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

For web applications:

The spring-boot-starter-web dependency is useful for providing all necessary configurations and dependencies for your web project (Web Service, REST API, etc.)

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

Other Spring Boot Starters:

Please see my page on Spring Starters for a comprehensive list of all other Spring Boot Starters that can be included, such as:

Configuring a Spring Boot Project without a Parent

If you already have a parent pom entry defined, it is not possible to define another as only one parent can be used within a project. However, it is possible to achieve a similar configuration by utilizing the spring-boot-dependencies artifact within the dependencyManagement section of your pom file:

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

Spring Boot Maven Plugin

The provided code instructs Maven on how to build and run your Spring Boot Application. You can build your Spring Boot application at any time using mvn clean compile package and run your app using mvn spring-boot:run.

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

Overriding Spring Dependency Versions

If you need to use a dependency version that is different than the one the Spring Boot Starter Parent provides, simply specify the version number on the desired dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-json</artifactId>
    <version>2.4.1</version>
</dependency>

Please note that your IDE may issue a warning indicating when overriding a managed version, you can ignore this.

Creating your Application Entry-point

Finally, your project will need a Spring Boot entry point. This can be created by making a main method and calling the SpringApplication.run method on your application class, and marking it with a @SpringBootApplication annotation:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MyExampleApplication {

    private static final Logger logger = LoggerFactory.getLogger(MyExampleApplication.class);
    
    public static void main(String[] args) {
        SpringApplication.run(MyExampleApplication.class, args);
    }
}

Summary

The spring-boot-starter-parent is a key starting point for the bootstrapping of your Spring Boot project. It provides underlying configurations for Maven and provides cohesion for Spring’s dependency system.