maven-dependency-plugin最大的用途是帮助分析项目依赖,dependency:list能够列出项目最终解析到的依赖列表,dependency:tree能进一步的描绘项目依赖树,dependency:analyze可以告诉你项目依赖潜在的问题,如果你有直接使用到的却未声明的依赖,该目标就会发出警告。maven-dependency-plugin还有很多目标帮助你操作依赖文件,例如dependency:copy-dependencies能将项目依赖从本地Maven仓库复制到某个特定的文件夹下面。
比如查看maven项目的依赖,我们可以用下面命令:
mvn dependency:tree
以Dubbo的 dubbo-demo-provider 为例,我们输入这个命令可以获得下面信息:
mvn dependency:tree
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.alibaba:dubbo-demo-provider:jar:2.5.4-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin is missing. @ com.alibaba:dubbo-parent:2.5.4-SNAPSHOT, /Users/ghj1976/project/github/alibaba/dubbo/pom.xml, line 379, column 12
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ com.alibaba:dubbo-parent:2.5.4-SNAPSHOT, /Users/ghj1976/project/github/alibaba/dubbo/pom.xml, line 365, column 12
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-deploy-plugin is missing. @ com.alibaba:dubbo-parent:2.5.4-SNAPSHOT, /Users/ghj1976/project/github/alibaba/dubbo/pom.xml, line 449, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dubbo-demo-provider 2.5.4-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ dubbo-demo-provider ---
[INFO] com.alibaba:dubbo-demo-provider:jar:2.5.4-SNAPSHOT
[INFO] +- com.alibaba:dubbo-demo-api:jar:2.5.4-SNAPSHOT:compile
[INFO] +- com.alibaba:dubbo:jar:2.5.4-SNAPSHOT:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.springframework:spring:jar:2.5.6.SEC03:compile
[INFO] +- org.javassist:javassist:jar:3.15.0-GA:compile
[INFO] +- org.jboss.netty:netty:jar:3.2.5.Final:compile
[INFO] +- org.apache.mina:mina-core:jar:1.1.7:compile
[INFO] +- org.glassfish.grizzly:grizzly-core:jar:2.1.4:compile
[INFO] | +- org.glassfish.grizzly:grizzly-framework:jar:2.1.4:compile
[INFO] | | \- org.glassfish.gmbal:gmbal-api-only:jar:3.0.0-b023:compile
[INFO] | | \- org.glassfish.external:management-api:jar:3.0.0-b012:compile
[INFO] | +- org.glassfish.grizzly:grizzly-portunif:jar:2.1.4:compile
[INFO] | \- org.glassfish.grizzly:grizzly-rcm:jar:2.1.4:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.1.2:compile
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.1.2:compile
[INFO] | \- commons-codec:commons-codec:jar:1.4:compile
[INFO] +- com.alibaba:fastjson:jar:1.1.39:compile
[INFO] +- com.thoughtworks.xstream:xstream:jar:1.4.1:compile
[INFO] | +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] | \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] +- org.apache.bsf:bsf-api:jar:3.1:compile
[INFO] +- org.apache.zookeeper:zookeeper:jar:3.3.3:compile
[INFO] | \- jline:jline:jar:0.9.94:compile
[INFO] +- com.github.sgroschupf:zkclient:jar:0.1:compile
[INFO] +- com.netflix.curator:curator-framework:jar:1.1.16:compile
[INFO] | +- com.netflix.curator:curator-client:jar:1.1.16:compile
[INFO] | \- com.google.guava:guava:jar:11.0.1:compile
[INFO] | \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] +- com.googlecode.xmemcached:xmemcached:jar:1.3.6:compile
[INFO] +- org.apache.cxf:cxf-rt-frontend-simple:jar:2.6.1:compile
[INFO] | +- org.apache.cxf:cxf-api:jar:2.6.1:compile
[INFO] | | +- org.codehaus.woodstox:woodstox-core-asl:jar:4.1.2:runtime
[INFO] | | | \- org.codehaus.woodstox:stax2-api:jar:3.1.1:runtime
[INFO] | | +- org.apache.ws.xmlschema:xmlschema-core:jar:2.0.2:compile
[INFO] | | +- org.apache.geronimo.specs:geronimo-javamail_1.4_spec:jar:1.7.1:compile
[INFO] | | \- wsdl4j:wsdl4j:jar:1.6.2:compile
[INFO] | +- org.apache.cxf:cxf-rt-core:jar:2.6.1:compile
[INFO] | | \- com.sun.xml.bind:jaxb-impl:jar:2.1.13:compile
[INFO] | \- org.apache.cxf:cxf-rt-bindings-soap:jar:2.6.1:compile
[INFO] | \- org.apache.cxf:cxf-rt-databinding-jaxb:jar:2.6.1:compile
[INFO] +- org.apache.cxf:cxf-rt-transports-http:jar:2.6.1:compile
[INFO] +- org.apache.thrift:libthrift:jar:0.8.0:compile
[INFO] | \- commons-lang:commons-lang:jar:2.5:compile
[INFO] +- com.caucho:hessian:jar:4.0.7:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] +- org.mortbay.jetty:jetty:jar:6.1.26:compile
[INFO] | +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] | \- org.mortbay.jetty:servlet-api:jar:2.5-20081211:compile
[INFO] +- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.2:compile
[INFO] +- redis.clients:jedis:jar:2.1.0:compile
[INFO] | \- commons-pool:commons-pool:jar:1.5.5:compile
[INFO] +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.2.0.Final:compile
[INFO] +- javax.cache:cache-api:jar:0.4:compile
[INFO] +- junit:junit:jar:4.10:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.1:test
[INFO] +- org.easymock:easymock:jar:3.0:test
[INFO] | +- cglib:cglib-nodep:jar:2.2:test
[INFO] | \- org.objenesis:objenesis:jar:1.2:test
[INFO] +- org.easymock:easymockclassextension:jar:3.0:test
[INFO] \- com.googlecode.jmockit:jmockit:jar:0.999.8:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.057 s
[INFO] Finished at: 2016-03-29T15:41:31+08:00
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------
unpack和copy类似,只不过它会把拷来的包解开,例如:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${project.parent.version}</version>
<outputDirectory>${project.build.directory}/dubbo</outputDirectory>
<includes>META-INF/assembly/**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
…
dependency:unpack 中常用参数有:
- artifactItems:用来包含声明需要解压的文件元素;
- artifactItem:声明需要解压的文件;
- outputDirectory:定义解压后输出的文件夹;
- includes:定义输出包含的规则;
- excludes:定义输出排除的规则。
Apache Maven Dependency Plugin中比较常用的功能有:
- dependency:copy 用来拷贝某一个或多个特定文件到指定目录;
- dependency:copy-dependencies 用来拷贝依赖的文件到指定目录;
- dependency:unpack 与 copy 类似,但会解压文件;
- dependency:unpack-dependencies 与 copy-dependencies 类似,但会解压文件;
参考资料:
使用maven命令来分析jar包之间的依赖关系
http://outofmemory.cn/code-snippet/10914/maven-command-dependency