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

落花浮王杯 提交于 2019-11-30 17:49:40

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" "http://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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!