Apache Maven Dependency Plugin

The Apache Maven Dependency Plugin is a powerful tool that helps manage and analyze project dependencies. It provides various goals to analyze, list, copy, unpack, and resolve dependencies. This guide will cover the essential features of the Maven Dependency Plugin, along with practical examples to help you get started.

Introduction to the Maven Dependency Plugin

The Maven Dependency Plugin offers several goals for managing project dependencies, including:

  • analyze: Analyzes project dependencies and identifies unused declared and used undeclared dependencies.
  • list: Lists the dependencies of a project.
  • copy: Copies project dependencies to a specific location.
  • unpack: Unpacks project dependencies.
  • resolve: Resolves project dependencies and displays the version used.

Latest Version

As of this writing, the latest version of the Maven Dependency Plugin is 3.6.0. Using the latest version ensures access to the newest features and improvements.

Setting Up the Maven Dependency Plugin

To use the Maven Dependency Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a Maven project with the Dependency Plugin.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=dependency-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a simple Maven project with the following structure:

dependency-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven Dependency Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven Dependency Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>dependency-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.6.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: Specifies the group ID for the Maven Dependency Plugin.
  • <artifactId>: Specifies the Maven Dependency Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.

Step 3: Using the Maven Dependency Plugin

The Maven Dependency Plugin provides several goals that you can execute to manage and analyze your project dependencies.

3.1 Analyze Dependencies

Run the following command to analyze your project dependencies and identify unused declared and used undeclared dependencies:

mvn dependency:analyze

3.2 List Dependencies

To list all dependencies of your project, run the following command:

mvn dependency:list

3.3 Copy Dependencies

To copy project dependencies to a specific directory, use the following command:

mvn dependency:copy-dependencies -DoutputDirectory=target/dependencies

3.4 Unpack Dependencies

To unpack project dependencies, use the following command:

mvn dependency:unpack -DoutputDirectory=target/dependencies-unpacked

3.5 Resolve Dependencies

To resolve project dependencies and display the version used, run the following command:

mvn dependency:resolve

Step 4: Running the Commands

Let's run the commands we discussed to see how they work. Navigate to the project directory and execute each command to get the respective outputs.

Running mvn dependency:analyze

mvn dependency:analyze

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.example:dependency-plugin-demo >----------------
[INFO] Building dependency-plugin-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.6.0:analyze (default-cli) @ dependency-plugin-demo ---
[INFO] Used undeclared dependencies found:
[INFO]    none
[INFO] Unused declared dependencies found:
[INFO]    none

Running mvn dependency:list

mvn dependency:list

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.example:dependency-plugin-demo >----------------
[INFO] Building dependency-plugin-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.6.0:list (default-cli) @ dependency-plugin-demo ---
[INFO] The following files have been resolved:
[INFO]    none

Running mvn dependency:copy-dependencies

mvn dependency:copy-dependencies -DoutputDirectory=target/dependencies

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.example:dependency-plugin-demo >----------------
[INFO] Building dependency-plugin-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.6.0:copy-dependencies (default-cli) @ dependency-plugin-demo ---
[INFO] Copying dependencies to target/dependencies
...

Running mvn dependency:unpack

mvn dependency:unpack -DoutputDirectory=target/dependencies-unpacked

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.example:dependency-plugin-demo >----------------
[INFO] Building dependency-plugin-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.6.0:unpack (default-cli) @ dependency-plugin-demo ---
[INFO] Unpacking dependencies to target/dependencies-unpacked
...

Running mvn dependency:resolve

mvn dependency:resolve

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.example:dependency-plugin-demo >----------------
[INFO] Building dependency-plugin-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.6.0:resolve (default-cli) @ dependency-plugin-demo ---
[INFO] Resolving dependencies...
...

Complete Example

Here is the complete pom.xml file for the example project with the Maven Dependency Plugin configured:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>dependency-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.6.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Conclusion

The Maven Dependency Plugin is an invaluable tool for managing and analyzing project dependencies. This guide provided a complete overview of setting up and using the Maven Dependency Plugin, along with practical examples. By integrating this plugin into your Maven workflow, you can efficiently manage your project's dependencies and resolve any issues that may arise.

Comments