How to list the transitive dependencies of an artifact from a repository?

Sled picture Sled · Mar 12, 2014 · Viewed 8.7k times · Source

I was wondering: is there a way using the Maven Dependency plug-in (or something else on the command-line), to list the transitive dependencies of an artifact from a repository (ie not in my pom.xml)?

I know can use mvn dependency:tree to list the dependencies of my local project, but I want to know the dependencies of something before I add it to my project (ie making an informed decision), and add dependencies to a local pom.xml for the sole purpose of the dependency plug-in seems hackish.

I was hoping I could just run something like:

mvn dependency:tree "-DgroupId=net.jawr" "-DartifactId=jawr-core" "-Dversion=3.5"

The best I've come up with is:

  1. delete my local repository (ie ~/.m2/repository)
  2. run mvn dependency:get "-DgroupId=net.jawr" "-DartifactId=jawr-core" "-Dversion=3.5"
  3. examine the output

but that seems very hackish and wasteful.

PS - I don't care if it's in tree format or not.

Answer

izstas picture izstas · Mar 14, 2014

I am not aware of any native way to do this with Maven, however, I came up with the following shell script which seems to do the job:

#!/bin/sh
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <groupId> <artifactId> <version>"
  exit
fi

POM_DIR="`echo "$1" | tr . /`/$2/$3"
POM_PATH="$POM_DIR/$2-$3.pom"

mkdir -p "$HOME/.m2/repository/$POM_DIR"
wget -q -O "$HOME/.m2/repository/$POM_PATH" "https://repo.maven.apache.org/maven2/$POM_PATH"
mvn -f "$HOME/.m2/repository/$POM_PATH" dependency:tree

This scripts downloads the POM for the artifact you specify from Maven Central Repository and then runs dependency:tree goal for that POM. Example:

izstas@izstas-PC:~$ ./deptree net.jawr jawr-core 3.5
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Java web resources bundling and compression 3.5
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ jawr-core ---
[INFO] net.jawr:jawr-core:jar:3.5
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.1:provided
[INFO] +- org.mozilla:rhino:jar:1.7R4:provided
[INFO] +- com.yahoo.platform.yui:yuicompressor:jar:2.4.7:provided
[INFO] +- javax.faces:jsf-api:jar:1.1_02:provided
[INFO] +- commons-validator:commons-validator:jar:1.2.0:provided
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.7.0:provided
[INFO] |  +- commons-digester:commons-digester:jar:1.6:provided
[INFO] |  |  \- commons-collections:commons-collections:jar:2.1:provided
[INFO] |  +- commons-logging:commons-logging:jar:1.0.4:provided
[INFO] |  +- oro:oro:jar:2.0.8:provided
[INFO] |  \- xml-apis:xml-apis:jar:1.0.b2:provided
[INFO] +- taglibs:standard:jar:1.1.2:provided
[INFO] +- net.sf.ehcache:ehcache:jar:2.7.4:provided
[INFO] +- com.carrotsearch:smartsprites:jar:0.2.10:provided
[INFO] |  +- com.google.guava:guava:jar:14.0.1:provided
[INFO] |  +- args4j:args4j:jar:2.0.16:provided
[INFO] |  +- org.apache.commons:commons-math3:jar:3.0:provided
[INFO] |  +- commons-io:commons-io:jar:2.4:provided
[INFO] |  \- org.apache.commons:commons-lang3:jar:3.1:provided
[INFO] +- com.google.javascript:closure-compiler:jar:v20131014:provided
[INFO] |  +- com.google.protobuf:protobuf-java:jar:2.4.1:provided
[INFO] |  +- org.json:json:jar:20090211:provided
[INFO] |  \- com.google.code.findbugs:jsr305:jar:1.3.9:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:provided
[INFO] +- log4j:log4j:jar:1.2.17:provided
[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] \- org.mockito:mockito-all:jar:1.9.5:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.756s
[INFO] Finished at: Fri Mar 14 21:00:41 MSK 2014
[INFO] Final Memory: 10M/181M
[INFO] ------------------------------------------------------------------------

(I probably could strip off unnecessary Maven output if I wanted to)

This, however, appears to download .jar files for dependencies, so I can't say it's a clean solution. But at least it doesn't require you to purge your local repository.