Gradle "changing" dependencies management - gradle

I want to share my problem with you. Maybe somebody faced this problem also and will have solution.
In brief Gradle doesn't resolve frequently changing dependencies.
We're using:
./gradlew -v
------------------------------------------------------------
Gradle 2.12
------------------------------------------------------------
Build time: 2016-03-14 08:32:03 UTC
Build number: none
Revision: b29fbb64ad6b068cb3f05f7e40dc670472129bc0
Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_66 (Oracle Corporation 25.66-b17)
OS: Linux 2.6.18-409.el5 amd64
Let me try to explain what's happened.
We have some project that have dependency of another independent project.
Both are under active development.
One is: string-parser version: 1.0.0-SNAPSHOT
Second is: tools-utils version: 2.2.0-SNAPSHOT
We have internal maven artifactory and we configured it in string-parser:
# some code there
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, TimeUnit.MILLISECONDS
}
repositories {
mavenLocal()
maven { url 'https://some.internal.com/deploy-snapshot'}
maven { url 'https://some.internal.com/deploy-release'}
}
dependencies {
# Some other dependencies listed here
compile('com.some.group:tools-utils:2.2.0-SNAPSHOT') {
changing = true
}
}
So when we're doing changes at local work station (Windows - I think it doesn't matter) for tools-utils and upload last snapshot artifact version to maven local and remote everything is okay. We go to string-parser project press "reimport" button (in Intellij Idea 2016.1.2) and Gradle switch to correct dependency version.
But if somebody does some changes and upload new version to Maven remote it won't update dependency in cache and still point to old version. To fix it we have manually delete artifact from Gradle cache and (!) from Maven Local.
Could you please advice me something because cleaning up cache manually (or with addition step on TeamCity) is a nightmare?

Try putting this in allprojects
// forces all changing dependencies (i.e. SNAPSHOTs) to automagicially download
// (thanks, #BillBarnhill!)
configurations.all {
resolutionStrategy {
cacheChangingModulesFor 0, 'seconds'
}
}
Source: Gradle-Fury

Related

Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.2.71'] was not found in any of the following sources

I have a fresh install of IntelliJ, I created a new kotlin gradle project using the following settings:
This produces the following build.gradle.kts, (the exact same file works on my Windows machine):
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.2.71"
}
group = "com.test"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Which produces this error, when trying to do a gradle refresh:
Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.2.71'] was not
found in any of the following sources:
Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.2.71')
Searched in the following repositories:
Gradle Central Plugin Repository
Check your Internet connection and make sure your Internet is not restricted.
I solved this problem by turning on proxy for all tunnels (not just HTTP) with a VPN app.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// kotlin("jvm") version "1.2.71"
}
group = "com.test"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
}
//tasks.withType<KotlinCompile> {
// kotlinOptions.jvmTarget = "1.8"
//}
gradle sync by commenting the above lines. The gradle will be set up.
once the gradle is downloaded, uncomment those line and sync again.
if the dependencies are not downloaded properly, run 'gradle build' in the terminal and click on gradle sync.
This solved the issue for me.
(1) in my case (OpenJDK 11 on Ubuntu 18.04) the problem was Gradle not being able to download the POM file from gradle plugin-server. you can test it by entering this line into jshell:
new java.net.URL("https://plugins.gradle.org/m2/org/jetbrains/kotlin/jvm/org.jetbrains.kotlin.jvm.gradle.plugin/1.3.11/org.jetbrains.kotlin.jvm.gradle.plugin-1.3.11.pom").openStream()
(you can find your url by running gradle with --debug option)
So if you received an exception like this: InvalidAlgorithmParameterException: trustAnchors parameter must be non-empty then the trouble is CA-certs cache. which could be easily fixed by writing these lines into bash Ref:
sudo su
/usr/bin/printf '\xfe\xed\xfe\xed\x00\x00\x00\x02\x00\x00\x00\x00\xe2\x68\x6e\x45\xfb\x43\xdf\xa4\xd9\x92\xdd\x41\xce\xb6\xb2\x1c\x63\x30\xd7\x92' > /etc/ssl/certs/java/cacerts
/var/lib/dpkg/info/ca-certificates-java.postinst configure
By the way do not forget to restart gradle daemon before trying again. (gradle --stop)
(2) another reason could be your internet not having access to bintray.com (the internet of Iran or China) which you can test by putting this line on jshell :
new java.net.URL("https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin-api/1.3.11/kotlin-gradle-plugin-api-1.3.11.pom").openStream()
If you received a connection timeout, it confirms this theory. In this case you need to buy and have proxy/vpn connected in order to be able to download these dependencies.
Check your gradle and kotlin (or Java) versions.
I got the same error and my issue is solved by specifying the kotlin version in build.gradle:
Before:
plugins {
id 'org.jetbrains.kotlin.jvm'
}
After:
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.4.10"
}
In my case (Ubuntu 20.04), problem was with gradle 7.2, installed from snap.
I have removed gradle 7.2, installed from snap and install gradle 7.2 from sdkman. Works fine for me.
If you are using java like me .I got the issue fixed by adding the following:
Root gradle
dependencies {
ext.kotlin_version = '1.4.10'
classpath "com.android.tools.build:gradle:7.0.4"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
......
}
App gradle file
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"
......
}
Ok, so the answer was very simple all along. For some reason I activated gradle's "Offline work" toggle and that was the cause of the problem.
To disable it simply go to Settings > Build, Execution, Deployment > Build Tools > Gradle and deselect the "Offline work" checkbox.
In my case the problem was because Charles Proxy. After closing Charles I could start working again
I updated my Kotlin version to 1.7.20 and fixed this problem.
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
I recently had similar issue with an empty project autogenerated by Intellij Idea.
Solved this problem by combining Java and Gradle versions.
Initially I had Oracle Java 8 with Gradle 6.8.3.
After several attempts I found a working combination - AdoptOpenJDK 11 and Gradle 5.6.4
In my case I changes the Gradle JVM in Settings > Build, Execution, Deployment > Build Tools > Gradle and it worked.
Disconnect from your VPN (or make sure you have an open internet connection), then restart Android Studio.
If you don't restart it sometimes it continues with invalid proxy properties.
This for Ktor devs. If you are working on a ktor application with the web project generator there is a chance the generator sets invalid jvm plugin version. So make sure you are setting correct version for jvm plugin. You can find the latest jvm plugin version here. Here is the sample build.gradle.kts file.
//Plugin section
plugins {
kotlin("jvm") version "1.8.0"
id("io.ktor.plugin") version "2.2.2"
}
//Dependancy section
dependencies {
...
testImplementation("io.ktor:ktor-server-tests-jvm:1.8.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.0")
...
}

