Getting a report of dependencies

It is useful to get a list of dependencies for a Maven project in order to identify and troubleshoot problems. The Maven Dependency plugin helps us here.

Getting ready

To appreciate this, we need to have a fairly complex project that has several transitive dependencies. You can look at one such project at https://github.com/selendroid/demoproject-selendroid.

The project source code is available as a Git repository. A Git repository is typically downloaded by cloning it. To do this, install Git on your system. Refer to the Git setup link (https://help.github.com/articles/set-up-git/) for detailed instructions.

How to do it...

Use the following steps, to get a list of dependencies for a Maven project:

  1. Clone the open source project demoproject-selendroid.
  2. Run the following Maven goal:
    mvn dependency:list
    
  3. Observe the output as shown in the following screenshot:
    How to do it...

How it works...

The Maven Dependency plugin has several goals to obtain information regarding dependencies as well as manage them.

Interestingly, this goal is not bound to any phase or lifecycle of Maven. This means, when the mvn dependency:list command is run, Maven runs the list goal and nothing else.

The dependency:list parameter tells Maven to run the list goal of the Maven Dependency plugin. This analyzes the pom file and generates a list of dependencies for the project.

There's more...

While the dependency list might be good enough for simple cases, a more interesting and useful one would be a dependency report in a tree format.

This can be done by running the tree goal of Maven Dependency plugin:

mvn dependency:tree

Running this on the preceding project gives the following output:

There's more...

As we can see, this is a better representation of the transitivity of the dependencies. Now we know that which other dependencies are used, for instance, commons-logging.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.57