Open In App

Spring Boot – Dependency Management

Last Updated : 20 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Spring-Boot framework is the most popular web development framework. No doubt, it provides an abundance of essential features and a convenient way to handle those features. At the heart of Spring-Boot, is the ‘Dependency Management’ feature. 

Note: Dependency Management is just a way of managing all the required dependencies in one place and efficiently making use of them. 

Importance of Dependency Management

  • It allows specifying all required dependencies according to the respective Spring-Boot version in one place itself.
  • You can specify or change the Spring-Boot version. On changing Spring-Boot versions, all the versions of mentioned(added) dependencies will be updated automatically.
  • You can prevent conflicts of different Spring-Boot libraries versions, which is beneficial for the ‘Multi-Module’ projects.

Working of Dependency Management in Spring-Boot 

  • Dependency is nothing but a ‘Library’ that provides specific functionality that we can use in our application.
  • In Spring-Boot, Dependency Management and Auto-Configuration work simultaneously.
  • It is the auto-configuration that makes managing dependencies supremely easy for us.
  • We have to add the dependencies in the pom.xml/build.gradle file.
  • These added dependencies will then get downloaded from Maven Central.
  • The downloaded dependencies will get stored into the ‘.m2’ folder in the local file system.
  • The Spring-Boot application can access these dependencies from  ‘.m2’ and its sub-directories.
  • Example -( .m2 -> repository -> org, etc )

Working of Dependency Management in Spring-Boot

Life-Cycle of Dependency Management

Example of Maven Central – ‘Starter Web’ dependency

Project Build Systems

  1. You should work with the two most used builds Maven and Gradle.
  2. Maven and Gradle use a different syntax for managing dependencies.
  3. Also, you don’t need to mention the version of the dependencies, as Spring-Boot configures them automatically. Though you can mention the version or override as well.
  4. The curated list published contains all the Spring Modules and third-party libraries that you can use with Spring-Boot.
  5. Maven manages them in the ‘pom.xml’ file, while Gradle manages them in the ‘build.gradle’ file.

Features of Maven build

  1. It uses the default Java compiler.
  2. It has UTF-8 source encoding
  3. A useful feature of not mentioning the version information of dependencies is inherited from POM ( spring-boot-dependencies ).
  4. Resource filtering and plugin configurations.
  5. Resource filtering is also for ‘application.properties’ and ‘application.yml’.

Spring-Boot Starters

Working with dependency management, Spring-Boot Starters plays an important role here. They are a set of convenient dependency descriptors that one should mention in your application. You can get access to all Spring and related tech stacks that you require for the project. A starter has a similar naming pattern – ( spring-boot-starter-* ). Third-party starters do not start with ‘spring-boot’. Star in starter pattern takes place of any technology name to be used.

Example: 'spring-boot-starter-jdbc'

Types of Starters:

  1. Application Starters.
  2. Technical Starters.
  3. Production-ready Starters.

All the required dependencies of Spring-Boot are embedded in the ‘dependencies’ tag/block.

Maven -> pom.xml

<dependencies>
     <dependency>
          <groupId> ... </groupId>
          <artifactId> ... </artifactId>
          <version> ... </version>
     </dependency>
</dependencies>

Maven

Adding Dependencies

When creating a new Spring-Boot project in STS ( Spring Tool Suite ), you can add the required dependencies while setting up the project.

  1. File
  2. New -> Spring Starter Project
  3. Next
  4. Search for the required dependencies and add them
  5. Next
  6. Finish

Dependency selection window.

To add the dependency for the current working project:

  1. Right-click on project
  2. Select Spring -> Add Starters
  3. Search for the required dependencies and add them
  4. Next
  5. Select pom.xml/HELP.md or both
  6. Finish

Appears after the above image step

If you know the dependency, you can directly place them in the pom.xml file. For example to add the Thymeleaf template engine in your project build, one can add the following dependency in the ‘<dependencies></dependencies>‘ tag.

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

 Example: pom.xml 

XML




<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>sia</groupId>
    <artifactId>GFG</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GFG</name>
    <description>GFG</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
  
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
  
</project>


Understanding/Configuring Dependencies

A – Starter Parent

To take advantage of auto-configured ‘sensible’ defaults, you should add Starter Parent in the project your build.

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

With default configuration like above, you can override respective dependencies by overriding a ‘property’.

<properties>
    <slf4j.version>___</slf4j.version>
</properties>

This will make sure that the mentioned version of a SLF4j library will be used.

You can also manage auto-configured ‘Starter Parent‘ and create a custom POM without the need to specify the first one with the help of artifact ‘scope=import’ of ‘spring-boot-dependencies.

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

After this, you can normally add the dependencies like the one mentioned above. But, to override the individual dependency, you will need to add a respective entry before the ‘spring-boot-dependencies’ entry.

<dependencyManagement>
    <dependencies>
        
        <!-- Override SLF4J provided by Spring Boot -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>___</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>___</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    
    </dependencies>
</dependencyManagement>

But, you have to manually configure the plugin management by adding ‘spring-boot-maven-plugin’ explicitly. Managing the Maven plug-in is very essential as it packs the Spring-Boot application into an executable jar.

Maven -> pom.xml

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

B – Java Version

You can also change the java version in the following –

<properties>
    <java.version>___</java.version>
</properties>

C – Developer Tools

A set of specific tools to make the application development process much easier. It is in the ‘spring-boot-devtools’ module.

Maven -> pom.xml

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
</dependency>

Gradle 

In the case of a ‘Starter Parent’ like in Maven, here there is no ‘Super Parent’ to take advantage of some auto configurations. To add dependencies in Gradle, add them in the ‘dependencies{ }’ section. For providing executable jar, you can add the following in the dependencies section –

'spring-boot-gradle-plugin'

Example – build.gradle

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.8.RELEASE")
    }
}

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")
}

For adding the ‘Developer tools‘, add the following in the ‘dependencies’ block

Gradle -> build.gradle

developmentOnly("org.springframework.boot:spring-boot-devtools")

Note: Each release of Spring Boot is associated with a base version of the Spring Framework, so it is highly recommended to not specify its version on your own.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads