Gradle not picking up latest library/version from artifactory - gradle

I have project A and B. Project B depends on A.
If I build a snapshot for B, it will trigger a snapshot build on A. The issue is that when the latest snapshot of A gets pushed, B doesn't pick up the latest snapshot. Rather, it picks up the one before that.
Is there any reason why? And can I pull the latest snapshot every time ?

If your dependencies do not have the "-SNAPSHOT", you can specifically tell gradle that the dependency is a changing one like this:
dependencies {
compile group: "group", name: "artifact", version: "1.0", changing: true
}
However even changing dependencies are cached for 24 hours by default. To make sure you will always get the latest one, use:
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

Related

Gradle changes selected version to snapshot

I am facing a confusing issue with gradle. I have my dependencies declared where I have a jar from my private repository. Recently I released a new version of my private lib, and when I try to import the dependency gradle automatically changes it to the SNAPSHOT version.
implementation group: "com.company.name", name: "my-library", version: "0.11.0"
and when I run the dependency tree
com.company.name:my-library:0.11.0 -> 0.11.0-SNAPSHOT
I tried forcing the version like this
implementation (group: "com.company.name", name: "my-library") {
version {
strictly '0.11.0'
}
}
and still the result was
com.company.name:my-library:{strictly 0.11.0} -> 0.11.0-SNAPSHOT
I cleaned all the caches and refreshed the dependencies, I still get the same thing.
The version was set in the dependencies.lock

Having trouble with Gradle dependencies not updating

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

Solution around gradle cache issues with snapshots?

When publishing snapshots to artifactory/mavenLocal, projects won't pick up the latest snapshot. This requires deleting the jar from ~/.gradle/cache
Maven has a feature to set timestamps for snapshots. how would this work with gradle cache?
There are two things to consider in resolving your issue:
How Gradle handles/recognizes snapshots
How to override Gradle's default behavior
Some background on how Gradle recognizes/handles snapshots
By default, Gradle will refresh a snapshot dependency every 24 hours.
Gradle will automatically recognize a dependency as a snapshot if the version ends with the -SNAPSHOT suffix. For example:
dependencies {
compile group: "aGroup", name: "anArtifact", version: "1.0-SNAPSHOT"
}
However, if the dependency's version string does not end with -SNAPSHOT Gradle needs to be told it's a snapshot with the changing parameter. For example:
dependencies {
compile group: "aGroup", name: "anArtifact", version: "1.0", changing: true
}
Overriding how often Gradle downloads snapshots
The only mechanism for overriding the default 24 hour policy is to configure Gradle to invalidate dependency cache (and thus download a new SNAPSHOT) more frequently. For example:
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
Dynamically versioned dependency cache will need to be configured separately
If you're using any dynamic versions, such as:
dependencies {
compile group: "aGroup", name: "anArtifact", version: "1.+", changing: true
}
You'll need to configure cache invalidation for those dependencies separately, like this:
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
}
Build performance impact
One thing to note is that the shorter the period of time a dependency is cached the more frequently Gradle will retrieve that artifact. If caching is disabled altogether, it will grab the dependency during each execution.

How to let gradle always download the latest version of a dependency?

We have a custom gradle plugin, which is applied to all the projects we have. Since the plugin is released every several days, I don't want to update all the codebase to change it to use the latest version of the plugin.
How to declare it in gradle to ask it always get the latest version of the dependency?
I tried:
dependencies {
classpath "com:my-plugin:[1.0.0,)"
}
or
dependencies {
classpath "com:my-plugin:+"
}
They can get the latest version the first time, but won't get the newer one again.
as a default, once gradle resolved a dynamic dependency, gradle won't check for newer versions for 24h. you have different options to influence this. one option is to run your build with --refresh-dependencies or you customize the TTL in your build script. E.g:
configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
}
The following script should do the job:
apply plugin: 'java'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.inject:guice:latest.release'
}
}
Check out the docs here.
Another option is to go for snapshot publishing and configure dependency resolver to check seconds if the library changes so often.

Gradle compile time dependency version meaning of "+" in relation to remote repositories and caching

When using a "+" at the end of a compile time dependency version number in Gradle, what is the exact meaning of the "+" in relation to remote repositories and caching?
Specifically, the "+" signifies a dependency version of that number or greater. However, if I have a local version cached in Gradle and a version with a higher version number in a remote repository, which version will be chosen?
repositories {
maven { url 'www.example.com' }
}
dependencies {
compile 'com.example.test:1.0.+'
}
Gradle will cache the resolved version for a while (24 hours by default), then resolve again. Here is how you would reconfigure the timeout for all configurations in a project:
configurations.all.resolutionStrategy.cacheDynamicVersionsFor(1, "hours")
For details, see the Gradle Build Language Reference.

Resources