blob: 5df9f9102f2f6fce9b5e71b89c83825da5d10877 [file]
---
title: Usage
author:
- Allan Ramirez
- Brian Fox
date: 2010-05-01
---
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
# Usage
Brief examples of how to use the dependency goals:
<!-- MACRO{toc|fromDepth=2} -->
`dependency:copy`
-----------------
This goal is meant to be bound to a lifecycle phase and configured in your `pom.xml`. It will resolve the artifact from the repository and place a copy in the specified location. Multiple artifacts can be defined in a single execution. A default output directory is specified but can be overridden for each ArtifactItem by setting the optional outputDirectory field. An optional new name can be set to rename or the version stripped while copying.
The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section.
Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactId and version satisfies the check for any type and classifier, but it adds the transitive dependencies of that module to the classpath (compile scope by default) and shows in `dependency:tree` as `g:a:pom:v`; give it scope `provided` or `test` and, to avoid the transitive dependencies, an `<exclusion>` of `*:*`. An item without a version takes its version from the project, as for the goal itself, and is then checked like any other item. a version range in the declaration matches an item version it contains, and a range in the item matches only an identical declaration. The check does not apply when the goal is invoked directly from the command line, such as `mvn dependency:copy`; an execution bound to the lifecycle is checked even if the property is given on the command line.
See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled.
Configure the plugin something like this if you intend to bind it to execute along with your build:
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
```
If you intend to configure this goal for execution on the command line using:
```
mvn dependency:copy
```
you must not put the configuration inside the _executions_ tag. Your configuration should look like this:
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
```
_Notes:_
- By default, artifacts are copied into _outputDirectory_ using the Maven artifact file name convention (that is, _artifactId-version-classifier.type_). Use the following rules to override the default convention:
- Use _artifactItem.destFileName_ to override the default file name.
- On the command line, give the file name as the sixth token of _artifact_: `mvn dependency:copy -Dartifact=groupId:artifactId:version:jar::name.jar` (the empty fifth token is the classifier). The form is `groupId:artifactId:version[:packaging[:classifier[:destFileName]]]`.
- Use _stripVersion_ to remove _version_ from default file name. This field is ignored when _artifactId.destFileName_ is set.
- Use _artifactItem.outputDirectory_ to override the plugin's _outputDirectory_ configuration per _artifactItem_.
- Use _remoteRepositories_ to resolve the artifacts from additional repositories, for example `-DremoteRepositories=myrepo::https://repo.acme.com/maven2`. The repositories of the project are still used.
`dependency:copy-dependencies`
------------------------------
This goal can be bound to a lifecycle phase and configured in your `pom.xml`. It will resolve the dependencies (including transitive dependencies) from the repository and place a copy in the specified location.
The artifacts can be placed in subfolders based on type. For example:
/outputDirectory
/outputDirectory/jars
/outputDirectory/wars
The artifacts can be placed in a subfolder per artifact. For example: /outputDirectory/junit-junit-3.8.1/ This feature also works with the subfolders per type. For example: /outputDirectory/jars/junit-junit-3.8.1/
Artifacts can also be resolved by specifying the classifier and optionally type. Type is only used with the classifier and defaults to java-sources. When the classifier is set, the list of dependencies is used as the base to resolve artifacts with the classifier and type. For example: `mvn dependency:copy-dependencies -Dclassifier=sources` will try to find the sources for all dependencies and copy them.
Also included is the ability to include or exclude by type (war, jar, etc.), scope (runtime, test, etc.), classifier (jdk14, sources, etc.), groupId, artifactId, or a combination of them.
**Note:** You may mix includes and excludes of the same category (e.g. scope). Includes are processed before excludes.
See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled.
The goal can also be launched from the command line like: `mvn dependency:copy-dependencies [optional params]`
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
```
`dependency:unpack`
-------------------
This goal is meant to be bound to a lifecycle and configured in your `pom.xml`. It will resolve the artifact from the repository and place a copy in the specified location. Multiple artifacts can be defined in a single execution. A default outputDirectory is specified but can be overridden for each ArtifactItem by setting the optional outputDirectory field.
A single artifact can be unpacked multiple times if different include/exclude parameters are defined for each artifact item.
See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled.
The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section.
Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactId and version satisfies the check for any type and classifier, but it adds the transitive dependencies of that module to the classpath (compile scope by default) and shows in `dependency:tree` as `g:a:pom:v`; give it scope `provided` or `test` and, to avoid the transitive dependencies, an `<exclusion>` of `*:*`. An item without a version takes its version from the project, as for the goal itself, and is then checked like any other item. a version range in the declaration matches an item version it contains, and a range in the item matches only an identical declaration. The check does not apply when the goal is invoked directly from the command line, such as `mvn dependency:copy`; an execution bound to the lifecycle is checked even if the property is given on the command line.
Configure the plugin something like this if you intend to bind it to execute along with your build:
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
```
If you intend to configure this goal for execution on the command line using:
```
mvn dependency:unpack
```
you must not put the configuration inside the _executions_ tag. Your configuration should look like this:
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<includes>[ comma separated list of file filters ]</includes>
<excludes>[ comma separated list of file filters ]</excludes>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
```
_Notes:_
- Use _remoteRepositories_ to resolve the artifacts from additional repositories, for example `-DremoteRepositories=myrepo::https://repo.acme.com/maven2`. The repositories of the project are still used.
`dependency:unpack-dependencies`
--------------------------------
This goal can be bound to a lifecycle and configured in your `pom.xml`. It will resolve the dependencies (including transitive dependencies) from the repository and unpack them to the specified location.
Unpack-dependencies includes transitive dependencies by default. To include only direct dependencies, set the excludeTransitive parameter to true.
Dependencies can be included or excluded by a list of types. See [unpack-dependencies](./unpack-dependencies-mojo.html) for details.
The artifacts can be unpacked in subfolders based on type. For example:
/outputDirectory
/outputDirectory/jars
/outputDirectory/wars
The artifacts can be placed in a subfolder per artifact. For example: /outputDirectory/junit-junit-3.8.1/ This feature also works with the subfolders per type. For example: /outputDirectory/jars/junit-junit-3.8.1/
Artifacts can also be resolved by specifying the classifier and optionally type. Type is only used with the classifier and defaults to java-sources. When the classifier is set, the list of dependencies is used as the base to resolve artifacts with the classifier and type. For example: `mvn dependency:unpack-dependencies -Dclassifier=sources` will try to find the sources for all dependencies and unpack them.
Filters can be applied to include or exclude files or filesets as necessary
Also included is the ability to include or exclude by type (war, jar, etc.), scope (runtime, test, etc.), classifier (jdk14, sources, etc.), groupId, artifactId, or a combination of them.
**Note:** You can mix includes and excludes of the same category (e.g. scope). Includes are processed before excludes.
See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled.
The goal can also be launched from the command line like: `mvn dependency:unpack-dependencies [optional params]`
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
```
#[[### Overwrite Rules]]#
Artifacts are copied or unpacked using the following rules:
- If the artifact doesn't exist in the destination, then copy/unpack it.
Otherwise:
- For copy/unpack goal only: if `artifactItem / overWrite` or `overWrite` is true, then it will force an overwrite.
- Releases check the `overWriteReleases` value (default = false). If true, then it will force an overwrite.
- Snapshots check the `overWriteSnapshots` value (default = false). If true, then it will force an overwrite.
- If none of the above is set to true, then it defaults to the `overWriteIfNewer` value (default = true). This value, if true, causes the plugin to only copy if the source is newer than the destination (or it doesn't exist in the destination). (for unpack, this checks the existence of the marker file, created in the `markersDirectory` path. To avoid unexpected behavior after `mvn clean`, this path should normally be contained within the `/target` hierarchy.)
Examples:
- Using the default settings (`overWriteReleases` = false, `overWriteSnapshots` = false, `overWriteIfNewer` = true), then a release or snapshot artifact will only over write the destination if the source is newer than the destination (or marker file if unpacking).
- If `overWriteReleases` = true, then a release artifact (e.g. `foo-1.0.jar`) will always overwrite.
- If `overWriteSnapshots` = true, then a snapshot artifact (e.g. `foo-1.0-SNAPSHOT.jar`) will always overwrite.
- If all of the values are false, then a copy/unpack will only occur if the artifact doesn't exist in the destination (or `markersDirectory` if unpacking).
`dependency:resolve`
--------------------
Resolve is intended to be used from the command line like: `mvn dependency:resolve`
This goal resolves all test and compile scoped dependencies and then displays the resolved versions. This ensures all dependencies are downloaded to the local repository. This is useful when troubleshooting or during intermittent remote repository failures when repeatedly building multiproject modules is undesirable and the build is failing on dependency resolution. It can also be used to quickly determine how versions are being resolved.
Maven resolves the dependencies before the goal runs, so every dependency must be available as a file. In a multi-module project that has not been built yet, a module that depends on another module of the same build fails. Either run a phase that packages the modules in the same build, such as `mvn package dependency:resolve`, or use `dependency:collect`, which needs only the POM files. The `exclude*` and `include*` parameters filter what the goal displays; they do not stop Maven from resolving those dependencies.
Artifacts can also be resolved by specifying the classifier and optionally type. Type is only used with the classifier and defaults to java-sources. When the classifier is set, the list of dependencies is used as the base to resolve artifacts with the classifier and type. For example: `mvn dependency:resolve -Dclassifier=test-jar` will try to find the test-jar for all dependencies resolve them to the local repository.
The `outputType` parameter (since 3.12.0) of `dependency:resolve`, `dependency:list` and `dependency:collect` selects a machine-readable output, which `outputFile` writes to a file:
- `text` is the default listing.
- `json` writes an object with the arrays `resolved`, `skipped` and `unresolved`. Each element has the string members `groupId`, `artifactId`, `version`, `type`, `scope`, `classifier` and `optional` of the JSON of `dependency:tree`, and `file` with `-DoutputAbsoluteArtifactFilename`.
- `pom` writes the resolved artifacts as a `<dependencies>` fragment, with `type` unless it is `jar`, `classifier` if there is one, `scope` unless it is `compile` and `optional` if it is, for example to start a `dependencyManagement` section.
```
mvn dependency:list -DoutputType=pom -DoutputFile=dependencies.xml
```
The `includes` and `excludes` parameters (since 3.12.0) of `dependency:resolve`, `dependency:list`, `dependency:collect` and `dependency:resolve-sources` take the same comma-separated `[groupId]:[artifactId]:[type]:[version]` patterns as the parameters of `dependency:tree`, with `*` wildcards. They apply in addition to the other `include*` and `exclude*` parameters, and an exclusion wins over an inclusion. The goals list only the matching artifacts, including parent POMs added by `includeParents`; unlike `dependency:tree`, they do not keep the path to an included artifact, and excluding an artifact does not exclude its dependencies. For `dependency:resolve-sources`, the patterns match the declared dependencies, not the `sources` artifacts resolved for them.
```
mvn dependency:list -Dincludes=org.springframework
mvn dependency:list -Dincludes=org.springframework*
mvn dependency:list -Dincludes=:::*-SNAPSHOT -Dexcludes=org.acme
```
`org.springframework` matches that group id only; `org.springframework*` also matches groups such as `org.springframework.boot`.
`dependency:resolve-sources`
----------------------------
Resolve-sources is intended to be used from the command line like: `mvn dependency:resolve-sources`
This is the same as the resolve goal except it includes the source attachments if they exist. This is useful when you want to download source attachments to your local repository.
You can also define the `markersDirectory` either in the `pom.xml` or settings to be a common location for all projects. This allows the system to resolve sources faster for dependencies that don't have the sources published. The plugin will store a marker file to describe if the sources were resolved or not. By placing them in a common location, multiple attempts to resolve non-existent sources will be avoided.
`dependency:resolve-plugins`
----------------------------
Resolve-plugins is intended to be used from the command line like: `mvn dependency:resolve-plugins -DexcludeTransitive=true`
This is the same as the resolve goal except it resolves plugins and optionally their dependencies.
`dependency:go-offline`
-----------------------
This goal resolves the same artifacts as calling `mvn dependency:resolve dependency:resolve-plugins`. In addition, it resolves the artifacts listed in the `annotationProcessorPaths` of the `maven-compiler-plugin`, which Maven 3 resolves only when it compiles. The paths are read from the plugin configuration and from each execution, and their transitive dependencies are resolved too (with the project's `dependencyManagement` when `annotationProcessorPathsUseDepMgmt` is `true`). The scope filters, such as `includeScope` and `excludeScope`, treat annotation processor paths as compile scope.
`dependency:properties`
------------------------
This goal sets a project property for each project dependency (direct and transitive), named `<groupId>:<artifactId>:<type>[:<classifier>]`, that holds the absolute path of the resolved artifact file. It is bound to the `initialize` phase by default.
Set `includeVersions` (user property `mdep.properties.includeVersions`) to also get a property with the version of the resolved artifact, named like the path property plus a `.version` suffix. This includes transitive dependencies, and the version is set even for an artifact that has no file. For a snapshot dependency the base version, for example `1.0-SNAPSHOT`, is used. An artifact from `extraArtifacts` with the same key overrides the value of the project dependency. The suffix can only collide with another key if a type or classifier itself ends in `.version`.
Bind the goal in the `pom.xml` and let the user property switch the versions on:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
```
Then run `mvn verify -Dmdep.properties.includeVersions=true`. A plugin that runs after the goal can read the property in its configuration, for example `#[[${org.slf4j:slf4j-api:jar.version}]]#` resolves to the version Maven selected, such as `2.0.16`. To always set the versions, put `<includeVersions>true</includeVersions>` in the `<configuration>` of the execution.
`dependency:purge-local-repository`
-----------------------------------
This goal is meant to delete all of the dependencies for the current project (or projects, in the case of a multimodule build) from the local repository. Purges can be run with a variety of limiting parameters, including artifact exclusions, limiting to direct dependencies only, and different levels of depth for deletion. By default, deleted artifacts can be re-resolved afterwards; you can disable this by specifying `-DreResolve=false`.
In its simplest form, the goal can be called like this:
```
mvn dependency:purge-local-repository
```
To add the restriction that the `org.apache.maven:maven-plugin-api` artifact not be deleted, we can modify the command to this:
```
mvn dependency:purge-local-repository -Dexclude=org.apache.maven:maven-plugin-api
```
**Note:** The `exclude` parameter is a comma-delimited list of groupId:artifactId pairs. It has a corresponding List-based parameter - `excludes` - for convenient use inside the `pom.xml`.
Another handy aspect of this goal is the ability to wipe out artifacts at varying depths. These depths are:
- **file** - Delete just the artifact's file.
- **version** - Delete the version directory containing this artifact. Amounts to `file.getParentFile()`.
- **artifactId** - Delete the artifactId directory containing this artifact. Amounts to `file.getParentFile().getParentFile()`.
- **groupId** - Delete the groupId directory structure containing this artifact.
At a minimum, this amounts to `file.getParentFile().getParentFile().getParentFile()`. However, if the groupId contains multiple parts, the goal will prune all groupId-part directories that contain only the files associated with this artifact. Again, note that the exception to this pruning algorithm is the lowest groupId part, which will always be pruned.
To prune dependency artifacts back to their associated artifactId directories (in order to verify proper artifact resolution, for example), simply use this command:
```
mvn dependency:purge-local-repository -DresolutionFuzziness=artifactId
```
Finally, it's possible to bind this goal to the build lifecycle. One reason for this might be to clean out all dependencies when the build is initialized, to verify correct resolution.
`dependency:analyze`
--------------------
This goal performs byte code analysis to determine missing or unused dependencies. This goal is meant to be launched from the command line. It will fork the build and execute test-compile so there are class files to analyze. If you want to bind analyze in your `pom.xml`, use the `dependency:analyze-only` instead.
This goal can be executed from the command line:
```
mvn dependency:analyze
```
Sample output:
```text
[INFO] [dependency:analyze]
[WARNING] Used undeclared dependencies found: (in org.example:my-app)
[WARNING] classworlds:classworlds:jar:1.1-alpha-2:compile
[WARNING] org.apache.maven.shared:maven-invoker:jar:2.0.7-SNAPSHOT:test
```
`dependency:analyze-only`
-------------------------
This goal performs bytecode analysis to determine missing or unused dependencies, similar to `dependency:analyze`.
It will not fork the build with test compilation, so it should be used when you want to bind the analyze goal in your `pom.xml`.
`dependency:analyze-dep-mgt`
----------------------------
This goal looks at the dependencies after final resolution and looks for mismatches in your dependencyManagement section.
This goal is useful for detecting projects that override the dependencyManagement directly. Set ignoreDirect to false to detect these otherwise normal conditions.
This goal can be executed from the command line:
```
mvn dependency:analyze-dep-mgt
```
Sample output:
```text
[INFO] Found Resolved Dependency / DependencyManagement mismatches:
[INFO] Dependency: commons-lang:commons-lang:jar
[INFO] DepMgt : 1.0
[INFO] Resolved: 2.3
[INFO] Dependency: commons-digester:commons-digester:jar
[INFO] DepMgt : 1.6
[INFO] Resolved: 1.7
[INFO] Dependency: javax.servlet:servlet-api:jar
[INFO] DepMgt : 2.3
[INFO] Resolved: 2.4
[WARNING] Potential problems found in Dependency Management
```
`dependency:analyze-report`
---------------------------
This goal is used to include a report of the dependencies in the output of the `maven-site-plugin`.
```xml
<reporting>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<reportSets>
<reportSet>
<reports>
<report>analyze-report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
```
Use `analyze-report-only` instead of `analyze-report` to render the same report without forking the `test-compile` phase, for example in a build that has already compiled the project:
```xml
<report>analyze-report-only</report>
```
`dependency:tree`
-----------------
This goal is used to view the dependency hierarchy of the project currently being built. It will output the resolved tree of dependencies that the Maven build process actually uses.
This goal can be executed from the command line:
```
mvn dependency:tree
```
Optionally, the `output` parameter can be specified to divert the output to a file:
```
mvn dependency:tree -DoutputFile=/path/to/file
```
Also, the `outputType` parameter can be used to generate different formats of output. The following formats are currently supported:
- text
- [dot](https://en.wikipedia.org/wiki/DOT_language)
- [graphml](https://en.wikipedia.org/wiki/GraphML)
- [tgf](https://en.wikipedia.org/wiki/Trivial_Graph_Format)
- [mermaid](https://mermaid.js.org/syntax/flowchart.html) (since 3.12.0)
```
mvn dependency:tree -DoutputFile=/path/to/file.graphml -DoutputType=graphml
```
The `showSizes` parameter (since 3.12.0) shows how much each dependency weighs. By default `dependency:tree` only collects the dependency graph and downloads no artifacts. With `-DshowSizes` the goal also resolves the artifact of each node, which downloads those missing from the local repository, and shows its file size and, for an artifact with dependencies, the total:
```
mvn dependency:tree -DshowSizes
```
```
org.example:app:jar:1.0-SNAPSHOT (total 1.5 MB)
\- org.foo:foo-all:jar:2.0:compile (200 KB, total 1.5 MB)
+- org.foo:foo-core:jar:2.0:compile (1.3 MB)
\- org.foo:foo-gone:jar:2.0:compile (size unknown)
```
- The total of a node is the size of all the distinct artifacts that the dependency brings in as Maven resolves them: its own artifact and those of its dependencies, transitively, each counted once. Distinct means a different `groupId:artifactId:type[:classifier]:version`.
- A dependency that Maven resolves once, below another node, counts for every node that depends on it, also in the tree without `-Dverbose`, where it is shown only once. The totals of two branches can therefore overlap, and the total of the project at the top, which is the size of all distinct dependencies, is less than the sum of the totals of its dependencies. An artifact that Maven leaves out, because it is a duplicate or loses to another version, does not count itself; the kept one does.
- The totals are computed on the verbose tree even without `-Dverbose`, and before the `includes` and `excludes` filters apply, so a filter changes which nodes are shown, not their totals.
- Sizes use the units B, KB, MB, GB and TB, where 1 KB is 1024 bytes, rounded to one decimal.
- A dependency of scope `system` is measured at its `systemPath`. An artifact that cannot be resolved is shown as `(size unknown)`, counts as zero in the totals, and the goal logs one warning with the number of such artifacts. An artifact whose file is not a regular file, such as the classes directory of a module of the same reactor build, is shown the same way. Run with `-X` to see why.
- Nodes that `-Dverbose` shows as omitted are not resolved and show no size.
- With `-DoutputType=json` each node that is not omitted gets the number `totalSize` and, when known, the number `size`, both in bytes. The `dot`, `graphml`, `tgf` and `mermaid` formats ignore the parameter with a warning and download nothing.
The `verbose` parameter explains the tree. By default the tree shows what Maven resolves: each artifact once, at the version and scope Maven selected. With `-Dverbose` the goal collects the dependencies again and also shows the nodes that Maven left out, and why:
```
mvn dependency:tree -Dverbose
```
For this project, whose `dependencyManagement` section sets `maven-model` to version `2.0.5` with scope `test`:
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-profile</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0.7</version>
<scope>runtime</scope>
</dependency>
</dependencies>
```
the output is:
```
org.example:app:jar:1.0-SNAPSHOT
+- org.apache.maven:maven-project:jar:2.0.6:compile
| +- org.apache.maven:maven-settings:jar:2.0.6:compile
| | +- (org.apache.maven:maven-model:jar:2.0.5:test - version managed from 2.0.6; scope managed from compile; omitted for conflict with 2.0.7)
| | +- (org.codehaus.plexus:plexus-utils:jar:1.4.1:compile - omitted for duplicate)
| | \- (org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile - omitted for duplicate)
| +- (org.apache.maven:maven-profile:jar:2.0.6:compile - omitted for conflict with 2.0.4)
| +- (org.apache.maven:maven-model:jar:2.0.5:test - version managed from 2.0.6; scope managed from compile; omitted for conflict with 2.0.7)
| +- org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
| | +- org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
| | | \- (org.codehaus.plexus:plexus-utils:jar:1.4.1:compile - omitted for duplicate)
| | +- (org.codehaus.plexus:plexus-utils:jar:1.4.1:compile - omitted for duplicate)
| | +- (org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile - omitted for duplicate)
| | \- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:compile
| | \- (org.codehaus.plexus:plexus-utils:jar:1.0.4:compile - omitted for conflict with 1.4.1)
| +- org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
| | +- (org.codehaus.plexus:plexus-utils:jar:1.4.1:compile - omitted for duplicate)
| | \- (org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile - omitted for duplicate)
| +- org.codehaus.plexus:plexus-utils:jar:1.4.1:compile
| \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
| +- junit:junit:jar:3.8.1:compile
| +- (org.codehaus.plexus:plexus-utils:jar:1.0.4:compile - omitted for conflict with 1.4.1)
| \- classworlds:classworlds:jar:1.1-alpha-2:compile
+- org.apache.maven:maven-profile:jar:2.0.4:test (scope not updated to compile)
| +- (org.apache.maven:maven-model:jar:2.0.5:test - version managed from 2.0.4; scope managed from compile; omitted for conflict with 2.0.7)
| +- (org.codehaus.plexus:plexus-utils:jar:1.1:test - omitted for conflict with 1.4.1)
| \- (org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:test - omitted for conflict with 1.0-alpha-9-stable-1)
\- org.apache.maven:maven-model:jar:2.0.7:runtime
\- (org.codehaus.plexus:plexus-utils:jar:1.4.1:runtime - omitted for duplicate)
```
A node in parentheses is omitted: Maven does not use it. The text after the dash says why:
- `omitted for duplicate` means the same artifact and version appears elsewhere in the tree, closer to the root, and that node is the one in use. For example, `plexus-utils:1.4.1` below `maven-settings` is a repeat of the one directly below `maven-project`.
- `omitted for conflict with <version>` means another version of the artifact is in use. Maven keeps the version of the node closest to the root, and the first one if they are equally close, so the node for `maven-model:2.0.7` below the root wins over the `2.0.5` that the other paths ask for. An artifact that often loses to another version is a candidate for a `dependencyManagement` entry or an exclusion.
A node can also carry notes in parentheses after the artifact, without being omitted. A note does not change what the artifact is, it explains how it got there:
- `version managed from <version>` means `dependencyManagement` replaced the version the dependency declared with the one shown. Maven 4 manages the dependencies of transitive dependencies as well, so with Maven 4 more nodes carry this note, some of them with the same version before and after.
- `scope managed from <scope>` is the same for the scope. `maven-model:2.0.5:test` above was declared with scope `compile` and `dependencyManagement` set it to `test`.
- `scope not updated to <scope>` means another path to the same artifact asked for a wider scope, here `compile` through `maven-project`, but the node shown keeps its own scope. A direct dependency keeps the scope the project declares, `test` for `maven-profile:2.0.4`, and a `system` scope is never widened.
The notes combine with the omission: `version managed from 2.0.6; scope managed from compile; omitted for conflict with 2.0.7` is a node whose version and scope were managed, which then lost to another version.
With `-DoutputType=json` and `-Dverbose`, each node also has `included`, `omittedReason`, `winnerVersion`, `versionManagedFrom`, `scopeManagedFrom` and `scopeNotUpdatedTo` members, where they apply; see [Dependency tree output formats](./examples/tree-mojo.html). The `includes` and `excludes` parameters filter the verbose tree as well.
`dependency:build-classpath`
----------------------------
This goal will output a classpath string of dependencies from the local repository to a file or log and optionally attach and deploy the file. For instance, the file would contain a classpath string like this:
```
/home/foo/.m2/repository/org/java/utils/util/util-1.0.jar:/home/foo/.m2/ ....
```
The resulting file could then be used like this:
```
java -cp `cat resultFile` MyClass
```
In its simplest form, to output the classpath to the log, the goal can be called like this:
```
mvn dependency:build-classpath
```
or to write the classpath to cp.txt:
```
mvn dependency:build-classpath -Dmdep.outputFile=cp.txt
```
Without `prefix`, each entry is the actual file of the artifact. In a multi-module build, the file of a module in the same reactor is in its build output (`target/classes`, or the packaged file after `package`), not in the local repository. To get a classpath that is the same for every artifact, set `prefix` together with `useRepositoryLayout`. Each entry then has the layout of a Maven repository below the prefix:
```
mvn package dependency:build-classpath -Dmdep.prefix=repository -Dmdep.useRepositoryLayout=true -Dmdep.fileSeparator=/ -Dmdep.pathSeparator=: -Dmdep.outputFile=cp.txt
```
```
repository/org/example/module-a/1.0-SNAPSHOT/module-a-1.0-SNAPSHOT.jar:repository/junit/junit/3.8.1/junit-3.8.1.jar
```
`useRepositoryLayout` has no effect without `prefix`. With `prefix` alone, each entry is the prefix followed by the file name only. With the layout, `stripVersion`, `stripClassifier`, `useBaseVersion` and `prependGroupId` are ignored (the goal logs a warning), so the classpath matches a directory filled by `dependency:copy-dependencies -Dmdep.useRepositoryLayout=true`; the `mdep.useRepositoryLayout` user property is shared with that goal. A module of the same reactor must be packaged (run at `package` or later) for its entry to name an existing jar.
The goal can also be bound to a lifecycle phase with the following configuration:
```xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>build-classpath</id>
<phase>generate-sources</phase>
<goals>
<goal>build-classpath</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
```
`dependency:list-repositories`
------------------------------
This goal lists all the repositories that this build depends upon. It shows repositories defined in your settings, poms, and declared in transitive dependency poms.
`dependency:get`
----------------
This goal resolves an artifact and (optionally) its dependencies from remote repositories using its Maven coordinates. The Maven Central repository are always consider during resolving.
```
mvn dependency:get -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion=2.2.1 -Dpackaging=jar -Dclassifier=sources
mvn dependency:get -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion=2.2.1 -Dpackaging=jar -Dclassifier=sources -DremoteRepositories=https://myrepo.com/maven2
```
`dependency:get-classpath`
-------------------------
This goal resolves an artifact, given by its Maven coordinates, and its transitive runtime dependencies, and outputs the classpath string of the files in the local repository. It needs no project, so it works in any directory. Use it to get the classpath of an ad-hoc artifact for `java -cp`. The artifact is resolved as for a project that depends on it, so the result can differ from `dependency:get -Dtransitive`. The artifact comes first, followed by its runtime dependencies. Test, provided, system and optional dependencies are left out. The versions and the order of the dependencies follow the dependency management rules of the running Maven, which differ between Maven versions.
```
mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9
mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 -Dmdep.outputFile=classpath.txt
mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 -Dtransitive=false -DremoteRepositories=https://myrepo.com/maven2
```
Without `mdep.outputFile` the classpath is logged at the INFO level. Use `-q` together with `mdep.outputFile` to keep the build output quiet. The separators and a path prefix can be changed with `mdep.pathSeparator`, `mdep.fileSeparator` and `mdep.prefix`, as for `dependency:build-classpath`. The file separator is applied to each path entry on its own.
`dependency:analyze-exclusions`
-------------------------------
This goal checks exclusions on dependencies and checks if the artifact actually brings in the given dependency. For instance, given dependency a:b:1.0 transitively includes x:y:1.0 which you do not want for some reason and exclude it. Later a:b:2.0 has removed the unwanted dependency and you upgrade. This goal will inform you that the exclusion is no longer required.
```
mvn dependency:analyze-exclusions
```
Sample output:
```text
[WARNING] The following dependencies defines unnecessary excludes
[WARNING] org.apache.maven:maven-artifact:
[WARNING] - javax.annotation:javax.annotation-api
[WARNING] - javax.activation:javax.activation-api
```
`dependency:add`
----------------
This goal adds a dependency to the project's `pom.xml` directly from the command line. If the dependency already exists, the goal fails with a descriptive error directing you to remove it first. For detailed examples, see [Managing Dependencies](./examples/managing-dependencies.html).
```
mvn dependency:add -Dgav=org.apache.commons:commons-lang3:3.17.0
mvn dependency:add -Dgav=org.apache.commons:commons-lang3:3.17.0 -Dscope=test
```
Add to `<dependencyManagement>`:
```
mvn dependency:add -Dgav=org.apache.commons:commons-lang3:3.17.0 -Dmanaged
```
Add a BOM import:
```
mvn dependency:add -Dgav=org.springframework.boot:spring-boot-dependencies:pom:3.2.0 -Dscope=import -Dmanaged
```
In multi-module projects, use Maven's `-pl` flag to target a specific module:
```
mvn dependency:add -Dgav=org.apache.commons:commons-lang3:3.17.0 -pl my-service
```
`dependency:remove`
-------------------
This goal removes a dependency from the project's `pom.xml` directly from the command line. For detailed examples, see [Managing Dependencies](./examples/managing-dependencies.html).
```
mvn dependency:remove -Dgav=org.apache.commons:commons-lang3
mvn dependency:remove -Dgav=org.apache.commons:commons-lang3 -Dmanaged
```
When removing a managed dependency from a parent POM, the goal warns if child modules still reference that dependency without an explicit version.