The Wayback Machine - https://web.archive.org/web/20241125194337/https://www.geeksforgeeks.org/spring-boot-dependency-management/
Open In App

Spring Boot – Dependency Management

Last Updated : 20 Feb, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Previous Article
Next Article

Similar Reads

Spring Boot - Dependency Injection and Spring Beans
Spring Boot is a powerful framework for building RESTful APIs and microservices with minimal configuration. Two fundamental concepts within Spring Boot are Dependency Injection (DI) and Spring Beans. Dependency Injection is a design pattern used to implement Inversion of Control (IoC), allowing the framework to manage object creation and dependenci
6 min read
Difference Between Spring Boot Starter Web and Spring Boot Starter Tomcat
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
Spring Boot – Handling Background Tasks with Spring Boot
Efficiently handling background tasks with Spring Boot is important for providing a smooth user experience and optimizing resource utilization. Background tasks refer to operations that are performed asynchronously or in the background, allowing the main application to continue processing other requests. In a Spring Boot application, background tas
5 min read
Spring Boot – Using Spring Boot with Apache Camel
Apache Camel and Spring Boot are two powerful frameworks that can be seamlessly integrated to build robust, scalable, and efficient applications. Apache Camel is an open-source integration framework that provides an extensive range of components and connectors, enabling developers to integrate different systems, APIs, and applications. It simplifie
5 min read
Spring Boot – Setting Up a Spring Boot Project with Gradle
Spring Boot is a Java framework designed to simplify the development of stand-alone, production-ready Spring applications with minimal setup and configuration. It simplifies the setup and development of new Spring applications, reducing boilerplate code. In this article, we will guide you through setting up a Spring Boot project using Gradle, a wid
4 min read
Migrate Application From Spring Boot 2 to Spring Boot 3
With the release of Spring Boot 3, significant changes and improvements have been introduced, particularly around Jakarta EE compliance, Java 17 support, and GraalVM native images. If you are using Spring Boot 2 and are considering migrating to Spring Boot 3, this article will walk you through the necessary steps, considerations, and potential pitf
5 min read
Spring Boot - Customizing Spring Boot Starter
Spring Boot Starters are specialized project types designed to encapsulate and distribute common functionality, simplifying the setup of Spring Boot applications. Official starters, like spring-boot-starter-web and spring-boot-starter-data-jpa, bundle dependencies, configurations, and pre-built beans for specific use cases. Creating a custom Spring
6 min read
Generate Model Classes using jsonSchema2Pojo (website and Maven Dependency) For a Spring Boot Project That Consumes API
Generating Java model classes from a JSON schema is a common task when building applications that consume APIs, especially in Spring Boot projects. It simplifies the development process by mapping POJOs (Plain Old Java Objects) directly to the API's JSON structure. In this article, we explore how to use jsonSchema2Pojo, both as a web tool and as a
4 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Spring JDBC Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java". Let us see how a pom.xml file of a maven project looks like. C/C++ Code <?xml version="1.0" encoding=
4 min read
Spring vs Spring Boot vs Spring MVC
Are you ready to dive into the exciting world of Java development? Whether you're a seasoned pro or just starting out, this article is your gateway to mastering the top frameworks and technologies in Java development. We'll explore the Spring framework, known for its versatility and lightweight nature, making it perfect for enterprise-level softwar
8 min read
Spring Boot | How to access database using Spring Data JPA
Spring Data JPA is a method to implement JPA repositories to add the data access layer in applications easily. CRUD stands for create, retrieve, update, delete which are the possible operations which can be performed in a database. In this article, we will see an example of how to access data from a database(MySQL for this article) in a spring boot
4 min read
How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA?
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concept in the language to the real world with the help
3 min read
How to Create and Setup Spring Boot Project in Spring Tool Suite?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
How to Run Your First Spring Boot Application in Spring Tool Suite?
Spring Tool Suite (STS) is a java IDE tailored for developing Spring-based enterprise applications. It is easier, faster, and more convenient. And most importantly it is based on Eclipse IDE. STS is free, open-source, and powered by VMware. Spring Tools 4 is the next generation of Spring tooling for the favorite coding environment. Largely rebuilt
3 min read
How to Make a Project Using Spring Boot, MySQL, Spring Data JPA, and Maven?
For the sample project, below mentioned tools got used Java 8Eclipse IDE for developmentHibernate ORM, Spring framework with Spring Data JPAMySQL database, MySQL Connector Java as JDBC driver.Example Project Using Spring Boot, MySQL, Spring Data JPA, and Maven Project Structure: As this is getting prepared as a maven project, all dependencies are s
4 min read
What's New in Spring 6 and Spring Boot 3?
Spring and Spring Boot are two of the most popular Java frameworks used by developers worldwide. The Spring team is continuously working on improving and enhancing the frameworks with each new major release. Spring 6 and Spring Boot 3 are expected to bring in significant new features and changes that will further boost development with these techno
5 min read
Java Spring Boot Microservices - Integration of Eureka, Feign & Spring Cloud Load Balancer
Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities.
13 min read
Spring Cloud Vs Spring Boot Actuator
Spring CloudSpring Cloud's main purpose is to give tools and services for managing and making distributed systems, especially in a microservices architecture. The developer wants to speed up the process of building patterns in distributed systems, these tools can fulfill their requirement. They include almost all from the management of configuratio
5 min read
Spring Boot - Reactive Programming Using Spring Webflux Framework
In this article, we will explore React programming in Spring Boot, Reactive programming is an asynchronous, non-blocking programming paradigm for developing highly responsive applications that react to external stimuli. What is Reactive ProgrammingIn reactive programming, the flow of data is asynchronous through push-based publishers and subscriber
4 min read
Show SQL from Spring Data JPA/Hibernate in Spring Boot
In Spring Boot, Spring Data JPA is part of the larger Spring Data Project that can simplify the development of the data access layers in the spring applications using the Java Persistence API and it can provide a higher-level abstraction over the JPA API. It can reduce the boilerplate code and make it easier to work with the database. Spring Data J
6 min read
Spring Boot - Spring Data JPA
Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any java object directly into a database table. Usually, the name of the object being persisted becomes the name of the table, and each field within tha
6 min read
Properties with Spring and Spring Boot
Java-based applications using the Spring framework and its evolution into the Spring Boot and the properties play a crucial role in configuring the various aspects of the application. Properties can allow the developers to externalize the configuration settings from the code. Understanding how to work with properties in the Spring and Spring Boot i
4 min read
Implementing CORS in Spring Boot with Spring Security
CORS issue is one of the common issues being faced in web development. We are here going to configure CORS in our backend application built using Spring Boot and Spring security, being used for security purpose. Before going to fix the issue, lets understand what CORS is and how it works and why browsers show error while sending CORS request. What
6 min read
Spring Boot 3.0 - JWT Authentication with Spring Security using MySQL Database
In Spring Security 5.7.0, the spring team deprecated the WebSecurityConfigurerAdapter, as they encourage users to move towards a component-based security configuration. Spring Boot 3.0 has come with many changes in Spring Security . In this article, we'll learn how to implement JWT authentication and authorization in a Spring Boot 3.0 application u
9 min read
How to Build a RESTful API with Spring Boot and Spring MVC?
RESTful APIs have become the standard for building scalable and maintainable web services in web development. REST (Representational State Transfer) enables a stateless, client-server architecture where resources are accessed via standard HTTP methods. This article demonstrates how to create a RESTful API using Spring Boot and Spring MVC. We will w
7 min read
Spring Security Integration with Spring Boot
Spring Security is a powerful and customizable authentication and access control framework for Java applications. It provides comprehensive security services for Java EE-based enterprise software applications. This article will integrate Spring Security with a Spring Boot application, covering configuration, authentication, and securing RESTful API
5 min read
Difference between Spring and Spring Boot
Spring Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier compared to classic Java frameworks and Applicati
4 min read
Java Spring Boot Microservices - Client Side Load Balancing with Spring Cloud LoadBalancer
Spring Cloud is a collection of projects like load balancing, service discovery, circuit breakers, routing, micro-proxy, etc will be given by Spring Cloud. So spring Cloud basically provides some of the common tools and techniques and projects to quickly develop some common patterns of the microservices. Basically, there are two ways to load balanc
12 min read
Spring Boot Batch Processing Using Spring Data JPA to CSV File
The Spring Batch is a framework in the Spring Boot ecosystem It can provide a lot of functionalities for Batch processing. The Spring Batch framework simplifies the batch development of applications by providing reliable components and other patterns for common batch processing concerns. Mostly, batch processing is used to read and write data in bu
7 min read
Configuring Multiple Spring Batch Jobs in a Spring Boot Application
Spring Batch serves as a robust framework within the Spring ecosystem, specifically tailored for managing batch processing tasks efficiently. It's designed to tackle big data jobs efficiently and comes with handy tools to make batch application development a breeze. In the context of a Spring Boot application, setting up multiple Spring Batch jobs
10 min read
three90RightbarBannerImg