gradle how to avoid latest version being pulled from artifactory

My gradle dependency tree shows as below
+--- org.apache.lucene:lucene-core:3.6.2 -> 4.5.1
Since there are few changes in 4.5.1 which are not backward compatible , I want to force version 3.6.2 to be picked up.
how to stop gradle from pulling the latest version from the artifactory ?
You can use gradle's resolution strategy to override declared dependency versions:
configurations.all {
resolutionStrategy {
force 'org.apache.lucene:lucene-core:3.6.2'
}
}
Further Reading

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 - why is this a dependency conflict or how to solve it?

Gradle reports a dependency conflict while I thought that I can resolve conflicts by forcing a particular version. Can someone please shed a light on this and how to force a particular version in any case?
This is the basic build script. It should work out of the box.
apply plugin: 'java'
ext {
version_spring = "4.0.4.RELEASE"
version_jbehave = "3.9.2"
}
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy {
failOnVersionConflict() ;; (1)
//
// The idea is to force a particular version of Spring
//
force "org:springframework:spring-core:${version_spring}"
force "org.springframework:spring-test:${version_spring}"
}
}
dependencies {
// Transitivily depending on org.springframework:spring-test:3.1.1.RELEASE
// Conflict is not resolved according to Gradle (see below)
compile "org.jbehave:jbehave-spring:${version_jbehave}"
}
Essentially I'm calling just "gradle dependencies". However, I'm throwing in various options to ensure that I'm not tricked by any cache.
$ gradle --no-daemon --cache rebuild --recompile-scripts \\
--refresh-dependencies --rerun-tasks dependencies
:dependencies
[..]
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':dependencies'.
> Could not resolve all dependencies for configuration ':compile'.
> A conflict was found between the following modules:
- org.springframework:spring-core:3.1.1.RELEASE
- org.springframework:spring-core:4.0.4.RELEASE
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
[..]
I'm using the latest version of Gradle:
$ gradle -v
------------------------------------------------------------
Gradle 1.12
------------------------------------------------------------
Build time: 2014-04-29 09:24:31 UTC
Build number: none
Revision: a831fa866d46cbee94e61a09af15f9dd95987421
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
Ivy: 2.2.0
JVM: 1.8.0 (Oracle Corporation 25.0-b70)
OS: Mac OS X 10.8.5 x86_64
There is a typo in the build script - org:springframework should be org.springframework. Fixing the typo should solve the problem.
Have you tried again with Java 1.8.0_05 rather than using that old beta? Just curious.
Also, what do you get when you try to use Spring 3.2.8 instead of that 4.0.4 version?

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