Adding dependencies and the local repository
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Almost every app needs libraries, such as a JDBC driver or a test framework. Each one must be found, downloaded and put only where it belongs: JUnit should never end up inside the app you ship.
Finding a library on Maven Central and copying its dependency block
A dependency is a library your project uses, named in pom.xml by its coordinates. Maven downloads it, and the libraries it needs, for you.
Search the library on central.sonatype.com, open it, and copy the ready dependency block from its Maven tab into <dependencies>. Then mvn dependency:list shows every JAR Maven resolved.
Example 1 · A dependency block from Maven Central, and dependency:list
Runs in: the receipt-app Maven project
<!-- copied from the Maven tab of the commons-text page on central.sonatype.com --><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version></dependency>$ mvn dependency:list [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- dependency:3.7.0:list (default-cli) @ receipt-app --- [INFO] The following files have been resolved: [INFO] org.apache.commons:commons-text:jar:1.15.0:compile -- module org.apache.commons.text [INFO] org.apache.commons:commons-lang3:jar:3.20.0:compile -- module org.apache.commons.lang3 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.305 s [INFO] Finished at: 2026-10-16T08:33:49+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- You add one block;
dependency:listshows two resolved JARs, because Commons Text brings Commons Lang.:compileat the end is the scope, the default. - The "-- module ..." part names the Java module inside each JAR and can be ignored for now.
Maven Central is the public store Maven downloads from. Pick a stable version: plain numbers such as 1.15.0, not ones ending in -alpha, -beta, -M1 or -RC1. Check the groupId too, because a few libraries have look-alike copies under other groups. After saving the pom, run mvn compile and reload the project in the IDE.
Where downloads go: the local repository ~/.m2/repository
The local repository is the folder .m2/repository in your home folder, where Maven keeps every JAR it has downloaded. Every project on your computer shares it.
The folders follow the coordinates, so you can find any library yourself. Look inside the folder of Commons Text.
Example 2 · What a library looks like in the local repository
Runs in: Git Bash or macOS Terminal
ls ~/.m2/repository/org/apache/commons/commons-text/1.15.0$ ls ~/.m2/repository/org/apache/commons/commons-text/1.15.0 _remote.repositories commons-text-1.15.0.jar commons-text-1.15.0.jar.sha1 commons-text-1.15.0.pom commons-text-1.15.0.pom.sha1
What this code does
- The path is the coordinates:
org/apache/commons(groupId),commons-text(artifactId),1.15.0(version). - The
.sha1files are checksums Maven uses to check the download;_remote.repositoriesrecords where the files came from.
Maven checks the local repository first and goes to Maven Central only for what is missing, so Commons Text is downloaded once, not once per project. Next to each JAR sits its .pom, which tells Maven what the library needs. The repository is a cache: you never edit or commit it, and deleting one version folder makes Maven download it again on the next build.
The folder is C:\Users\<you>\.m2\repository on Windows and /Users/<you>/.m2/repository (the same as ~/.m2/repository) on macOS.
Scopes: compile, runtime, test and provided
A scope says on which classpaths a dependency appears: compiling the app, running the tests, running the app, and whether it travels with the app. Leaving <scope> out gives compile.
The pom below has three libraries with three scopes: Commons Text (default), the MySQL driver (runtime) and JUnit (test). Two compile errors then show the scopes at work.
Example 3 · pom.xml with compile, runtime and test scopes
Runs in: the receipt-app Maven project
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.college</groupId> <artifactId>receipt-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>17</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>26.7.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.14.4</version> <scope>test</scope> </dependency> </dependencies></project>(No output by itself. Example 5 shows the tree Maven builds from these three dependencies.)
What this code does
- Commons Text has no
<scope>, so it iscompile. The MySQL driver isruntimeand JUnit istest. junit-jupiteris JUnit's all-in-one artifact: the test API plus the engine that runs the tests.
Example 4 · Scopes as safety rails: two compile errors
Runs in: a terminal in the receipt-app Maven project
mvn -q compile1) src/main/java/com/college/app/QuickCheck.java uses JUnit's Assertions: $ mvn -q compile [ERROR] /C:/Users/asha/projects/receipt-app/src/main/java/com/college/app/QuickCheck.java:[3,29] package org.junit.jupiter.api does not exist [ERROR] /C:/Users/asha/projects/receipt-app/src/main/java/com/college/app/QuickCheck.java:[7,9] cannot find symbol 2) src/main/java/com/college/db/DriverInfo.java does new com.mysql.cj.jdbc.Driver(): $ mvn -q compile [ERROR] /C:/Users/asha/projects/receipt-app/src/main/java/com/college/db/DriverInfo.java:[3,25] package com.mysql.cj.jdbc does not exist [ERROR] /C:/Users/asha/projects/receipt-app/src/main/java/com/college/db/DriverInfo.java:[7,9] cannot find symbol [ERROR] /C:/Users/asha/projects/receipt-app/src/main/java/com/college/db/DriverInfo.java:[7,29] cannot find symbol
What this code does
- JUnit has
testscope, sosrc/main/javacannot see it: "package org.junit.jupiter.api does not exist". Test code belongs insrc/test/java. - The driver has
runtimescope, so its classes are hidden from the compiler. Your JDBC code should use onlyjava.sqltypes; the next module shows the driver being found at run time.
compile suits libraries your code calls directly, such as Commons Text. test is for test-only libraries such as JUnit. runtime is for libraries needed only when the program runs, such as the MySQL driver: JDBC code compiles against the java.sql interfaces in the JDK, and the driver is found at run time. provided means the place where the app runs already has the library, such as the servlet API inside a Tomcat server.
The narrow scopes are safety rails. Test code cannot leak into the app, and app code cannot tie itself to one database vendor's classes by accident; both mistakes fail at compile time, as Example 4 shows.
Transitive dependencies and mvn dependency:tree
A transitive dependency is a library pulled in by one of your dependencies. It takes its scope from its parent: everything under JUnit is test, everything under the MySQL driver is runtime.
mvn dependency:tree prints every dependency as a tree. Your three declared dependencies sit at the first level, and everything below them was pulled in without being named in your pom.
Example 5 · mvn dependency:tree: three declared, twelve JARs
Runs in: a terminal in the receipt-app Maven project
mvn dependency:tree$ mvn dependency:tree [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- dependency:3.7.0:tree (default-cli) @ receipt-app --- [INFO] com.college:receipt-app:jar:1.0-SNAPSHOT [INFO] +- org.apache.commons:commons-text:jar:1.15.0:compile [INFO] | \- org.apache.commons:commons-lang3:jar:3.20.0:compile [INFO] +- com.mysql:mysql-connector-j:jar:26.7.0:runtime [INFO] | \- com.google.protobuf:protobuf-java:jar:4.31.1:runtime [INFO] \- org.junit.jupiter:junit-jupiter:jar:5.14.4:test [INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.14.4:test [INFO] | +- org.opentest4j:opentest4j:jar:1.3.0:test [INFO] | +- org.junit.platform:junit-platform-commons:jar:1.14.4:test [INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.2:test [INFO] +- org.junit.jupiter:junit-jupiter-params:jar:5.14.4:test [INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.14.4:test [INFO] \- org.junit.platform:junit-platform-engine:jar:1.14.4:test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.578 s [INFO] Finished at: 2026-10-16T10:17:43+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- Lines starting with
+-or\-directly under the project are the three dependencies from the pom; deeper lines are transitive ones, with the scope inherited from their parent. protobuf-javaarrives with the MySQL driver and JUnit brings eight JARs in total. None of them is in your pom.
Each line shows groupId:artifactId:type:version:scope, so three lines in your pom become a dozen JARs, each on exactly the right classpath.
When two libraries need different versions of the same JAR, Maven keeps only one: the version nearest to your project in the tree, and a version you declare yourself is always nearest. This rule is nearest wins, and the tree shows which version won and who brought it in.
Predict the output
Read the scope in the tree
A transitive dependency takes its scope from its parent. Pick the scope.
Round 1 of 2
Which scope does protobuf-java get?
Errors: Could not resolve dependencies, typos and offline builds
-U makes Maven check the remote repositories again for downloads that failed or changed, and -o builds offline, using only the local repository.
"Could not resolve dependencies" means Maven could not find a JAR you asked for. The first run below has a typo, the second run repeats it, and the third builds offline.
Example 6 · Could not resolve dependencies: a typo, a cached failure, offline mode
Runs in: a terminal in the receipt-app Maven project
mvn compilemvn compilemvn -o compilepom.xml says <artifactId>commons-txt</artifactId> $ mvn compile [WARNING] The POM for org.apache.commons:commons-txt:jar:1.15.0 is missing, no dependency information available [ERROR] Failed to execute goal on project receipt-app: Could not resolve dependencies for project com.college:receipt-app:jar:1.0-SNAPSHOT [ERROR] dependency: org.apache.commons:commons-txt:jar:1.15.0 (compile) [ERROR] Could not find artifact org.apache.commons:commons-txt:jar:1.15.0 in central (https://repo.maven.apache.org/maven2) $ mvn compile [ERROR] Failed to execute goal on project receipt-app: Could not resolve dependencies for project com.college:receipt-app:jar:1.0-SNAPSHOT [ERROR] dependency: org.apache.commons:commons-txt:jar:1.15.0 (compile) [ERROR] org.apache.commons:commons-txt:jar:1.15.0 was not found in https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced pom.xml asks for commons-text 1.13.1, never downloaded, and the build is offline $ mvn -o compile [ERROR] Failed to execute goal on project receipt-app: Could not resolve dependencies for project com.college:receipt-app:jar:1.0-SNAPSHOT [ERROR] dependency: org.apache.commons:commons-text:jar:1.13.1 (compile) [ERROR] Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.apache.commons:commons-text:jar:1.13.1 has not been downloaded from it before.
What this code does
- The first message names the exact coordinates Maven looked for:
commons-txtis the typo. The second run shows the cached failure; fixing the typo solves it, and-Uforces a retry when the coordinates were right. - Offline mode can only use the local repository, and version 1.13.1 was never downloaded on this computer.
Read the line under the error: it names the exact coordinates Maven tried. The usual cause is a typo in groupId, artifactId or version, or a version that does not exist.
Offline mode is handy on a slow connection, but it fails for any version you have never downloaded. Behind a college or office proxy, ask for the proxy settings: Maven needs them in ~/.m2/settings.xml.
Fix the error
Fix the dependency
The build cannot resolve this library. Pick the corrected line.
The groupId and version are right.
Error
Could not resolve dependencies for project com.college:receipt-app:jar:1.0-SNAPSHOT The following artifacts could not be resolved: org.apache.commons:commons-txt:jar:1.15.0 (absent)
Next step
Check your understanding
12 quick questions