Java Academy

Master Java Development

Menu

Core Java

Servlets

JDBC

Microservices

Build Tools

APIs

Build Tools · Lesson 3

Maven Dependency Management

Managing Project Dependencies

A dependency is a library another part of the application needs to compile or run. Maven records dependencies in pom.xml, resolves their transitive dependencies, and places them on the appropriate classpath. Explicit dependency management makes builds repeatable and upgrades reviewable.

Declaring a Dependency

A project should declare the library it directly uses rather than relying on a transitive dependency to appear by accident. The Java example represents the same direct-dependency idea with a small service contract.

public class DependencyCatalog {
    public static String directDependency(String libraryName, String version) {
        return libraryName + ":" + version;
    }

    public static void main(String[] arguments) {
        System.out.println(directDependency("jackson-databind", "2.17.0"));
    }
}

Maven resolves the declared module and its transitive dependencies from configured repositories. A dependency-management section can centralize versions without adding every managed module to the classpath.

Dependency Concepts

ConceptMeaningExample
DirectDeclared by this projectJSON library used by code
TransitivePulled by another dependencyLogging implementation
VersionSpecific release selected2.17.0
ExclusionPrevent a transitive moduleRemove conflicting logger

Common Mistake and Practices

Common Mistake: Declaring a library only because it happens to arrive transitively. An upstream upgrade can remove it and break compilation without any change in your source code.

  • Declare every library used directly.
  • Pin or centrally manage important versions.
  • Inspect the dependency tree during upgrades.
  • Remove unused dependencies.
  • Review licenses and security advisories.

Real-World Use Case

An API service declares Jackson directly because its controllers serialize JSON. Maven may also resolve logging libraries transitively, but the service does not depend on their accidental presence for compilation.

Quiz Check

Quick Knowledge Test

Loading quiz questions...