mvn-dependency
Manage Maven project dependencies
TLDR
Display the full dependency tree, including direct and transitive dependencies
Analyze the dependencies and highlight unused or undeclared ones
Copy all project dependencies (by default, to target/dependency/)
Resolve and download all project dependencies to the local Maven repository
Force Maven to update all dependencies from remote repositories
SYNOPSIS
mvn dependency:<goal> [-D<property>=<value>] [-P<profile>] [<Maven_Options>]
PARAMETERS
dependency:tree
Displays the full dependency tree for the project, showing direct and transitive dependencies. Useful for identifying conflicts or unwanted inclusions.
Example: mvn dependency:tree -Dverbose
dependency:resolve
Resolves all project dependencies and displays a list of the resolved artifacts. It's often used implicitly by other goals but can be run explicitly to check resolution issues.
dependency:analyze
Analyzes the project's dependencies for potential issues, reporting 'used declared', 'used undeclared', and 'unused declared' dependencies. Helps in optimizing pom.xml.
Example: mvn dependency:analyze
dependency:copy-dependencies
Copies project dependencies to a specified output directory. Useful for creating a 'lib' folder for deployment or execution.
Example: mvn dependency:copy-dependencies -DoutputDirectory=target/libs
dependency:purge-local-repository
Purges (deletes) artifacts from the local Maven repository based on the project's dependencies, forcing Maven to re-download them.
Example: mvn dependency:purge-local-repository -DactTransitively=false
dependency:get
Downloads a single artifact from the remote repositories to the local repository. Handy for manually fetching a specific library.
Example: mvn dependency:get -Dartifact=groupId:artifactId:version
-D<property>=<value>
Sets a system property, often used to pass configuration parameters to specific goals (e.g., -DoutputFile=tree.txt for dependency:tree).
-P<profile>
Activates a specific Maven profile defined in the pom.xml or settings, which can alter dependency declarations or plugin configurations.
<Maven_Options>
General Maven command-line options like -U (force update of snapshots/releases), -B (batch mode), -f <file> (specify POM file), etc.
DESCRIPTION
The "mvn-dependency" command is not a standalone Linux executable but rather a shorthand reference to invoking the Maven Dependency Plugin via the mvn command-line tool. This powerful plugin is an essential part of the Apache Maven ecosystem, designed to manage, analyze, and manipulate project dependencies. Its primary functions include:
• Understanding Dependency Trees: Visualizing and analyzing the full hierarchy of direct and transitive dependencies.
• Resolving Conflicts: Identifying and helping to resolve version conflicts among dependencies.
• Downloading Artifacts: Copying project dependencies to a specified location.
• Analyzing Usage: Reporting unused, used declared, and used undeclared dependencies.
• Purging Local Repository: Cleaning specific artifacts from your local Maven repository.
It is crucial for maintaining healthy and efficient Java projects by providing deep insights into their external library requirements and ensuring consistent builds.
CAVEATS
The "mvn-dependency" command requires Apache Maven to be installed and properly configured on your system. It also requires a valid pom.xml file in the current or parent directory to operate on a project's dependencies.
Direct command-line usage of plugin goals like dependency:tree should be distinguished from Maven's standard build lifecycle goals (e.g., mvn clean install), where dependency resolution often occurs implicitly. Misunderstanding dependency scopes or transitive dependencies can lead to runtime errors or bloated artifacts.
DEPENDENCY SCOPES
Maven dependencies are assigned a scope that dictates their lifecycle and classpath inclusion:
• compile: Default scope, required for compiling, building, and running.
• provided: Required for compile, but provided by the runtime environment (e.g., a servlet container). Not packaged in the WAR/EAR.
• runtime: Not required for compile, but for execution.
• test: Required only for compiling and running tests.
• system: Similar to provided but refers to a JAR on the local filesystem, usually discouraged.
• import: Used in a dependencyManagement section to import dependencies from another POM (a Bill of Materials - BOM).
TRANSITIVE DEPENDENCIES
Maven automatically includes dependencies of your dependencies. For example, if your project depends on library A, and library A depends on library B, Maven will automatically include B in your project's classpath. This simplifies dependency declaration but can lead to unexpected conflicts or 'dependency bloat' if not managed carefully.
EXCLUSIONS
To prevent unwanted transitive dependencies, you can explicitly exclude them in your pom.xml. This is crucial for resolving version conflicts or removing libraries you don't need.
Example:
<dependency>
<groupId>com.example</groupId>
<artifactId>my-lib</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>com.another</groupId>
<artifactId>unwanted-transitive</artifactId>
</exclusion>
</exclusions>
</dependency>
HISTORY
The Apache Maven project, initiated in 2002, revolutionized Java build processes by introducing a Project Object Model (POM) and a plugin-based architecture. The Maven Dependency Plugin, a core component, emerged early in Maven's development to address the complex challenges of managing project libraries. Prior to Maven, developers often manually managed JAR files, leading to 'JAR hell' (version conflicts and missing dependencies). The dependency plugin, along with Maven's central repository concept, provided a standardized, declarative approach to dependency management, significantly improving project consistency and build reproducibility across the Java ecosystem.


