Downloaded path for Gradle comple statement [duplicate] - gradle

How does Gradle store downloaded jar files on the local file system? Maven stores them in the .m2 directory under USER_HOME, but where does Gradle store them? I checked the .gradle folder there, but saw only compiled scripts.

On Mac, Linux and Windows i.e. on all 3 of the major platforms, Gradle stores dependencies at:
~/.gradle/caches/modules-2/files-2.1

Gradle caches artifacts in USER_HOME/.gradle folder. The compiled scripts are usually in the .gradle folder in your project folder.
If you can't find the cache, maybe it's because you have not cached any artifacts yet. You can always see where Gradle has cached artifacts with a simple script:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:12.0'
}
task showMeCache doLast() {
configurations.compileClasspath.each { println it }
}
Now if you run gradle showMeCache it should download the dependencies into the cache and print the full path.

In Windows 10 PC, it is saved at:
C:\Users\%USERNAME%\.gradle\caches\modules-2\files-2.1\

Gradle's local repository folder is:
$USER_HOME/.gradle/caches/modules-2/files-2.1
Defined dependencies will be loaded from remote repositories into gradle's local repository folder. For each loaded file, gradle will be create a new folder named with md5 value of the original file (pom,jar,..). Full path for the dependency file is made up from :
groupid + artifactid + version + FILE_MD5_VALUE + FILE_NAME
If our defined dependency is:
compile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
Then the library will be loaded into :
/$USER_HOME/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jdbc/4.3.4.RELEASE/42175d194cf6aa7c716c0887f30255e5c0a5262c/spring-jdbc-4.3.4.RELEASE.jar

In fact the cache location depends on the GRADLE_USER_HOME environment variable value.
By default, it is $USER_HOME/.gradle on Unix-OS based and %userprofile%.\gradle on Windows.
But if you set this variable, the cache directory would be located from this path.
And whatever the case, you should dig into caches\modules-2\files-2.1 to find the dependencies.

If you want your dependency files to be in some specific folder you can simply use a copy task for it. For Eg.
task copyDepJars(type: Copy) {
from configurations.compile
into 'C:\\Users\\athakur\\Desktop\\lib'
}

I am on windows,
You should be able find the dependencies inside
$USER_HOME.gradle\caches\artifacts-24\filestore

Many answers are correct!
I want to add that you can easily find your download location with
gradle --info build
like described in https://stackoverflow.com/a/54000767/4471199.
New downloaded artifacts will be shown in stdout:
Downloading https://plugins.gradle.org/m2/org/springframework/boot/spring-boot-parent/2.1.7.RELEASE/spring-boot-parent-2.1.7.RELEASE.pom to /tmp/gradle_download551283009937119777bin
In this case, I used the docker image gradle:5.6.2-jdk12.
As you can see, the docker container uses /tmp as download location.

You can use the gradle argument --project-cache-dir "/Users/whatever/.gradle/" to force the gradle cache directory.
In this way you can be darn sure you know what directory is being used (as well as create different caches for different projects)

I just stumbled onto this while searching for this answer. If you are using intellij, you can navigate to the file location, but opening the external lib folder in the project explorer, right clicking on the jar, and select Open Library Settings.

It took me a while to realize this, hence the additional answer. Hopefully it can save folks time. Note that if you are running sudo gradle the dependencies may not be in your home directory, even if sudo echo $HOME returns /Users/<my-non-root-user>/. On my Mac, Gradle was caching the dependencies in /private/var/root/.gradle/caches/.

In case it is an Android gradle project - you can find the android libraries below your $ANDROID_HOME/extras/android/m2repository folder

In android studio do the following steps to check the gradle downloaded jar file.
Set project structure view to "Project"
At bottom External library section available, expand it.
Here you can see downloaded jar files.

On my windows machine with "Buildship 2.0.2" plugin installed in eclipse, dependencies are stored :
$USER_HOME.gradle\caches\modules-2\files-2.1

