ResolutionStrategy - Gradle DSL Version 9.3.1

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

Examples:

plugins {
    id 'java' 
}

configurations.all {
  resolutionStrategy {
    
    
    failOnVersionConflict()

    
    preferProjectModules()

    
    
    force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
    
    forcedModules = ['asm:asm-all:3.3.1']

    
    dependencySubstitution {
      substitute module('org.gradle:api') using project(':api')
      substitute project(':util') using module('org.gradle:util:3.0')
    }

    
    cacheDynamicVersionsFor 10*60, 'seconds'
    
    cacheChangingModulesFor 0, 'seconds'
  }
}

Properties

Methods

MethodDescription
activateDependencyLocking()

Activates dependency locking support in Gradle. Once turned on a configuration, resolution result can be saved and then reused for subsequent builds. This enables reproducible builds when using dynamic versions.

cacheChangingModulesFor(value, units)

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

cacheChangingModulesFor(value, units)

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

cacheDynamicVersionsFor(value, units)

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

cacheDynamicVersionsFor(value, units)

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

componentSelection(action)

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

deactivateDependencyLocking()

Deactivates dependency locking support in Gradle.

dependencySubstitution(action)

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.

disableDependencyVerification()

Deactivates dependency verification for this configuration. You should always be careful when disabling verification, and in particular avoid disabling it for verification of plugins, because a plugin could use this to disable verification itself.

eachDependency(rule)

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:

enableDependencyVerification()

Enabled dependency verification for this configuration.

failOnChangingVersions()

If this method is called, Gradle will make sure that no changing version participates in resolution. This can be used in cases you want to make sure your build is reproducible, without relying on dependency locking.

failOnDynamicVersions()

If this method is called, Gradle will make sure that no dynamic version was used in the resulting dependency graph. In practice, it means that if the resolved dependency graph contains a module and that the versions participating in the selection of that module contain at least one dynamic version, then resolution will fail if the resolution result can change because of this version selector. This can be used in cases you want to make sure your build is reproducible, without relying on dependency locking.

failOnNonReproducibleResolution()

Configures Gradle to fail the build is the resolution result is expected to be unstable, that is to say that it includes dynamic versions or changing versions and therefore the result may change depending on when the build is executed. This method is equivalent to calling both ResolutionStrategy.failOnDynamicVersions() and ResolutionStrategy.failOnChangingVersions().

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:

force(notations)

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

getUseGlobalDependencySubstitutionRules()

Gradle implicitly registers dependency substitution rules for all configurations in the whole build tree to find projects in other included builds. These rules are always active by default. There are however cases, where a certain configuration should not apply these rules when resolving. For example, if a binary version of a module should be discovered that is also represented by a project in another build. This property may be used to deactivate these global substitution rules.

preferProjectModules()

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

sortArtifacts(sortOrder)

Specifies the ordering for resolved artifacts. Options are:

Script blocks

No script blocks

Property details

The capabilities resolution strategy.

The currently configured version selection rules object.

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

Method details

Activates dependency locking support in Gradle. Once turned on a configuration, resolution result can be saved and then reused for subsequent builds. This enables reproducible builds when using dynamic versions.

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, 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.

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

Deactivates dependency locking support in 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:

configurations.all {
  resolutionStrategy.dependencySubstitution {
    
    substitute module('org.gradle:api') using project(':api')
    substitute project(':util') using module('org.gradle:util:3.0')

    
    substitute module('org.gradle:api:2.0') using module('org.gradle:api:2.1')
  }
}

Deactivates dependency verification for this configuration. You should always be careful when disabling verification, and in particular avoid disabling it for verification of plugins, because a plugin could use this to disable verification itself.

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 {
  compileClasspath.resolutionStrategy {
    eachDependency { DependencyResolveDetails details ->
      
      if (details.requested.group == 'org.gradle') {
        details.useVersion '1.4'
      }
    }
    eachDependency { details ->
      
      if (details.requested.name == 'groovy-all') {
         
         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[])

Enabled dependency verification for this configuration.

If this method is called, Gradle will make sure that no changing version participates in resolution. This can be used in cases you want to make sure your build is reproducible, without relying on dependency locking.

If this method is called, Gradle will make sure that no dynamic version was used in the resulting dependency graph. In practice, it means that if the resolved dependency graph contains a module and that the versions participating in the selection of that module contain at least one dynamic version, then resolution will fail if the resolution result can change because of this version selector. This can be used in cases you want to make sure your build is reproducible, without relying on dependency locking.

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:

plugins {
    id 'java' 
}

configurations.all {
  resolutionStrategy.failOnVersionConflict()
}

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

It accepts the following notations:

Example:

plugins {
    id 'java' 
}

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

Gradle implicitly registers dependency substitution rules for all configurations in the whole build tree to find projects in other included builds. These rules are always active by default. There are however cases, where a certain configuration should not apply these rules when resolving. For example, if a binary version of a module should be discovered that is also represented by a project in another build. This property may be used to deactivate these global substitution rules.

void preferProjectModules()

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:

plugins {
    id 'java' 
}

configurations.all {
  resolutionStrategy.preferProjectModules()
}

void sortArtifacts(SortOrder sortOrder)

Specifies the ordering for resolved artifacts. Options are:

  • <UNHANDLED-LINK>SortOrder#DEFAULT</UNHANDLED-LINK> : Don't specify the sort order. Gradle will provide artifacts in the default order.
  • <UNHANDLED-LINK>SortOrder#CONSUMER_FIRST</UNHANDLED-LINK> : Artifacts for a consuming component should appear before artifacts for its dependencies.
  • <UNHANDLED-LINK>SortOrder#DEPENDENCY_FIRST</UNHANDLED-LINK> : Artifacts for a consuming component should appear after artifacts for its dependencies.

A best attempt will be made to sort artifacts according the supplied SortOrder, but no guarantees will be made in the presence of dependency cycles. NOTE: For a particular Gradle version, artifact ordering will be consistent. Multiple resolves for the same inputs will result in the same outputs in the same order.