Gradle use mavenLocal to reuse artifacts in local Maven repository

Gradle and Maven are two different build tools, they have their own local repository, you can use the artifacts from both of them at the same time, but you can not combine them as one single repository. That means you can have both repositories store the same artifact without knowing, and you may do a lot of unnecessary file downloading and waste a lot of time on waiting them.

For example, on my system Gradle and Maven using different local repositories in different folders

 
Gradle: C:\Users\username\.gradle\caches
Maven: C:\Users\username\.m2\repository
 

You may wonder why they can't share the same repository. There are some reasons, the biggest reason is the design goal of Gradle, Gradle can use Maven as its repository manager, but it also has its own repository management system which don't have to rely on any other repository managers. And the local repository of Gradle has its own format and structure which is incompatible with Maven. If you check the folder structure of above folders, you will find they are quite different.

Usually the repository block of the Gradle build script declared as follows

 
repositories {
    mavenCentral()
}
 

What this declaration do is telling the Gradle to download artifacts from remote Maven repository if the artifacts doesn't exist in Gradle's local cache. And when it downloads the artifact, the file will be saved in the cache and will be reused next time. It won't consider the local Maven repository.

For some commonly used artifacts, it very possible that they already exists in Maven's local repository, you will download and save the file twice and waste your disk space.

The better way is first check the local Maven repository and then query the remote one if not found. The configuration like this

 
repositories {
    mavenLocal()
    mavenCentral()
}
 

The mavenLocal tells Gradle to fist looking for artifacts in local Maven repository and then try to fetch it remotely.

For example, I have three dependencies: junit, hamcrest and commons-collections in a project and the commons-collections existed in Maven's local repository. The Gradle Dependencies will contains the following content

 
C:\Users\sam\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar
 
C:\Users\sam\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar
 
C:\Users\samsam\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar
 

You can see the jars comes from different repositories. Gradle use the commons-collections artifact in Maven local repository, no downloading and no two copies of the same file.