Where Does Gradle Save Dependencies' Jars

Gradle store on local file system

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.

Where does gradle save dependencies' jars?

BY DEFAULT

Linux:

~/.gradle/caches/modules-2/files-2.1

Windows:

%USERPROFILE%/.gradle/caches/modules-2/files-2.1

Mac:

~/.gradle/caches/modules-2/files-2.1

Android:

no idea... can anyone edit?

SETTING TO SOMETHING ELSE

But you can override these default settings by setting GRADLE_USER_HOME. The latter environment variable, if set, is NOT the same as GRADLE_HOME, which is the path to the directory containing the bin/ directory for the Gradle version your system is using at the command prompt (NB instead of invoking Gradle directly at the command line most people seem to use the Gradle wrapper however: $ ./gradlew build).

RUNNING GRADLE IN AN IDE

IDEs may be using a different version and be storing dependency jars somewhere else. I only know Eclipse. Having installed Buildship ("Eclipse Plug-ins for Gradle") the place to go is Window --> Preferences --> Gradle. There is a box here for you to specify "Gradle User Home". Mine is currently empty. I presume it is using the system GRADLE_USER_HOME (which I have indeed set)... but I haven't bothered checking: although it is possible to run Gradle tasks from within Eclipse to me it is more handy to run Gradle commands in a separate Terminal (*nix)/Command Prompt window ('Doze).

Gradle: Where are external dependencies stored?

~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver/3.2.2/<checksum of the JAR>/mongodb-driver-3.2.2.jar

Where does gradle stores the jars of external plugins?

You will find the plugin .jar file inside the ~/.gradle/caches/modules-2/files-2.1/ folder.

Saving Gradle Dependencies to a Directory

Firstly, please consider using Gradle (or Gradle Wrapper) to do such things like getting/downloading dependencies of a project on another computer. But if you need to copy dependencies for any other reason you can define a task similar to:

task copyDependencies(type: Copy) {
from configurations.runtime
into "lib"
}

When you run:

gradle copyDependencies

runtime dependencies will be copied to a lib/ folder.

Example

build.gradle

apply plugin: 'groovy'

repositories {
mavenCentral()
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.1'
compile group: 'com.h2database', name: 'h2', version: '1.4.196'

testCompile group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4'

}
task copyDependencies(type: Copy) {
from configurations.runtime
into "lib"
}

Command:

gradle copyDependencies

And lib/ directory contains:

lib
├── groovy-all-2.3.11.jar
├── h2-1.4.196.jar
└── jackson-core-2.9.1.jar

Use Gradle Wrapper

As I mentioned earlier, please consider using Gradle Wrapper so you don't have to worry about if there is a Gradle distribution installed on another computer. As stated in the documentation you can easily add Gradle Wrapper and then you can run

./gradlew [task]

by using wrapper instead of Gradle installed on your OS. In your case running

./gradlew build 

will download all dependencies and build the project. It's way better than copying dependencies manually, Gradle was invented to do it for us.

When does gradle store in .m2 and when in cache?

Gradle will read from your local maven repository only when you declare it as a valid repository:

repositories {
mavenLocal()
}

Gradle will write to your local maven repository only when you publish artifacts and tell it to publish to the local maven repo.

  • If you are using the maven plugin, when executing the task install
  • If you are using the maven-publish plugin, when executing the task publishToMavenLocal

Gradle will use its own internal cache for all resolved dependencies, including ones coming from the local maven repository.

For example, if you use a dependency org:foo:1.0 from your maven local repository, the metadata and artifact will be copied to the Gradle cache on first resolution. From then on, the dependency will be resolved from the Gradle cache.

However, if the dependency is changing, such as when using a -SNAPSHOT version, the Gradle cache will by default keep the last one resolved for 24h. After which it will perform a new resolution, hitting again the local maven repository in this example.

See the documentation for controlling that cache duration for dynamic and/or changing dependencies.

How to copy a gradle dependency jar from gradle's cache?

I didn't declare the dependency with the implementation configuration but with a custom one, which I named toCopy and then I added a task in order to copy only the specific jar in a fixed location:

configurations {
toCopy
implementation {
extendsFrom toCopy
}
}

dependencies {
toCopy 'mysql:mysql-connector-java'
}

task copyToLib(type: Copy) {
from configurations.toCopy
into 'lib'
rename '(.*)', 'mysql-connector-java.jar'
}

Then I can call gradle copyToLib and the jar is being copied to the lib folder.



Related Topics



Leave a reply



Submit