Dependency Management in Spring Boot

The heart of Spring-Boot, is the 'Dependency Management' feature.

"Dependency is nothing but a library that provides specific support to the application to run and implement". We can manage these dependencies at one place in our application called pom.xml or build.gradle file.

It totally depends on which build tool you are using in your application. Every release of the spring boot comes up with new dependencies, also spring boot manages the updating of dependencies if we want to update the spring boot version.

Importance of Dependency Management :

Dependency Management is just a way of managing all the required dependencies in one place and efficiently making use of them. It allows specifying all required dependencies according to the respective Spring-Boot version in one place itself. If you change the 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 Dependencies 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.

  • For the dependency to work, We just 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 )

Project Build Systems :-

There are two main build tools - Maven and Gradle.

  • Maven and Gradle use a different syntax for managing dependencies.
  • 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.
  • Maven manages the dependencies in the 'pom.xml' file, while Gradle manages them in the 'build.gradle' file.

Maven Dependency:

  • For Maven configuration, we should inherit the Spring Boot Starter parent project to manage the Spring Boot Starters dependencies.
  • For this, simply we can inherit the starter parent in our pom.xml file as shown below.
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>1.5.8.RELEASE</version>
		</parent>
												

We should specify the version number for Spring Boot Parent Starter dependency. Then for other starter dependencies, we do not need to specify the Spring Boot version number.

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

Gradle Dependency:

We do not need Spring Boot start Parent dependency like Maven for Gradle. We can import the Spring Boot Starters dependencies directly into build.gradle file. Observe the code given below −

	buildscript {
		ext {
			springBootVersion = '1.5.8.RELEASE'
		}
		repositories {
			mavenCentral()
		}
		dependencies {
			classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		}
	}
                                            	

Also in Gradle, we need not specify the Spring Boot version number for dependencies. Spring Boot automatically configures the dependency based on the version.

		dependencies {
			compile('org.springframework.boot:spring-boot-starter-web')
		}
                                            

Spring-Boot Starters :

  • Spring-Boot Starters plays an important role in dependency management.
  • They are a set of convenient dependency descriptors.
  • You can get access to all Spring and related tech stacks that you require for the project by using spring-boot-starter.
  • 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.
  • For Example: 'spring-boot-starter-jdbc'

Types of Starters:

  • Application Starters.
  • Technical Starters.
  • 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>