Concept Explanation
You declare five dependencies. Maven downloads sixty. Those extra fifty-five are transitive — the dependencies of your dependencies — and they are where most build problems originate.
Scopes
Scope controls when a dependency is on the classpath:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope> <!-- compile only, not packaged -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope> <!-- tests only, never shipped -->
</dependency>
| Scope | Compile | Test | Packaged |
|---|---|---|---|
compile (default) | ✓ | ✓ | ✓ |
provided | ✓ | ✓ | ✗ |
runtime | ✗ | ✓ | ✓ |
test | ✗ | ✓ | ✗ |
Getting this wrong ships your test framework to production, or leaves a JDBC driver out of the jar.
Seeing the Tree
The single most useful command when something is wrong:
mvn dependency:tree
[INFO] com.example:shop:jar:1.0
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.2.0
[INFO] | +- org.springframework:spring-web:jar:6.1.1
[INFO] | \- com.fasterxml.jackson.core:jackson-databind:jar:2.15.3
[INFO] \- com.example:legacy-lib:jar:2.1
[INFO] \- (com.fasterxml.jackson.core:jackson-databind:jar:2.9.0 - omitted for conflict)
That last line is the story: two libraries wanted different Jackson versions, and Maven picked one.
Gradle's equivalent:
./gradlew dependencies --configuration runtimeClasspath
Conflict Resolution
The two tools resolve conflicts differently, and the difference matters:
- Maven — nearest wins. The version closest to your project in the tree is used, regardless of whether it is newer.
- Gradle — highest wins. The newest requested version is used.
Maven's rule can silently downgrade a library. That is the cause of many NoSuchMethodError failures: the code was compiled against 2.15 but 2.9 is on the classpath at runtime.
Pinning a Version
Force a version explicitly rather than hoping:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>
</dependencies>
</dependencyManagement>
dependencyManagement sets versions without adding the dependency — it applies wherever that artifact appears in the tree.
Exclude a transitive dependency you do not want:
<dependency>
<groupId>com.example</groupId>
<artifactId>legacy-lib</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
The Spring Boot BOM
This is why Spring Boot projects rarely specify versions:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
The parent POM is a bill of materials pinning tested-compatible versions of hundreds of libraries. Overriding one is how you break that guarantee.
Common Mistakes
- Overriding a single BOM-managed version: You lose the compatibility testing the BOM provided.
- Ignoring
NoSuchMethodError: It almost always means a version conflict, not a code bug. - Version ranges (
[1.0,2.0)): Your build stops being reproducible — the same commit can produce different jars. - Test libraries at
compilescope: Bloats the artifact and can leak test utilities into production. - Never auditing dependencies: Known CVEs in transitive libraries are a common attack vector.
Best Practices
- Run
dependency:treebefore guessing — it answers the question in seconds. - Pin versions in
dependencyManagement, never inline in multiple places. - Use the wrapper (
mvnw/gradlew) so the build tool version is fixed too. - Run a vulnerability scan in CI (OWASP dependency-check or Dependabot).
- Remove unused dependencies:
mvn dependency:analyzefinds declared-but-unused and used-but-undeclared entries.
Practice Exercises
- The Tree: Run
dependency:treeon a Spring Boot project and count how many artifacts one starter pulls in. - The Conflict: Add two libraries requiring different versions of the same dependency and find the "omitted for conflict" line.
- The Exclusion: Exclude a transitive dependency and confirm with
dependency:treethat it is gone.