Advantages of Dependency Management
- It provides the centralization of dependency information by specifying the Spring Boot version in one place. It helps when we switch from one version to another.
- It avoids the mismatch of different versions of Spring Boot libraries.
- We only need to write a library name specifying the version. It is helpful in multi-module projects.
Note: Spring Boot also allows overriding of dependencies version, if required.
Working on Dependency Management in Spring-Boot
In Spring Boot, dependency management refers to the process of managing external dependencies, such as libraries and frameworks, that are required for building and running a Spring Boot application. Spring Boot simplifies dependency management by providing a powerful and intuitive mechanism for declaring dependencies and managing their versions.
To work on dependency management in Spring Boot:
- Declare Dependencies: You specify the dependencies your application needs in the pom.xml file (for Maven) or build.gradle file (for Gradle). Spring Boot starters, which are curated sets of dependencies for common use cases, can be used to quickly add dependencies for specific features, such as web development, data access, security, and more.
- Version Management: Spring Boot manages dependency versions through its Bill of Materials (BOM). The BOM defines a set of compatible versions for Spring Boot and its dependencies, ensuring that all dependencies work well together. This helps avoid version conflicts and compatibility issues.
- Dependency Injection: Spring Boot uses dependency injection to manage the dependencies of your application's components. This allows you to decouple your code and make it easier to maintain and test. Dependencies can be injected into beans using annotations such as @Autowired or constructor injection.
- External Configuration: Spring Boot provides mechanisms for externalizing configuration, allowing you to configure dependencies and other application settings externally. This makes it easier to manage configuration across different environments and reduces the need for hardcoding configuration values.
Project Build Systems
Project build systems are tools used to automate the process of building, testing, and packaging software projects. They manage dependencies, compile source code, run tests, and produce executable artifacts such as JAR files or WAR files. Common project build systems include Maven, Gradle, and Ant.
- Maven: Maven is a popular build automation tool primarily used for Java projects. It uses XML-based configuration files (pom.xml) to define project structure, dependencies, and build lifecycle phases. Maven resolves dependencies from remote repositories and automatically downloads required libraries.
- Gradle: Gradle is a modern build automation tool that offers more flexibility and extensibility compared to Maven. It uses a Groovy-based DSL (Domain-Specific Language) or Kotlin scripts to define project configuration and tasks. Gradle uses a dependency resolution mechanism similar to Maven but provides better performance and incremental builds.
- Ant: Ant is an older build automation tool commonly used for Java projects before Maven and Gradle gained popularity. It uses XML-based build files (build.xml) to define tasks, dependencies, and build targets. Ant provides a flexible and customizable build process but lacks the declarative model and dependency management features of Maven and Gradle.
Maven Dependency Management System
The Maven project inherits the following features from spring-boot-starter-parent:
- The default Java compiler version
- UTF-8 source encoding
- It inherits a Dependency Section from the spring-boot-dependency-pom. It manages the version of common dependencies. It ignores the <version> tag for that dependencies.
- Dependencies inherited from the spring-boot-dependencies POM
- Sensible resource filtering
- Sensible plugin configuration
Changing the Java Version
We can also change the Java version by using the <java.version> tag.
<properties>
<java.version>1.8</java.version>
</properties>
Inheriting Starter Parent
The following spring-boot-starter-parent inherits automatically when we configure the project.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version> <!-- lookup parent from repository -->
<relativePath/>
</parent>
Note: In the above dependency, we have specified only the Spring Boot version. If we want to add additional starters, simply remove the <version> tag. Similarly, we can also override the individual dependency by overriding a property in our project.
For example, if we want to add another dependency with the same artefact that we have injected already, inject that dependency again inside the <properties> tag to override the previous one.
Adding Spring Boot Maven Plugin
We can also add the Maven plugin to our pom.xml file. It wraps the project into an executable jar file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Spring Boot without Parent POM
If we don't want to use spring-boot-starter-parent dependency, but still want to take the advantage of the dependency management, we can use the <scope> tag, as follows:
Note: It does not maintain plugin management.
<dependencyManagement>
<dependencies>
<dependency><!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The above dependency does not allow overriding. To achieve the overriding, we need to add an entry inside the <dependencyManagement> tag of our project before the spring-boot-dependencies entry.
For example, to upgrade another spring-data-release train, add the following dependency in the pom.xml file.
<dependencyManagement>
<dependencies>
<!--Override Spring Data release train-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Related Article Spring Tool Suite
Spring-Boot Starters
Spring Boot starters are a set of curated dependencies that simplify the process of configuring and bootstrapping Spring Boot applications. They provide a convenient way to add common features and functionality to your Spring Boot projects without needing to manually manage dependencies and configurations. Starters encapsulate groups of related dependencies, making it easy to get started with various aspects of Spring Boot development, such as web applications, data access, security, messaging, and more.
Each Spring Boot starter includes:
- Core Dependencies: The core dependencies required for a specific feature or functionality.
- Auto-Configuration: Pre-configured settings and configurations that are automatically applied when the starter is included in a Spring Boot project.
- Transitive Dependencies: Additional dependencies that are necessary for the starter to function properly.
Types of Starters
- Core Starters: Core starters provide essential functionality for building Spring Boot applications. Examples include spring-boot-starter, spring-boot-starter-web, and spring-boot-starter-test, which include dependencies for Spring Boot itself, web development, and testing, respectively.
- Data Starters: Data starters provide dependencies for working with different data storage technologies, such as databases (e.g., spring-boot-starter-data-jpa, spring-boot-starter-data-mongodb), caching (e.g., spring-boot-starter-data-redis), and messaging (e.g., spring-boot-starter-data-amqp).
- Security Starters: Security starters include dependencies and configurations for implementing security features in Spring Boot applications, such as authentication, authorization, and encryption. Examples include spring-boot-starter-security and spring-boot-starter-oauth2-client.
- Integration Starters: Integration starters provide dependencies for integrating Spring Boot applications with external systems and services. Examples include spring-boot-starter-integration for Spring Integration and spring-cloud-starter-stream-* for event-driven microservices.
- Testing Starters: Testing starters include dependencies and tools for testing Spring Boot applications. Examples include spring-boot-starter-test for unit and integration testing and spring-cloud-starter-contract-stub-runner for contract testing with Spring Cloud Contract.
Frequently Asked Questions
What is Spring Dependency Management?
Spring Boot dependency management is about managing dependencies and configuring them automatically. Each release of Spring Boot provides a list of dependencies that it supports.
Which dependency is used for Spring Boot?
The spring-boot-starter dependency is the core starter for Spring Boot applications.
What is dependency management in Maven?
Dependency management in Maven refers to the process of specifying and managing dependencies for a project. In Maven, dependencies are defined in the project's pom.xml file using <dependency> elements.
What is Spring dependency management plugin?
The Spring Dependency Management Plugin is a Maven plugin provided by the Spring Framework project to simplify dependency management for Spring-based projects. It allows developers to define and manage versions of Spring dependencies in a central location, reducing the need for manual version management in individual project pom.xml files.
What are the types of Dependency Injection in Spring Boot?
There are several types of Dependency Injection supported by Spring Boot, constructor injection, setter injection, field injection, and method injection.
Conclusion
In this article, we learnt about Spring Boot’s ability to manage dependencies. We also learned about the advantages of dependency management in Spring boot. We learnt about Maven dependency management. Also check how to carry out the STS Download.
Happy Learning!