7

I am having problems with resolutionStrategy.cacheChangingModulesFor.

My project build.gradle looks similar to this

apply plugin: 'base'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply from: "gradle/mixins/cachestrategy.gradle"
configurations.all {
  resolutionStrategy.cacheDynamicVersionsFor 5, 'minutes'
  resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

buildscript {
  repositories {
    maven {
      url artifactoryUrl
    }
  }
  dependencies {
    classpath (group: 'com.myorg', name: 'aCustomPlugin', version: '1.5.0-SNAPSHOT') {
      changing = true
    }
  }
}

allprojects {
  apply plugin: 'base'
  apply plugin: 'com.myorg.aCustomPlugin'
}

my question is: How can i specify the cacheResolutionStrategy for the SNAPSHOT version in my buildscript block?

t0r0X
  • 4,212
  • 1
  • 38
  • 34
wrossmck
  • 369
  • 8
  • 21

1 Answers1

12

specifying it outside the block, doesn't work (since the buildscript block is evaluated first, in order to build the scripts... ) so the cache strategy rules defined in the scripts haven't been evaluated yet.

the resolution strategy should be placed in the buildscript block like this

buildscript {
  repositories {
    mavenLocal()
    maven {
      url artifactoryUrl
    }
  }
  dependencies {
    classpath (group: 'com.myorg', name: 'aCustomPlugin', version: '1.5.0-SNAPSHOT') {
      changing = true
    }
  }
  configurations.all {
    resolutionStrategy.cacheDynamicVersionsFor 5, 'minutes'
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
  }
}
wrossmck
  • 369
  • 8
  • 21
  • 2
    Btw, SNAPSHOT versions are treated as "changing" by default (if Maven repo is used). Therefore it is not required to set `changing = true` explicitly. – Marcin Zajączkowski Oct 05 '16 at 14:56
  • Pretty sure that was there due to a bug in the version of gradle we were using at the time. Now, it's _not_ needed! :) – wrossmck Oct 05 '16 at 14:59
  • 2
    can you explain the difference between cacheDynamicVersionsFor and cacheChangingModulesFor ? – jatin Goyal Jun 10 '20 at 04:38
  • 1
    For posterity, "changing modules" refers to SNAPSHOT versions and any dependency explicitly marked as changing. An example of a dynamic version would be 2.+ which would resolve to the newest 2.x version available. – Blake M. Jan 27 '22 at 22:55