Maven工程经过pom.xml里的<dependency>来定义依赖项。固然,咱们不会少定义依赖项,不然编译不经过。不过,若是咱们多定义了依赖项虽然不会形成灾难,但可能会形成一些问题,好比:html
所以,咱们但愿在pom.xml里定义的依赖项很少很多,而且其做用域(<scope>)也恰到好处,恰好知足本工程的须要。问题是该如何进行检查呢?答案是使用maven-dependency-plug插件。java
maven-dependency-plugin插件简介spring
maven-dependency-plugin用于管理依赖项,提供了多个可执行的goal,在此咱们只关心其中的analyze和tree两个。express
下面是dependency:analyze的一个输出样例片段:apache
>mvn dependency:analyze
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ wtp-core --- [WARNING] Used undeclared dependencies found: [WARNING] org.springframework:spring-beans:jar:3.2.3.RELEASE:compile [WARNING] Unused declared dependencies found: [WARNING] junit:junit:jar:4.7:test [WARNING] org.springframework:spring-test:jar:3.2.3.RELEASE:test [WARNING] org.slf4j:jcl-over-slf4j:jar:1.6.1:runtime [WARNING] org.slf4j:slf4j-log4j12:jar:1.6.1:runtime [WARNING] commons-lang:commons-lang:jar:2.5:test
官方文档里说,dependency:analyze是经过分析bytecode来输出报告。上面的输出有两部分,一是Used undeclared dependencies(使用但未定义的依赖项),二是Unused declared dependencies(未使用但却定义的依赖项)。api
再说说dependency:tree,下面是一个输出样例片段:oracle
>mvn dependency:tree
com.wisetop.wtp:wtp-core:jar:2.2.1-SNAPSHOT +- junit:junit:jar:4.7:test +- org.springframework:spring-test:jar:3.2.3.RELEASE:test +- org.slf4j:slf4j-api:jar:1.6.1:compile +- org.slf4j:jcl-over-slf4j:jar:1.6.1:runtime +- org.slf4j:slf4j-log4j12:jar:1.6.1:runtime | \- log4j:log4j:jar:1.2.16:runtime +- org.springframework:spring-core:jar:3.2.3.RELEASE:compile | \- commons-logging:commons-logging:jar:1.1.1:provided +- org.springframework:spring-context:jar:3.2.3.RELEASE:compile | +- org.springframework:spring-aop:jar:3.2.3.RELEASE:compile | +- org.springframework:spring-beans:jar:3.2.3.RELEASE:compile | \- org.springframework:spring-expression:jar:3.2.3.RELEASE:compile +- org.springframework:spring-tx:jar:3.2.3.RELEASE:compile | \- aopalliance:aopalliance:jar:1.0:compile +- org.springframework:spring-context-support:jar:3.2.3.RELEASE:compile +- commons-beanutils:commons-beanutils:jar:1.8.3:compile +- dom4j:dom4j:jar:1.6.1:compile | \- xml-apis:xml-apis:jar:1.0.b2:compile +- com.wisetop.extra.oracle:wt-oracle-xdb6:jar:11.2.0.3:compile | \- com.wisetop.extra.oracle:wt-oracle-ojdbc6:jar:11.2.0.3:compile +- commons-lang:commons-lang:jar:2.5:test +- org.quartz-scheduler:quartz:jar:1.7.3:compile +- commons-codec:commons-codec:jar:1.4:compile \- javax.servlet:javax.servlet-api:jar:3.0.1:provided
该命令列出了各依赖项、以及传递出来的依赖项。经过此命令咱们能够识别出依赖项来自何处,特别地,若是你只关心某个特定的依赖项,好比上面的log4j是怎么出来的,你可加上过滤条件:dom
>mvn dependency:tree -Dincludes=log4j:log4j [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ wtp-core --- [INFO] com.wisetop.wtp:wtp-core:jar:2.2.1-SNAPSHOT [INFO] \- org.slf4j:slf4j-log4j12:jar:1.6.1:runtime [INFO] \- log4j:log4j:jar:1.2.16:runtime
固然,你能够简写为:maven
>mvn dependency:tree -Dincludes=*:log4j
其输出是同样的。ide
另外,一个依赖项可能来自多处,你可加上"-Dverbose"参数进行查看。例如,下面的命令将显示spring-core可来自多处:
>mvn dependency:tree -Dincludes=*:spring-core -Dverbose [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ wtp-core --- [INFO] com.wisetop.wtp:wtp-core:jar:2.2.1-SNAPSHOT [INFO] +- org.springframework:spring-test:jar:3.2.3.RELEASE:test [INFO] | \- (org.springframework:spring-core:jar:3.2.3.RELEASE:test - omitted for duplicate) [INFO] +- org.springframework:spring-core:jar:3.2.3.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:3.2.3.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:3.2.3.RELEASE:compile [INFO] | | \- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate) [INFO] | +- org.springframework:spring-beans:jar:3.2.3.RELEASE:compile [INFO] | | \- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate) [INFO] | +- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate) [INFO] | \- org.springframework:spring-expression:jar:3.2.3.RELEASE:compile [INFO] | \- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate) [INFO] +- org.springframework:spring-tx:jar:3.2.3.RELEASE:compile [INFO] | \- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate) [INFO] \- org.springframework:spring-context-support:jar:3.2.3.RELEASE:compile [INFO] \- (org.springframework:spring-core:jar:3.2.3.RELEASE:compile - omitted for duplicate)
该输出显示,咱们在pom.xml里直接依赖了spring-core,而且告诉咱们若是咱们不直接依赖spring-core的话则它也可能来自何处。
关于更多使用方法请参见dependency:tree的说明文档,以及过滤依赖树。
使用maven-dependency-plugin对pom.xml文件进行优化
上面说到,优化的目标是很少很多地定义依赖项。不只依赖项的个数要很少很多,并且依赖项的做用域也要恰到好处。其验证方法是:
须要特别说明的是: