Spring Boot Dependencies

Spring Boot Dependencies & Dependency Management

The Spring Boot automatically configures all the necessary Spring Boot dependencies by itself. Each release of Spring Boot provides a list of supported dependencies; it is available as a part of the Bills of Materials that can be used with Maven. So, we are required to specify the dependency’s version in our project configuration. It will manage all the necessary dependencies itself. By default, It also upgrades all the dependencies in a consistent way when the Spring Boot version is updated.

Let’s have a look at the quick index of the Spring Boot dependencies topic:

Spring Boot Dependencies

Build System

Spring Boot supports several build systems such as Maven, Gradle, Ant. It is strongly recommended to select a build system that supports dependency management and can consume published artifacts to the “Maven Central“. The recommended build systems are Maven and Gradle as they provide easy dependency management. However, there is another build Ant is also available, but it will not be particularly well supported as compared to Maven and Gradle.

Spring Boot Dependency Management

Each release of Spring Boot provides a list of supported dependencies. We do not need to explicitly specify the dependency version in our Spring Boot project configuration; it will automatically manage for us. Even when we upgrade our Spring Boot version; it will automatically consistently update those dependencies. However, we can specify a version and override Spring Boot’s recommendations if it is necessary.

The provided list holds all the Spring modules that we can use with Spring Boot as well as third party libraries. The list is available as standard Bills of Materials with dedicated support for Maven and Gradle.

It is recommended to not specify Spring Version as each release of Spring Boot is associated with a base version of Spring Framework.

Before diving into Spring Boot dependencies and it’s management. Let’s discuss it’s advantages:

Advantages of Dependency Management

The following are some key advantages of dependency management:

  • It centralizes the dependency information by specifying the Spring Boot version in one place.
  • It is useful if we want to change the Spring Boot version.
  • It avoids the collapsing of different versions of Spring Boot libraries. We only need to specify the library name with a specific version.
  • It is useful in multi-module project.

Spring Boot Dependency Management for Maven Projects

To obtain the default configuration for the Maven project, users can inherit from the spring-boot-starter-parent project. The tools & features provides by the starter parent project are as follows:

  • The default Java compiler
  • UTF-8 source encoding
  • A Dependency Management section, which is useful for omitting the <version> tags for common dependencies, is inherited from the spring-boot-dependencies POM.
  • Sensible resource filtering.
  • Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).
  • Sensible resource filtering for application.properties and application.yml

We can override the default config files with a maven property resource.delimeter.

Inheriting the starter parent

To configure the Spring Boot project to inherit from the spring-boot-starter-parent dependency, place the below configuration code to your pom.xml file:

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

You should only specify the Spring Boot version. If you want to import the additional starters, you can safely ignore the version number.

We can also override individual dependencies by specifying them in our project. For example, to upgrade our project to another Spring Data release train, we have to add the following dependency:

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

We can also checkout a list of supported properties from spring-boot-dependencies pom.

Spring Boot Project without Parent POM

What if you want to use your configuration in the project, or don’t want to configure the default configuration? Spring Boot allows us to define the project configuration explicitly. We may use our corporate standard parent and explicitly declare all our Maven configuration.

Thus, we can take advantage of dependency management. To take the advantage of dependency management without initializing the default parent POM, we will have to declare the required dependency by using the scope=import dependency.

Consider the below configuration:

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

The above configuration will not override individual dependencies using a property as mention above in this section. To do so, we have to add an additional entry in the dependency management of our project before the Spring Boot dependency entry.

For example, to upgrade to another Spring Data release, add the following code in pom.xml:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Similarly, we can override any of Spring Boot dependencies.

How to change Java Version?

By default, the spring-boot-starter-parent chooses suitable conservative Java version. If you want to any other Java version, add a the following property:

<properties>
    <java.version>1.8</java.version>
</properties>

Spring Boot Maven Plug-in

The Spring Boot Maven Plug-in packages the project as an executable jar. To add the Spring Boot Maven plug-in, place the below configuration code to <plugins> section:

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

Note: if we are using the Spring Boot starter parent pom, we are only required to add the plugin, there is no need to configure it unless you want to change the settings defined in the parent.

Spring Boot Dependency Management for Gradle Projects

The starter POMs can be directly imported to the Gradle project in their dependencies section. Unlike Maven, there is a “super parent” to import to share some configuration in Gradle.

apply plugin: 'java'
repositories {
    jcenter()
}
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:2.4.0")
}

The Spring Boot also supports spring-boot-gradle-plugin and provides tasks to create executable jars and run projects from the source. Further, we can also take advantage of dependency management, among other capabilities. It also allows us to omit the version number for any dependencies that are managed by Spring Boot:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.4.0")
    }
}
apply plugin: 'java'
apply plugin: 'spring-boot'
repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Spring Boot Dependency Management for Ant Projects

We can also build a Spring Boot project using Apache Ant+Ivy. The spring-boot-antlib “AntLib” module helps Ant to create executable jars.

The declaration of dependencies in ivy.xml file will look like as follows:

ivy-module version="2.0">
    <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
    <configurations>
        <conf name="compile" description="everything needed to compile this module" />
        <conf name="runtime" extends="compile" description="everything needed to run this module" />
    </configurations>
    <dependencies>
        <dependency org="org.springframework.boot" name="spring-boot-starter"
            rev="${spring-boot.version}" conf="compile" />
    </dependencies>
</ivy-module>

and, the build.xml file will look like as follows:

<project
    xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:spring-boot="antlib:org.springframework.boot.ant"
    name="myapp" default="build">

    <property name="spring-boot.version" value="2.4.0" />

    <target name="resolve" description="--> retrieve dependencies with ivy">
        <ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>

    <target name="classpaths" depends="resolve">
        <path id="compile.classpath">
            <fileset dir="lib/compile" includes="*.jar" />
        </path>
    </target>

    <target name="init" depends="classpaths">
        <mkdir dir="build/classes" />
    </target>

    <target name="compile" depends="init" description="compile">
        <javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
    </target>

    <target name="build" depends="compile">
        <spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
            <spring-boot:lib>
                <fileset dir="lib/runtime" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
</project>