gradle DLS学习之ResolutionStrategy

Defines the strategies around dependency resolution. For example, forcing certain dependency versions, substitutions, conflict resolutions or snapshot timeouts.

Examples:

apply plugin: 'java' //so that there are some configurations

configurations.all {
  resolutionStrategy {
    // fail eagerly on version conflict (includes transitive dependencies)
    // e.g. multiple different versions of the same dependency (group and name are equal)
    failOnVersionConflict()

    // prefer modules that are part of this build (multi-project or composite build) over external modules
    preferProjectModules()

    // force certain versions of dependencies (including transitive) // *append new forced modules: force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4' // *replace existing forced modules with new ones: forcedModules = ['asm:asm-all:3.3.1'] // add dependency substitution rules dependencySubstitution { substitute module('org.gradle:api') with project(':api') substitute project(':util') with module('org.gradle:util:3.0') } // cache dynamic versions for 10 minutes cacheDynamicVersionsFor 10*60, 'seconds' // don't cache changing modules at all cacheChangingModulesFor 0, 'seconds' } }


Property details

ComponentSelectionRules componentSelection (read-only)

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

The currently configured version selection rules object.

DependencySubstitutions dependencySubstitution (read-only)

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

The set of dependency substitution rules that are set for this configuration.

Set<ModuleVersionSelector> forcedModules

Returns currently configured forced modules. For more information on forcing versions see ResolutionStrategy.force(java.lang.Object[])

Method details

void cacheChangingModulesFor(int value, String units)

Sets the length of time that changing modules will be cached, with units expressed as a String.

A convenience method for ResolutionStrategy.cacheChangingModulesFor(int, java.util.concurrent.TimeUnit)with units expressed as a String. Units are resolved by calling the valueOf(String) method of TimeUnit with the upper-cased string value.

void cacheChangingModulesFor(int value, TimeUnit units)

Sets the length of time that changing modules will be cached.

Gradle caches the contents and artifacts of changing modules. By default, these cached values are kept for 24 hours, after which the cached entry is expired and the module is resolved again.

Use this method to provide a custom expiry time after which the cached entries for any changing module will be expired.

void cacheDynamicVersionsFor(int value, String units)

Sets the length of time that dynamic versions will be cached, with units expressed as a String.

A convenience method for ResolutionStrategy.cacheDynamicVersionsFor(int, java.util.concurrent.TimeUnit)with units expressed as a String. Units are resolved by calling the valueOf(String) method of TimeUnit with the upper-cased string value.

void cacheDynamicVersionsFor(int value, TimeUnit units)

Sets the length of time that dynamic versions will be cached.

Gradle keeps a cache of dynamic version => resolved version (ie 2.+ => 2.3). By default, these cached values are kept for 24 hours, after which the cached entry is expired and the dynamic version is resolved again.

Use this method to provide a custom expiry time after which the cached value for any dynamic version will be expired.

ResolutionStrategy componentSelection(Action<? super ComponentSelectionRules> action)

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

The componentSelection block provides rules to filter or blacklist certain components from appearing in the resolution result.

ResolutionStrategy dependencySubstitution(Action<? super DependencySubstitutions> action)

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

Configures the set of dependency substitution rules for this configuration. The action receives an instance of DependencySubstitutions which can then be configured with substitution rules.

Examples:

// add dependency substitution rules
configurations.all {
  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') } } 
ResolutionStrategy eachDependency(Action<? super DependencyResolveDetails> rule)

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 DependencyResolveDetails that can be used to find out what dependency is being resolved and to influence the resolution process. Example:

configurations {
  compile.resolutionStrategy {
    eachDependency { DependencyResolveDetails details ->
      //specifying a fixed version for all libraries with 'org.gradle' group
      if (details.requested.group == 'org.gradle') {
        details.useVersion '1.4'
      }
    }
    eachDependency { details ->
      //multiple actions can be specified
      if (details.requested.name == 'groovy-all') { //changing the name: details.useTarget group: details.requested.group, name: 'groovy', version: details.requested.version } } } } 

The rules are evaluated in order they are declared. Rules are evaluated after forced modules are applied (see ResolutionStrategy.force(java.lang.Object[])

ResolutionStrategy failOnVersionConflict()

In case of conflict, Gradle by default uses the newest of conflicting versions. However, you can change this behavior. Use this method to configure the resolution to fail eagerly on any version conflict, e.g. multiple different versions of the same dependency (group and name are equal) in the same Configuration. The check includes both first level and transitive dependencies. See example below:

apply plugin: 'java' //so that there are some configurations

configurations.all {
  resolutionStrategy.failOnVersionConflict()
}
ResolutionStrategy force(Object... moduleVersionSelectorNotations)

Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to be considered when resolving dependencies.

It accepts following notations:

  • String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
  • instance of ModuleVersionSelector
  • any collection or array of above will be automatically flattened

Example:

apply plugin: 'java' //so that there are some configurations

configurations.all {
  resolutionStrategy.force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
}
void preferProjectModules()

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

Gradle can resolve conflicts purely by version number or prioritize project dependencies over binary. The default is by version number.

This applies to both first level and transitive dependencies. See example below:

apply plugin: 'java' //so that there are some configurations

configurations.all {
  resolutionStrategy.preferProjectModules()
}

转载于:https://www.cnblogs.com/wzbinStu/p/8488840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值