Groovy Documentation

org.gradle.api.artifacts
[Java] Interface ResolutionStrategy


public interface ResolutionStrategy

Defines the strategies around dependency resolution. For example, forcing certain dependency versions, 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()

     // 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 a dependency resolve rule
     eachDependency { DependencyResolveDetails details ->
       //specifying a fixed version for all libraries with 'org.gradle' group
       if (details.requested.group == 'org.gradle') {
           details.useVersion'1.4'
       }
       //changing 'groovy-all' into 'groovy':
       if (details.requested.name == 'groovy-all') {
          details.useTarget group: details.requested.group, name: 'groovy', version: details.requested.version
       }
     }

     // cache dynamic versions for 10 minutes
     cacheDynamicVersionsFor 10*60, 'seconds'
     // don't cache changing modules at all
     cacheChangingModulesFor 0, 'seconds'
   }
 }
 
Since:
1.0-milestone-6


Method Summary
void cacheChangingModulesFor(int value, String units)

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

void cacheChangingModulesFor(int value, TimeUnit units)

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

void cacheDynamicVersionsFor(int value, String units)

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

void cacheDynamicVersionsFor(int value, TimeUnit units)

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

ResolutionStrategy eachDependency(Action rule)

Adds a dependency resolve rule that is triggered for every dependency (including transitive) when the configuration is being resolved.

ResolutionStrategy failOnVersionConflict()

In case of conflict, Gradle by default uses the newest of conflicting versions.

ResolutionStrategy force(Object... moduleVersionSelectorNotations)

Allows forcing certain versions of dependencies, including transitive dependencies.

Set getForcedModules()

Returns currently configured forced modules.

ResolutionStrategy setForcedModules(Object... moduleVersionSelectorNotations)

Allows forcing certain versions of dependencies, including transitive dependencies.

 

Method Detail

cacheChangingModulesFor

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

Parameters:
value - The number of time units
units - The units
Since:
1.0-milestone-6


cacheChangingModulesFor

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

Parameters:
value - The number of time units
units - The units
Since:
1.0-milestone-6


cacheDynamicVersionsFor

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

Parameters:
value - The number of time units
units - The units
Since:
1.0-milestone-6


cacheDynamicVersionsFor

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

Parameters:
value - The number of time units
units - The units
Since:
1.0-milestone-6


eachDependency

@Incubating
public ResolutionStrategy eachDependency(Action rule)
Adds a dependency resolve 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:
 apply plugin: 'java' //so that there are some configurations

 configurations.all {
   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 force(Object...)
Returns:
this
Since:
1.4


failOnVersionConflict

public 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()
 }
 
Returns:
this resolution strategy instance
Since:
1.0-milestone-6


force

public 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:

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'
 }
 
Parameters:
moduleVersionSelectorNotations - typically group:name:version notations to append
Returns:
this ResolutionStrategy instance
Since:
1.0-milestone-7


getForcedModules

public Set getForcedModules()
Returns currently configured forced modules. For more information on forcing versions see force(Object...)
Returns:
forced modules
Since:
1.0-milestone-7


setForcedModules

public ResolutionStrategy setForcedModules(Object... moduleVersionSelectorNotations)
Allows forcing certain versions of dependencies, including transitive dependencies. Replaces existing forced modules with the input.

For information on notations see force(Object...)

Example:

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

 configurations.all {
   resolutionStrategy.forcedModules = ['asm:asm-all:3.3.1', 'commons-io:commons-io:1.4']
 }
 
Parameters:
moduleVersionSelectorNotations - typically group:name:version notations to set
Returns:
this ResolutionStrategy instance
Since:
1.0-milestone-7


 

Gradle API 1.12