DependencySubstitutions

允许将依赖项替换为其他依赖项。

属性

无属性

方法

方法描述
all(rule)

添加一个依赖替换规则,该规则在解析配置时对每个依赖项(包括传递性依赖项)触发。该 action 接收 DependencySubstitution 的实例,可用于查找正在解析哪个依赖项并影响解析过程。

module(notation)

从提供的输入字符串创建一个 ModuleComponentSelector。字符串必须采用 "{group}:{module}:{version}" 格式。

project(path)

从提供的输入字符串创建一个 ProjectComponentSelector。字符串必须采用 ":path" 格式。

substitute(substitutedDependency)

一种 DSL 友好的机制,用于为与提供的选择器匹配的依赖项构建依赖替换。

脚本块

无脚本块

方法详情

添加一个依赖替换规则,该规则在解析配置时对每个依赖项(包括传递性依赖项)触发。该 action 接收 DependencySubstitution 的实例,可用于查找正在解析哪个依赖项并影响解析过程。

示例

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.module == 'groovy-all') {
      dependency.useTarget dependency.requested.group + ':groovy:' + dependency.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 + ":+"
      }
    }
  }
}

规则按照声明的顺序进行评估。规则在应用强制模块后进行评估(参见 ResolutionStrategy.force(java.lang.Object[]))。

ComponentSelector module(String notation)

从提供的输入字符串创建一个 ModuleComponentSelector。字符串必须采用 "{group}:{module}:{version}" 格式。

从提供的输入字符串创建一个 ProjectComponentSelector。字符串必须采用 ":path" 格式。

Substitution substitute(ComponentSelector substitutedDependency)

一种 DSL 友好的机制,用于为与提供的选择器匹配的依赖项构建依赖替换。

示例

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

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