For my case, I was using an Ivy repository, and my Gradle dependencies were stored in ~/.ivy2/.

Related

Building open source dependencies using gradle

I really don't have much experience in developing let alone using build tools.
I was assigned a task to build dependencies locally and get the jar files.
say I have a list of deps (GAV) like this:-
1. org.jetbrains.kotlin:kotlin-stdlib:1.6.0-RC
2. com.auth0:java-jwt:3.18.2, etc
3. openapi4j:openapi-operation-validator:1.0.7, etc
So i was able to download the source code url from maven repository and source code from github programmatically, for example :-
org.jetbrains.kotlin:kotlin-stdlib:1.6.0-RC - https://github.com/JetBrains/kotlin
com.auth0:java-jwt:3.18.2 - https://github.com/auth0/java-jwt
openapi4j-openapi-operation-validator-1.0.7 https://github.com/openapi4j/openapi4j
but there are many build.gradle files in different directories, how do I know which directory should I move into before running the gradle build command.
Things I have already tried and failed:-
For deps like this openapi4j:openapi-operation-validator:1.0.7, i can directly go into the openapi-operation-validator folder in the Github repo (https://github.com/openapi4j/openapi4j ) and run the gradle build command, but not all projects are structured like that I guess?
For deps like this com.auth0:java-jwt:3.18.2, the artifactId (java-jwt
) is already present in the github path (https://github.com/auth0/java-jwt), so i can run the gradle build command on the root github repo.
From the spring guides , among all the Gradle.build files available I can check which file has:-
jar {
archiveBaseName = <artifactId>
archiveVersion = <version>
}
, then I can move to that dir and run Gradle build, but not all build.gradle files have this.
None of the above approaches are concrete, is there any other firm approach that I can use to tackle the problem?
Your approach is generally correct.
You need to find the source code in github/gitlab/wherever, read the readme file and try to build it with whatever build tool was used there.
This may or may not work.

How Do You Specify Where The Gradle Wrapper Installs Gradle?

I have an off the shelf application that ships a version of gradle with it. It also has scripts that are hard coded to set GRADLE_HOME to this location.
I want to zip up this dir, put it in nexus and replace it with the gradle wrapper.
How do I configure the gradle wrapper to download this zip from nexus and extract it to a specific location in the project?
EDIT: In the gradle-wrapper.properties I have
distributionPath=wrapper/gradle
However, I end up with it being unzipped to
...\wrapper\gradle\gradle-2.3-bin\8gn7esgljqyucijpbynjk93oc\gradle-2.3
How do I get it to unzip to the path I specified and not to the subdirs?
The location to which Gradle gets unpacked is a combination of the distributionBase and distributionPath properties in gradle-wrapper.properties file. The location specified by distributionPath will always be considered as relative to distributionBase. The only available values for distributionPath are GRADLE_USER_HOME and PROJECT. Even when using PROJECT the wrapper will still generate the folder structure you see above.
If you want to control this more precisely I'd suggest not relying on the wrapper to do this and instead add a task to your build specifically for this purpose.

Gradle downloads to ?/.gradle directory when running tasks

Gradle created a ?/.gradle/ in the directory that gradle was run in. We would expect the cache directory to be created at ~/.gradle.
Example:
/project # Project root and cwd when running gradle command
/.gradle # Expected - project-specific gradle folder
/? # Directory literally named with a question mark
/.gradle # Unexpected - Global gradle folder with wrappers and cached artifacts
The user running the scripts did not have a home directory, giving the user a home directory or specifying a gradle-user-home solved the issue:
gradle --gradle-user-home=/foo/bar ...
or
GRADLE_USER_HOME=/foo/bar gradle ...
There are two different folders gradle stores information. ~/.gradle is used to store downloaded artifacts, gradle wrappers, etc. Basically everything that can be shared between multiple builds. The .gradle folder in your project is used to store project specific information used for example by the gradle up-to-date check mechanism.
let's find it out why it behaves like this.
As gradle use following code to get user home:
System.getProperty("user.home");
Follow the link for openjdk 8 source code.
It comes to conclusion: When JVM can not found user name in os, it will use ? as a return. So gradle will create ?/.gradle for usage.

How to use gradle zip in local system without downloading when using gradle-wrapper

I'm trying to build a gradle project with gradle-wrapper (gradlew).
When I build with ./gradlew build, it outputs text
Downloading http://services.gradle.org/distributions/gradle-1.11-bin.zip
And I already got gradle-1.11-bin.zip downloaded separately and I don't want to be downloading it again when I build.
So, where shall I put gradle-1.11-bin.zip in my project or system so that I don't have to download again?
gradle/wrapper/gradle-wrapper.properties is as following.
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-bin.zip
And I've tried copying gradle-1.11-bin.zip into gradle/wrapper/dists which didn't solve the problem.
From gradle-wrapper documentation, I found in section 61.1. Configuration
If you don't want any download to happen when your project is build
via gradlew, simply add the Gradle distribution zip to your version
control at the location specified by your wrapper configuration. A
relative URL is supported - you can specify a distribution file
relative to the location of gradle-wrapper.properties file.
So, I changed distributionUrl property in gradle/wrapper/gradle-wrapper.properties to
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=gradle-1.11-bin.zip
Then, I made a copy of gradle-1.11-bin.zip in gradle/wrapper/.
Then, ./gradlew build downloaded local copy of zip and built the project.
Here's a real-world example:
mkdir -p $HOME/dev
cd $HOME/dev
git clone https://github.com/oss-review-toolkit/ort
cd ort/gradle/wrapper
wget https://services.gradle.org/distributions/gradle-7.5.1-bin.zip
sed -i 's/distributionUrl=.*/distributionUrl=gradle-7.5.1-bin.zip/' gradle-wrapper.properties
cd ../..
./gradlew installDist
Modifty the gradle/gradle-wrapper.properties
Windows:
distributionUrl=file\:/d:/gradle-2.2.1-all.zip
linux:
distributionUrl=file\:/tmp/gradle-2.2.1-all.zip
Just drag downloaded gradle file in your browser address bar and then copy address from address bar and change gradle/wrapper/gradle-wrapper.properties as following:
distributionUrl=ADDRESS THAT COPY FROM YOUR BROWSER
example:
distributionUrl=file:///E:/gradle/gradle-4.1-all.zip
or you can copy gradle.zip file to wrapper folder then use relative path:
distributionUrl=gradle.zip
This is what I did to avoid editing all gradle wrapper for current and future projects.
Locate your gradle-wrapper.properties in your project folder (eg. ./gradle/wrapper/gradle-wrapper.properties)
open the file with a text editor like Sublime Text to locate the distributionUrl line (eg. distributionUrl=http\://services.gradle.org/distributions/gradle-2.10-bin.zip
The gradle binary archive needed is gradle-2.10-bin.zip. Another version is gradle-2.10-all.zip that includes everything including source code and documentation.
Please note each project ships with different versions of gradle distributions and you can change the version to the one you have (gradle-x.xx-XXX.zip) that include the binary. (eg. gradle-2.11-bin.zip | gradle-2.11-all.zip ).
Execute gradlew.bat on window or gradle on linux to start the wrapper to build the project.
It will begin downloading the gradle-2.10-bin.zip to the .gradle\wrapper\dists in your home directory (eg.C:\Users\Sojimaxi\.gradle\wrapper\dists\gradle-2.10-bin). This download happens just once for each specified gradle version.
If you already downloaded the archive before you can terminate the download using Ctrl+C
Go into the gradle download location C:\Users\Sojimaxi\.gradle\wrapper\dists\gradle-2.10-all\78v82fsf226usgvgh7q2ptcvif copy your own copy of gradle-2.10-bin.zip into that directory then delete the gradle-2.10-all.zip.part in that directory.
That's all. Go back to your project directory to execute gradlew.bat and it will use your local copy instead of downloading a new one.
This solution didn't work for me but help me to get the right way so if you want install gradle offline follow these steps:
1- at your project under gradle directory open this file (gradle-wrapper.properties)
2- at last line you will find the gradle version, download that version or copy the file from another pc
distributionUrl=http\://services.gradle.org/distributions/gradle-3.3-bin.zip
Download Link will be like this: http://services.gradle.org/distributions/gradle-3.3-bin.zip
3- open this location
C:\Users\userName.gradle\wrapper\dists\gradle-3.3-all
and ensure that is only one folder (the name doesn’t matter it different at devices) if there any other folders delete all of them and press gradle sync on android studio which will generate another folder automatic with a random name.
4- open that folder and ensure that has only the last two files in the screenshot.
5- move the zip file that you downloaded into that folder
6- press sync gradle on android studio again suppose that gradle will work fine.
I found another easiest way to do this just started my XAMPP server and then made a folder gradle inside htdocs. I added the zipped file gradle-4.4-all.zip inside that folder. Changed the distribution url to distributionUrl=http\://localhost/gradle/gradle-4.4-all.zip
I restarted android and the syncing completed without any issue.
copy your path wher you put the file gradle-5.0-rc-5-bin.zip
example:
path C:/My doc/tools
if you have some spaces in your path change it with %20
file:///C:/My%20doc/tools/gradle-5.0-rc-5-bin.zip
and execute the command:
$ gradlew wrapper --gradle-version 5.0-rc-5
I download to
C:\data\Setup\Development\Gradle 2.11\gradle-2.11-all.zip
inside Eclipse, I declare:
Then I create new Gradle project (with wrapper) very fast, no need download. (Easy more than this solution)
create local server to mock https download(maybe a little complex),but it work
1. install tomcat then unzip and exec E:\apache-tomcat-8.5.4\bin\startup.bat
2.put gradle-2.14.1-all.zip to E:\apache-tomcat-8.5.4\webapps\ROOT\distributions
3.change url like this distributionUrl=http\://localhost:8080/distributions/gradle-2.14.1-all.zip
now run as usual
// Do not use android 8 as it will keep downloading grade distribution use grade 7
ionic cordova platform add android#8.0.0
// This will run
ionic cordova platform add android#~7.1.1 --save
Then run below and this time it should get success( Worked for me)
ionic cordova build android

How can I force gradle to redownload dependencies?

How can I tell gradle to redownload dependencies from repositories?
Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies. You can also delete the cached files under ~/.gradle/caches. With the next build Gradle would attempt to download them again.
What is your specific use case? Do you use dynamic dependency versions or SNAPSHOT versions?
On Unix systems, you can delete all the existing artifacts (artifacts and metadata) Gradle has downloaded using:
rm -rf $HOME/.gradle/caches/
Note that --refresh-dependencies won't always re-download every artifact; it will use existing copies if they match what exists in the repository. From the Gradle User Guide, refreshing dependencies:
The --refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts. A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded. However, where possible Gradle will check if the previously downloaded artifacts are valid before downloading again. This is done by comparing published SHA1 values in the repository with the SHA1 values for existing downloaded artifacts.
[...]
It’s a common misconception to think that using --refresh-dependencies will force download of dependencies. This is not the case: Gradle will only perform what is strictly required to refresh the dynamic dependencies. This may involve downloading new listing or metadata files, or even artifacts, but if nothing changed, the impact is minimal.
If you are using a recent version of Gradle, you can use --refresh-dependencies option.
./gradlew build --refresh-dependencies
you can refer to the Gradle manual.
The --refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts. A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded.
You can tell Gradle to re-download some dependencies in the build script by flagging the dependency as 'changing'. Gradle will then check for updates every 24 hours, but this can be configured using the resolutionStrategy DSL. I find it useful to use this for for SNAPSHOT or NIGHTLY builds.
configurations.all {
// Check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
Expanded:
dependencies {
implementation group: "group", name: "projectA", version: "1.1-SNAPSHOT", changing: true
}
Condensed:
implementation('group:projectA:1.1-SNAPSHOT') { changing = true }
I found this solution at this forum thread.
For MAC
./gradlew build --refresh-dependencies
For Windows
gradlew build --refresh-dependencies
Can also try gradlew assembleDevelopmentDebug --refresh-dependencies
For Windows...in order to make gradle re-download specific dependencies:
delete the dependencies you want to re-download from the directory below:
C:\Users\%USERNAME%\.gradle\caches\modules-2\files-2.1
delete all metadata directories at the path:
C:\Users\%USERNAME%\.gradle\caches\modules-2\metadata-*
run gradle build (or gradlew build if using gradle wrapper) in the project's root directory.
note: the numbers in the file paths above might be different for you.
None of the solutions above worked for me.
If you use IntelliJ, what resolved it for me was simply refreshing all Gradle projects:
One can remove folder with cached jars.
In my case, on Mac the library was cached at path:
/Users/MY_NAME/.gradle/caches/modules-2/files-2.1/cached-library-to-remove
I removed the cached library folder ("cached-library-to-remove" in above example), deleted the build folder of my project and compiled again.
Fresh library was downloaded then.
To refresh cached 'release' version the only option is to clear local cache.
rm -rf $HOME/.gradle/caches/
To refresh cached 'snapshot' version you can:
./gradlew build --refresh-dependencies
For those who are wondering where to run gradle commands:
Open Android Studio
Click on Terminal(You will find it in the base of Android Studio)
The command tool will open
Type your command gradlew build --refresh-dependencies
Instead of removing your entire gradle cache, like some answers here are suggesting, you can delete the cache for a specific group or artifact id. I added the following function to my .bash_profile:
deleteGradleCache() {
local id=$1
if [ -z "$id" ]; then
echo "Please provide an group or artifact id to delete"
return 1
fi
find ~/.gradle/caches/ -type d -name "$id" -prune -exec rm -rf "{}" \; -print
}
Usage:
$ deleteGradleCache com.android.support
Then, on the next build or if you resync, gradle will re-download dependencies.
There is 2 ways to do that:
Using command line option to refresh dependenices cashe.
You can delete local cache where artefasts are caches by Gradle and trigger build
Using --refresh-dependencies option:
./gradlew build --refresh-dependencies
Short explanation --refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts.
Long explanantion
WIth –refresh-dependencies’ Gradle will always hit the remote server to check for updated artifacts: however, Gradle will avoid downloading a file where the same file already exists in the cache.
First Gradle will make a HEAD request and check if the server reports the file as unchanged since last time (if the ‘content-length’ and ‘last-modified’ are unchanged). In this case you’ll get the message: "Cached resource is up-to-date (lastModified: {})."
Next Gradle will determine the remote checksum if possible (either from the HEAD request or by downloading a ‘.sha1’ file)..
If this checksum matches another file already downloaded (from any repository), then Gradle will simply copy the file in the cache,
rather than re-downloading. In this case you’ll get the message: "“Found locally available resource with matching checksum: [{}, {}]”.
Using delete:
When you delete caches
rm -rf $HOME/.gradle/caches/
You just clean all the cached jars and sha1 sums and Gradle is in situation where there is no artifacts on your machine and has to download everything. Yes it will work 100% for the first time, but when another SNAPSHOT is released and it is part of your dependency tree you will be faced again in front of the choice to refresh or to purge the caches.
For Android Studio 3.4.1
Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies".
This should resolve your issue.
Seems change is changed to isChange for gradle version 6.3, kotlin version 1.3.70, Groovy 2.5.10
The working configuration is
implementation("com.sample:commons:1.0.0-SNAPSHOT") {
isChanging = true
}
Also, run this command to fetch the latest
./gradlew assemble --refresh-dependencies
This worked for me.
Make sure Gradle is not set to offline by unchecking button at File>Settings>Gradle>Offline Work.
Add this to the top level of your build.gradle, nice to have above dependencies
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
I made sure my dependencies are written like this:
implementation('com.github.juanmendez:ThatDependency:ThatBranch-SNAPSHOT') {
changing = true
}
Thereafter, I open the Gradle panel in Android Studio and click the blue circle arrows button. I can always see my updates getting a new fresh copy.
If you are using Intellij, you can right click the root project and then select refresh gradle dependencies.
Mb I'm too late however my solution is for single repository. I think deleting ~/.gradle/* is overkill.
The problmem I've bumped into was that I was deleting directory where sources were and gradle was getting another version not from nexus.
To avoid that I run the next:
~/.gradle$ find . -type d -name 'group.plugins.awssdk'
./caches/modules-2/files-2.1/group.plugins.awssdk
./caches/modules-2/metadata-2.23/descriptors/group.plugins.awssdk
~/.gradle$ rm -r ./caches/modules-2/files-2.1/group.plugins.awssdk ./caches/modules-2/metadata-2.23/descriptors/group.plugins.awssdk
After that gradle is dragging files from nexus.
In my case none of the above worked, what I did was:
In build.gradle, commenting the dependencies related to the unresolved imports I had
Clicking "Sync Now"
Uncommenting what I just commented
Clicking "Sync Now" again
Then my imports were properly resolved again.
Deleting all the caches makes download all the dependacies again. so it take so long time and it is boring thing wait again again to re download all the dependancies.
How ever i could be able to resolve this below way.
Just delete groups which need to be refreshed.
Ex : if we want to refresh com.user.test group
rm -fr ~/.gradle/caches/modules-2/files-2.1/com.user.test/
then remove dependency from build.gradle and re add it.
then it will refresh dependencies what we want.
I think gradle 2.14.1 fixes the issue. The accepted answer is correct, but there is a bug in gradle with –refresh-dependencies. 2.14.1 fixes that.
See https://discuss.gradle.org/t/refresh-dependencies-should-use-cachechangingmodulesfor-0s/556
For the majority of cases, just simply re-building the project should do the trick. Sometimes you have to run ./gradlew build --refresh-dependencies as several answers have already mentioned (takes a long time, depending on how much dependencies you have). How ever, sometimes none of those will work: the dependency just won't get updated. Then, you can do this:
Remove dependency from your gradle file
Run / debug your project and wait for it to fail (with NonExistingClass reason)
Hit "build project" and wait for it to finish successfully
Run / debug once again
This is ridiculous and seems like madness, but I actually do use this procedure daily, simply because the dependency I need can be updated dozens of times and none of adequate solutions would have any effect.
If you are using eclipse and if you want force eclipse to re load dependencies you could try below command
gradlew clean cleaneclipse build eclipse --refresh-dependencies
Only a manual deletion of the specific dependency in the cache folder works... an artifactory built by a colleague in enterprise repo.
You can do it like this
https://marschall.github.io/2017/04/17/disabling-gradle-cache.html
To quote from Disabling the Gradle Build Cache
The Gradle build cache may be a great thing when you’re regularly building >large projects with Gradle. However when only occasionally building open source >projects it can quickly become large.
To disable the Gradle build cache add the following line to ~/.gradle/gradle.properties
org.gradle.caching=false
You can clean the existing cache with
rm -rf $HOME/.gradle/caches/
rm -rf $HOME/.gradle/wrapper/
delete this directory:
C:\Users\[username]\.gradle
You need to redownload it, so you can either manually download and replace the corrupted file and again sync your project . Go to this location
C:\users[username].gradle\wrapper\dist\gradle3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9\gradle-3.3-all.zip
Here delete gradle3.3allzip and replace it by downloading again from this site
https://services.gradle.org/distributions/
Find the same file and download and paste it to that location
Then sync your project.
Hope it works for you too.

Resources