LinuxCommandLibrary

mvn-dependency

Manage Maven project dependencies

TLDR

Display the full dependency tree, including direct and transitive dependencies

$ mvn dependency:tree
copy

Analyze the dependencies and highlight unused or undeclared ones
$ mvn dependency:analyze
copy

Copy all project dependencies (by default, to target/dependency/)
$ mvn dependency:copy-dependencies
copy

Resolve and download all project dependencies to the local Maven repository
$ mvn dependency:resolve
copy

Force Maven to update all dependencies from remote repositories
$ mvn dependency:resolve [[-U|--update-snapshots]]
copy

SYNOPSIS

mvn dependency:goal [-Doption=value]...

PARAMETERS

-Dclassifier=value
    Classifier for dependency artifacts (e.g., sources, javadoc).

-DexcludeGroupIds=groups
    Comma-separated GroupIds to exclude from processing.

-DexcludeArtifacts=artifacts
    Comma-separated ArtifactIds to exclude.

-DincludeGroupIds=groups
    Comma-separated GroupIds to include.

-DincludeArtifacts=artifacts
    Comma-separated ArtifactIds to include.

-DoutputDirectory=dir
    Target directory for output files.

-DoutputFile=name
    Name of the output file.

-DoverwriteReleases
    Overwrite existing release artifacts.

-DoverwriteSnapshots
    Overwrite existing snapshot artifacts.

-Dsilent
    Suppress console output.

-DstripVersion
    Strip version from output filenames.

DESCRIPTION

The Maven Dependency Plugin (invoked as mvn dependency:goal) provides essential functionality for handling artifacts and dependencies in Maven-based Java projects. It enables developers to analyze dependency usage, resolve and download dependencies, copy or unpack artifacts to custom locations, generate classpaths, and prepare projects for offline builds.

Key capabilities include visualizing the full dependency tree with dependency:tree to spot conflicts or transitives, detecting unused or undeclared dependencies via dependency:analyze, downloading sources/javadocs with dependency:sources, and copying dependencies into a lib directory for distribution using dependency:copy-dependencies. This plugin streamlines build processes, improves reproducibility, and aids in dependency hygiene.

Goals share common options like exclusion/inclusion filters and output settings, configurable via -D properties. It's integral for CI/CD pipelines and large-scale projects, reducing classpath issues and optimizing artifact management. Requires Maven 3.x and a valid pom.xml. (178 words)

CAVEATS

Requires installed Maven and pom.xml; some goals need network access to repositories. Not for non-Maven projects. Output varies by goal.

COMMON GOALS

dependency:tree - Displays dependency tree.
dependency:analyze - Checks for unused/undeclared deps.
dependency:copy-dependencies - Copies deps to directory.
dependency:sources - Downloads source JARs.

EXAMPLE

mvn dependency:tree - View full tree.
mvn dependency:copy-dependencies -DoutputDirectory=target/lib - Copy to lib dir.

HISTORY

Introduced in Maven 2.0 (2005); evolved through Maven 3.x. Current version 3.6.1 (2023) adds better conflict resolution and AOT support.

SEE ALSO

mvn(1)

Copied to clipboard