Spring Dependency Management Plugin

Author Avatar
Young Hug Apr 26, 2020

This doc is about the Spring Dependency Management Plugin.

Spring Dependency Management Plugin

Based on the configured dependency management metadata, the Dependency Management Plugin will control the versions of your project’s direct and transitive dependencies and will honour any exclusions declared in the poms of your project’s dependencies.

Before we go

Import the plugin in your build.gradle or pom.xml.

1
2
3
plugins {
id "io.spring.dependency-management" version "1.0.6.RELEASE"
}

or

1
2
3
4
5
6
7
8
9
10
buildscript {
repositories {
maven { url 'https://repo.spring.io/plugins-snapshot' }
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:1.0.7.BUILD-SNAPSHOT'
}
}

apply plugin: "io.spring.dependency-management"

How to use

There are some ways to use.

DSL

The DSL allows you to declare dependency management using a : separated string to configure the coordinates of the managed dependency, as shown in the following example:

1
2
3
4
5
dependencyManagement {
dependencies {
dependency 'org.springframework:spring-core:4.0.3.RELEASE'
}
}

or

1
2
3
4
5
dependencyManagement {
dependencies {
dependency group:'org.springframework', name:'spring-core', version:'4.0.3.RELEASE'
}
}

With the configuration above, we can import the dependency without version:

1
2
3
dependencies {
compile 'org.springframework:spring-core'
}

Dependency Sets

When you want to provide dependency management for multiple modules with the same group and version you should use a dependency set. Using a dependency set removes the need to specify the same group and version multiple times, as shown in the following example:

1
2
3
4
5
6
7
8
dependencyManagement {
dependencies {
dependencySet(group:'org.slf4j', version: '1.7.7') {
entry 'slf4j-api'
entry 'slf4j-simple'
}
}
}

Exclusions

You can also use the DSL to declare exclusions. The two main advantages of using this mechanism are that they will be included in the `` of your project’s generated pom and that they will be applied using Maven’s exclusion semantics.

1
2
3
4
5
6
7
dependencyManagement {
dependencies {
dependency('org.springframework:spring-core:4.0.3.RELEASE') {
exclude 'commons-logging:commons-logging'
}
}
}

An exclusion can also be declared on an entry in a dependency set, as shown in the following example:

1
2
3
4
5
6
7
8
9
dependencyManagement {
dependencies {
dependencySet(group:'org.springframework', version: '4.1.4.RELEASE') {
entry('spring-core') {
exclude group: 'commons-logging', name: 'commons-logging'
}
}
}
}

Importing a Maven Bom

The plugin also allows you to import an existing Maven bom to utilise its dependency management, as shown in the following example:

1
2
3
4
5
6
7
8
9
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:1.0.1.RELEASE'
}
}

dependencies {
compile 'org.springframework.integration:spring-integration-core'
}

Importing Multiple Boms

If multiple boms provide dependency management for the same dependency, the dependency management from the last bom will be used.

Overriding Versions in a Bom

Overriding a particular managed version. There are two ways to do this:

  1. Change the value of a version property
  2. Override the dependency management
Change the value of a version property

A property can be overridden as part of importing a bom, as shown in the following example:

1
2
3
4
5
6
7
dependencyManagement {
imports {
mavenBom('io.spring.platform:platform-bom:1.0.1.RELEASE') {
bomProperty 'spring.version', '4.0.4.RELEASE'
}
}
}

You can also use a map, as shown in the following example:

1
2
3
4
5
6
7
8
9
dependencyManagement {
imports {
mavenBom('io.spring.platform:platform-bom:1.0.1.RELEASE') {
bomProperties([
'spring.version': '4.0.4.RELEASE'
])
}
}
}

Alternatively, the property can also be overridden using a project’s properties configured via any of the mechanisms that Gradle provides. You may choose to configure it in your build.gradle script, as shown in the following example:

1
2
3
ext['spring.version'] = '4.0.4.RELEASE'
#Or
spring.version=4.0.4.RELEASE
Override the dependency management

f the bom that you have imported does not use properties, or you want the override to be honoured in the Maven pom that’s generated for your Gradle project, you should use dependency management to perform the override.

1
2
3
4
5
6
7
8
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE'
}
dependencies {
dependency 'com.google.guava:guava:18.0'
}
}

If you do not want a project’s dependencies to override its dependency management, this behavior can be disabled using overriddenByDependencies, as shown in the following example:

1
2
3
dependencyManagement {
overriddenByDependencies = false
}
Configuring the Dependency Management Resolution Strategy

The plugin uses separate, detached configurations for its internal dependency resolution. You can configure the resolution strategy for these configurations using a closure. If you’re using a snapshot, you may want to disable the caching of an imported bom by configuring Gradle to cache changing modules for zero seconds, as shown in the following example:

1
2
3
4
5
dependencyManagement {
resolutionStrategy {
cacheChangingModulesFor 0, 'seconds'
}
}

Dependency Management for Specific Configurations

To target dependency management at a single configuration, you nest the dependency management within a block named after the configuration, such as compile as shown in the following example:

1
2
3
4
5
6
7
8
9
10
dependencyManagement {
compile {
dependencies {
// …
}
imports {
// …
}
}
}

To target dependency management at multiple configurations, you use configurations to list the configurations to which the dependency management should be applied, as shown in the following example:

1
2
3
4
5
6
7
8
9
10
dependencyManagement {
configurations(compile, custom) {
dependencies {

}
imports {

}
}
}

Accessing Properties from Imported Boms

The plugin makes all of the properties from imported boms available for use in your Gradle build. Properties from both global dependency management and configuration-specific dependency management can be accessed. A property named spring.version from global dependency management can be accessed as shown in the following example:

1
dependencyManagement.importedProperties['spring.version']

The same property from the compile configuration’s dependency management can be accessed as shown in the following example:

1
dependencyManagement.compile.importedProperties['spring.version']

Pom generation

Gradle’s maven and maven-publish plugins automatically generate a pom file that describes the published artifact. The plugin will automatically include any global dependency management.

This blog is under a CC BY-NC-SA 3.0 Unported License
Link to this article: https://younggod.netlify.app/2020/04/25/tech/gradle/SpringDependencyManagementPlugin/