DependencySubstitutions

API Documentation:DependencySubstitutions

Note: This class is incubating and may change in a future version of Gradle.

Allows replacing dependencies with other dependencies.

Properties

No properties

Methods

MethodDescription
all(rule)
Incubating

Adds a dependency substitution rule that is triggered for every dependency (including transitive) when the configuration is being resolved. The action receives an instance of DependencySubstitution that can be used to find out what dependency is being resolved and to influence the resolution process.

module(notation)
Incubating

Create a ModuleComponentSelector from the provided input string. Strings must be in the format "{group}:{module}:{version}".

project(path)
Incubating

Create a ProjectComponentSelector from the provided input string. Strings must be in the format ":path".

substitute(substitutedDependency)
Incubating

DSL-friendly mechanism to construct a dependency substitution for dependencies matching the provided selector.

Script blocks

No script blocks

Method details

Note: This method is incubating and may change in a future version of Gradle.

Adds a dependency substitution rule that is triggered for every dependency (including transitive) when the configuration is being resolved. The action receives an instance of DependencySubstitution that can be used to find out what dependency is being resolved and to influence the resolution process.

Example:

configurations { main }
// add dependency substitution rules
configurations.main.resolutionStrategy.dependencySubstitution {
  // Use a rule to change the dependency module while leaving group + version intact
  all { DependencySubstitution dependency ->
    if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.name == 'groovy-all') {
      dependency.useTarget details.requested.group + ':groovy:' + details.requested.version
    }
  }
  // Use a rule to replace all missing projects with module dependencies
  all { DependencySubstitution dependency ->
   if (dependency.requested instanceof ProjectComponentSelector) {
      def targetProject = findProject(":${dependency.requested.path}")
      if (targetProject == null) {
        dependency.useTarget "org.myorg:" + dependency.requested.path + ":+"
      }
    }
  }
}

The rules are evaluated in order they are declared. Rules are evaluated after forced modules are applied (see ResolutionStrategy.force()

ComponentSelector module(String notation)

Note: This method is incubating and may change in a future version of Gradle.

Create a ModuleComponentSelector from the provided input string. Strings must be in the format "{group}:{module}:{version}".

Note: This method is incubating and may change in a future version of Gradle.

Create a ProjectComponentSelector from the provided input string. Strings must be in the format ":path".

Substitution substitute(ComponentSelector substitutedDependency)

Note: This method is incubating and may change in a future version of Gradle.

DSL-friendly mechanism to construct a dependency substitution for dependencies matching the provided selector.

Examples:

configurations { main }
configurations.main.resolutionStrategy.dependencySubstitution {
  // Substitute project and module dependencies
  substitute module('org.gradle:api') with project(':api')
  substitute project(':util') with module('org.gradle:util:3.0')

  // Substitute one module dependency for another
  substitute module('org.gradle:api:2.0') with module('org.gradle:api:2.1')
}