Use Extra Properties to Consolidate Dependency Versions in Your build.gradle File

Published: Jan 24, 2021
Updated: May 3, 2021

When defining dependency versions in your build.gradle file, if dependencies share the same version, you have to repeat it each time.

For example:

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-actuator:2.4.2'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.4.2'
	implementation 'org.springframework.boot:spring-boot-starter-web:2.4.2'
}

You can consolidate this by defining an extra property (in gradle-speak). Note: double quotes are required when using groovy’s string interpolation.

For example:

ext {
  springVersion = '2.4.2'
}

dependencies {
	implementation "org.springframework.boot:spring-boot-starter-actuator:${springVersion}"
	implementation "org.springframework.boot:spring-boot-starter-data-jpa:${springVersion}"
	implementation "org.springframework.boot:spring-boot-starter-web:${springVersion}"
}