4

I have a project with 3 common jars shared across multiple repositories. I'm using Intellij 2019.4. In my gradle build, I included the following method:

dependencyManagement {
    resolutionStrategy {
        // don't cache SNAPSHOT modules; always retrieve them from the maven cache
        cacheChangingModulesFor 0, 'seconds'
    }
}

That's supposed to tell Gradle not to cache snapshots, but mine are still being cached. I build and install one of the common jars, and the maven repo has it, but the Gradle cache still has a jar from over 24 hours ago. Is there a different method I should be using? I want Gradle to ALWAYS use what's in the .m2 repo.

CNDyson
  • 1,687
  • 7
  • 28
  • 63
  • Are you sure the outdated jars are actually being used in the build? I don't think Gradle uses the build cache when sourcing artifacts from local repositories. See https://docs.gradle.org/current/userguide/declaring_repositories.html#sec:case-for-maven-local – Spaceman1701 Nov 19 '19 at 18:27
  • Yes, I'm sure. The changes I make to updated jars aren't being reflected. When I navigate to the source code, it takes me to the jar in the Gradle cache. – CNDyson Nov 19 '19 at 20:30
  • I had the [same problem](https://stackoverflow.com/questions/57900891/gradle-resolution-strategy-cachedynamicversionsfor-isnt-working) And after I've discovered, it looks like gradle has some kind of delay ~1-5min, even though you set up resolutionStrategy 0 seconds – Max Nov 28 '19 at 01:59

2 Answers2

2

Gradle will only search for modules in the declared repositories.

This means that if you need a library, SNAPSHOT or not, from you local Maven repository, you need to declare it as a repository in Gradle.

repositories {
    mavenLocal() {
        content {
            includeModule("my.org", "someLib")
        }
    }
    // other repositories
}

However, there are caveats in adding mavenLocal() to your repositories, so make sure to use repository content filtering to only search your local Maven repo for those SNAPSHOT dependencies, as shown above.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • Perhaps I should have posted my whole build file. I already have `mavenLocal()` declared. Is there anything else I should look for? – CNDyson Nov 21 '19 at 15:07
  • 1
    Please do add your repository declaration to your question then, and maybe clarify the version of the library you are trying to get. The most likely explanation is ordering as I showed, but there could be something else. – Louis Jacomet Nov 25 '19 at 16:23
1

Try to add changing = true into individual SNAPSHOT dependencies' configClosure.

implementation("group:module:1.0-SNAPSHOT") {changing = true}

Then cacheChangingModulesFor should apply to them:

configurations.all() {
    resolutionStrategy {
        cacheChangingModulesFor 0, "seconds"
    }
}

With version latest.integration, this would require the build-number added into the version - but, this would keep the build reproducible, since one can switch back to the previous build of the library.

There also is a CLI option --refresh-dependencies, which refreshes them all.

The Gradle manual explains it, too: Declaring a changing version.

Forcibly deleting them before build would be another option.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216