Gradle Goodness: Use bill of materials (BOM) As Dependency Constraints
Since Gradle 5 we can easily use a bill of materials (BOM) in our build file to get recommended dependency versions. The dependency versions defined in the BOM are dependency constraints in Gradle. This means the dependencies we define in our build that are part of the BOM don’t need a version, because the version is resolved via the dependency constraint that is defined in the BOM. Also transitive dependency versions are resolved using the BOM if applicable. We use the dependency handler method platform
to define the BOM we want to import. The versions in the BOM are recommendations. We can override the recommendation by specifying the version for a dependency found in the BOM with an explicit version.
In the following example build file we import the BOM for Spring Boot 1.2.4.RELEASE. We also add two dependencies that are defined in the BOM: commons-codec:commons-codec
and org.yaml:snakeyaml
. One without a version and one with an explicit version to override the version defined in the BOM:
// File: build.gradle.kts
plugins {
java
}
repositories {
jcenter()
}
dependencies {
// Load bill of materials (BOM) for Spring Boot.
// The dependencies in the BOM will be
// dependency constraints in our build.
implementation(platform("org.springframework.boot:spring-boot-dependencies:2.1.4.RELEASE"))
// Use dependency defined in BOM.
// Version is not needed, because the version
// defined in the BOM is a dependency constraint
// that is used.
implementation("commons-codec:commons-codec")
// Override version for dependency in the BOM.
// Version in BOM is 1.23.
implementation(group = "org.yaml",
name = "snakeyaml",
version = "1.24")
}
When we run the dependencies
task for the configuration compileClasspath
we can see how the dependencies are resolved. Notice that version 1.24
for snakeyaml
is used, while the version in the BOM is 1.23
:
$ gradle -q dependencies --configuration compileClasspath
------------------------------------------------------------
Root project
------------------------------------------------------------
compileClasspath - Compile classpath for source set 'main'.
+--- org.springframework.boot:spring-boot-dependencies:2.1.4.RELEASE
| +--- commons-codec:commons-codec:1.11 (c)
| \--- org.yaml:snakeyaml:1.23 -> 1.24 (c)
+--- commons-codec:commons-codec -> 1.11
\--- org.yaml:snakeyaml:1.24
(c) - dependency constraint
A web-based, searchable dependency report is available by adding the --scan option.
$
The dependency handler method enforcedPlatform
is also available. When we use this method to import a BOM in our build the versions of dependencies we use are forced for the dependencies we use. Even if we define an explicit version on a dependency found in the BOM, the version is forced that is described in the BOM.
// File: build.gradle.kts
plugins {
java
}
repositories {
jcenter()
}
dependencies {
// Load bill of materials (BOM) for Spring Boot.
// The dependencies in the BOM will be
// dependency constraints in our build, but
// the versions in the BOM are forced for
// used dependencies.
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:2.1.4.RELEASE"))
// Use dependency defined in BOM.
// Version is not needed, because the version
// defined in the BOM is a dependency constraint
// that is used.
implementation("commons-codec:commons-codec")
// Version in BOM is 1.23 and because
// we use enforcedPlatform the version
// will be 1.23 once the dependency is resolved,
// even though we define a newer version explicitly.
implementation(group = "org.yaml",
name = "snakeyaml",
version = "1.24")
}
Now we look at the resolved dependencies and see how the snakeyaml
dependency is forced to use version 1.23
:
$ gradle -q dependencies --configuration compileClasspath
------------------------------------------------------------
Root project
------------------------------------------------------------
compileClasspath - Compile classpath for source set 'main'.
+--- org.springframework.boot:spring-boot-dependencies:2.1.4.RELEASE
| +--- commons-codec:commons-codec:1.11 (c)
| \--- org.yaml:snakeyaml:1.23 (c)
+--- commons-codec:commons-codec -> 1.11
\--- org.yaml:snakeyaml:1.24 -> 1.23
(c) - dependency constraint
A web-based, searchable dependency report is available by adding the --scan option.
$
Read more about the Gradle BOM support on the Gradle website.
Written with Gradle 5.4.