Sub topic 7
Where Dependencies Come From: Repositories and the Local Cache
When you add a library such as JUnit to a project, you don't download a JAR file yourself. You write its coordinates (groupId, artifactId, and version), and the build tool finds the file for you. This chapter explains where the build tool looks, what it saves on your computer, and how to control both. The next chapter covers how to declare dependencies and choose their scopes.
What a Repository Is
A repository is a storage place for built Java artifacts. An artifact is a file a build produces, usually a JAR, together with a small description file called a POM. Every artifact in a repository sits at a path built from its coordinates, so a tool can find any library if it knows its groupId, artifactId, and version.
There are two kinds of repository:
- A remote repository is a server on the internet or on your company network. Build tools download artifacts from it over HTTPS.
- The local repository (also called the local cache) is a folder on your own computer. It keeps copies of everything the tool has downloaded, so the tool doesn't need to fetch the same file twice.
Maven Central
Maven Central is the largest public remote repository for Java. Most open-source Java libraries publish their releases there. Its address is https://repo.maven.apache.org/maven2/.
Maven uses Maven Central by default, so you don't have to configure anything to use it. Gradle has no default repository. You turn Maven Central on yourself with mavenCentral(), as the next sections show.
How Coordinates Become a File Path
A repository turns coordinates into a folder path with a fixed rule:
- Replace each dot in the groupId with a slash.
- Add the artifactId as a folder.
- Add the version as a folder.
- Name the file
artifactId-version.jar(andartifactId-version.pom).
For JUnit 5, org.junit.jupiter:junit-jupiter:5.10.2 becomes:
org/junit/jupiter/junit-jupiter/5.10.2/junit-jupiter-5.10.2.jar
org/junit/jupiter/junit-jupiter/5.10.2/junit-jupiter-5.10.2.pomPut the repository address in front of that path and you have the download link. You can open https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter/5.10.2/ in a web browser and see the files.
The Resolution Process
Resolution is the work a build tool does to turn a list of coordinates into real files on disk. For each dependency, it follows these steps:
A checksum is a short fingerprint of a file, stored next to it as a .sha1 or .md5 file. The tool computes the fingerprint of the downloaded file and compares the two. A mismatch means the download is broken or has been tampered with.
This process explains why your first build of a new project is slow and later builds are fast. The first build downloads everything. Later builds read from the cache.
The Maven Local Repository
Maven keeps its local repository in your home folder at ~/.m2/repository (on Windows, C:\Users\YourName\.m2\repository). It uses the same folder layout as Maven Central.
You can watch Maven fill the cache. The dependency:get goal downloads one artifact. It works in any folder, even outside a project:
mvn dependency:get -Dartifact=org.junit.jupiter:junit-jupiter:5.10.2
ls ~/.m2/repository/org/junit/jupiter/junit-jupiter/5.10.2/The listing shows the JAR, the POM, their .sha1 checksum files, and a _remote.repositories file. Maven uses that last file to record which repository each file came from.
Putting your own project in the cache
The local repository also stores your artifacts. Running mvn install builds your project and copies its JAR and POM into ~/.m2/repository. After that, another project on the same computer can depend on com.example:hello:1.0-SNAPSHOT as if it came from Maven Central. This is handy when you work on a library and an app at the same time.
The Gradle Cache
Gradle has its own cache in ~/.gradle/caches/modules-2/files-2.1. It does not use ~/.m2 by default. Its layout is a little different: the groupId keeps its dots, and each file sits inside a folder named after its checksum.
~/.gradle/caches/modules-2/files-2.1/org.junit.jupiter/junit-jupiter/5.10.2/<checksum>/junit-jupiter-5.10.2.jarYou rarely need to look inside it. If Maven and Gradle both build projects on your computer, the same library may be downloaded twice, once into each cache.
Adding Other Repositories
Some libraries are not on Maven Central. Your company may also run its own private repository. You can add repositories to both tools.
In Gradle, the repositories block lists them in order. Gradle checks them from top to bottom:
repositories {
mavenCentral()
maven {
url = uri("https://repo.example.com/maven2")
}
}In Maven, add a <repositories> section inside <project> in pom.xml. Maven still checks Maven Central as well:
<repositories>
<repository>
<id>example-repo</id>
<url>https://repo.example.com/maven2</url>
</repository>
</repositories>The id is a name you choose. Maven uses it to match passwords stored in settings, so keep it stable.
Mirrors
Many companies want every download to go through one internal server. That server is called a mirror or proxy. It fetches from Maven Central once, keeps a copy, and serves it to everyone. In Maven you set this up per computer in ~/.m2/settings.xml, not in the project:
<settings>
<mirrors>
<mirror>
<id>company-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.example.com/repository/maven-public/</url>
</mirror>
</mirrors>
</settings><mirrorOf>*</mirrorOf> means "send requests for every repository to this URL instead."
Releases and SNAPSHOTs in the Cache
The cache treats the two kinds of versions differently:
| Version type | Example | Can it change? | Cache behavior |
|---|---|---|---|
| Release | `5.10.2` | No | Downloaded once, then always read from the cache |
| SNAPSHOT | `1.0-SNAPSHOT` | Yes | Checked again for a newer copy, by default once a day in Maven |
A SNAPSHOT is a version still in development. Its content can change from one build to the next. To force Maven to check for newer snapshots right away, run mvn -U package. Gradle's equivalent is ./gradlew build --refresh-dependencies.
Working Offline
Once the cache holds everything a project needs, you can build without a network:
mvn -o package
./gradlew build --offlineIf a needed file is missing from the cache, the build fails with a clear "not found" or "offline" error and doesn't try the network.
Common Problems
- "Could not find artifact": check the coordinates for typos, then check that the library is in one of your repositories. Searching for it at
https://central.sonatype.comshows whether it is on Maven Central. - Gradle cannot find anything: you probably forgot
mavenCentral()in therepositoriesblock. - A broken or half-downloaded file: delete just that artifact's folder, for example
~/.m2/repository/org/junit/jupiter/junit-jupiter/5.10.2, and build again. Deleting the whole cache also works, but the next build has to download everything again.
Exercise
What is the full path in the Maven local repository of the JAR for the coordinates com.google.guava:guava:33.2.1-jre?
Show answer
~/.m2/repository/com/google/guava/guava/33.2.1-jre/guava-33.2.1-jre.jar. The dots in the groupId become slashes, followed by the artifactId folder, the version folder, and the file named artifactId-version.jar.
Exercise
You build a project with Maven on a train with no internet, and the build succeeds. A teammate tries the same project with Gradle on the same laptop for the first time, and the build fails. Why?
Show answer
Maven and Gradle use separate caches (~/.m2/repository and ~/.gradle/caches). Maven found everything in its own cache, but Gradle's cache was empty and it could not reach a remote repository to download the files.
Exercise
Your build.gradle.kts declares testImplementation("org.junit.jupiter:junit-jupiter:5.10.2") but has no repositories block. What happens, and how do you fix it?
Show answer
Resolution fails because Gradle has no default repository, so it has nowhere to look for JUnit. Add repositories { mavenCentral() } to the build script.