Spring Dependency Management Plugin
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 | plugins { |
or
1 | buildscript { |
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 | dependencyManagement { |
or
1 | dependencyManagement { |
With the configuration above, we can import the dependency without version:
1 | dependencies { |
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 | dependencyManagement { |
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 | dependencyManagement { |
An exclusion can also be declared on an entry in a dependency set, as shown in the following example:
1 | dependencyManagement { |
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 | dependencyManagement { |
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:
- Change the value of a version property
- 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 | dependencyManagement { |
You can also use a map, as shown in the following example:
1 | dependencyManagement { |
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 | ext['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 | dependencyManagement { |
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 | dependencyManagement { |
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 | dependencyManagement { |
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 | dependencyManagement { |
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 | dependencyManagement { |
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/