01 / 20

Maven/Gradle
Dependencies

Learn how to add external libraries to your Java project using build tools, so you never have to manage .jar files by hand again.

Java Build Tools

What Is a Dependency?

A dependency is an external library or module your project needs to compile and run. Instead of copying code, you declare what you need, and the build tool downloads it for you.

Without a build tool
Manually download .jar files, put them in a folder, configure your classpath by hand.
With a build tool
Write one line in a config file: the tool fetches, stores, and links the library automatically.

Meet Maven and Gradle

Both are build tools for Java. They automate compiling, testing, packaging, and managing dependencies. The main difference is how you write their config files.

Maven
Uses XML in a file called pom.xml. Older, highly standardized, very common in enterprise projects.
Gradle
Uses Groovy or Kotlin in a file called build.gradle. Newer, more concise, popular in Android development.

How Dependencies Flow

1. YOU DECLARE pom.xml / build.gradle 2. TOOL RESOLVES Finds version + deps 3. DOWNLOADS From Maven Central 4. CACHES LOCALLY ~/.m2 or ~/.gradle 5. ADDS TO CLASSPATH Ready to import! import com.google.gson.Gson; ✓

The GAV Coordinate System

Every dependency on Maven Central is uniquely identified by three coordinates. Think of it as a mailing address for code:

GroupId
The organization or project namespace (like a reversed domain). Example: com.google.code.gson
ArtifactId
The specific library name within that group. Example: gson
Version
Which release you want. Example: 2.10.1

Maven: pom.xml Syntax

In Maven, you declare dependencies inside a <dependencies> block in your pom.xml file using XML tags:

pom.xml
<dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> </dependencies>

Gradle: build.gradle Syntax

In Gradle, the same dependency is a single concise line inside a dependencies block. The GAV is separated by colons:

build.gradle
dependencies { implementation 'com.google.code.gson:gson:2.10.1' }

Maven vs Gradle: Side by Side

Feature Maven Gradle
Config file pom.xml build.gradle
Language XML Groovy / Kotlin
Dependency format 6 lines of XML tags 1 line: 'G:A:V'
Build command mvn compile gradle build
Local cache ~/.m2/repository ~/.gradle/caches
Popularity Enterprise / legacy Android / modern

Understanding Scopes

A scope (Maven) or configuration (Gradle) controls when a dependency is available: at compile time, runtime, or only for tests.

compile / implementation
Available at compile time and runtime. The default scope for most libraries.
test / testImplementation
Only available in test source sets. JUnit lives here: your production code never sees it.
provided / compileOnly
Needed to compile, but NOT included in the final .jar. The server provides it at runtime.

How to Add a Dependency

1
Find the library on search.maven.org or mvnrepository.com. Search by name (e.g., "gson").
2
Copy the GAV coordinates. Most sites show ready-to-paste snippets for both Maven and Gradle.
3
Paste into your config file: inside <dependencies> for Maven, or the dependencies block for Gradle.
4
Reload / sync the project. In IntelliJ: click the "Reload" icon. In terminal: run mvn compile or gradle build.
5
Import and use the library classes in your Java code. Done!

Example: Adding Gson in Maven

Goal: parse JSON strings in your Java app using Google's Gson library.

pom.xml (add inside <dependencies>)
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency>
App.java (after syncing)
import com.google.gson.Gson; public class App { public static void main(String[] args) { Gson gson = new Gson(); String json = gson.toJson("Hello!"); System.out.println(json); // "Hello!" } }

Example: Adding JUnit 5 in Gradle

Goal: set up unit testing in a Gradle project. JUnit is a test-only dependency, so we use testImplementation.

build.gradle
dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' } test { useJUnitPlatform() }
AppTest.java
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class AppTest { @Test void additionWorks() { assertEquals(4, 2 + 2); } }

Example: Multiple Dependencies

Real projects often need several libraries. Just stack them in the same block:

build.gradle
dependencies { implementation 'com.google.code.gson:gson:2.10.1' implementation 'org.apache.commons:commons-lang3:3.14.0' implementation 'org.slf4j:slf4j-api:2.0.9' testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' }

Transitive Dependencies

When you add library A, it might depend on library B, which depends on C. The build tool fetches all of them automatically: these are called transitive dependencies.

Your Project Library A lib-B lib-C YOU ADD AUTO FETCHED

Common Mistakes

✕ Wrong scope
Using implementation for a test library means it ships in your production .jar, bloating it unnecessarily.
✕ Missing version
Omitting the version tag in Maven or the third colon-segment in Gradle causes a build error.
✕ Forgetting to sync
Adding a dependency to the config file but not refreshing the project means your IDE still can't find the classes.
✕ Typo in GAV
A single wrong character in groupId or artifactId leads to "dependency not found" errors: always copy-paste.

Question 1 of 5

What are the three parts of a Maven/Gradle dependency coordinate (the "GAV")?

Question 2 of 5

In a Gradle build file, which keyword adds a dependency that is only available during testing?

Question 3 of 5

You added a dependency to your pom.xml, but your IDE says the class cannot be found. What is the most likely fix?

Question 4 of 5

Which of the following is the correct Gradle syntax to add version 3.14.0 of Apache Commons Lang?

Question 5 of 5

What are transitive dependencies?