优化依赖

在软件开发过程中,程序员会通过重构等方式不断地优化自己的代码,使其变得更简洁,更灵活。同理,程序员也应该能够对Maven项目的依赖了然于胸,并对其进行优化,如去除多余的依赖,显式地声明某些必要的依赖。

mvn dependency:list

通过阅读本章前面的内容,读者应该能够了解到:Maven会自动解析所有项目的直接依赖和传递性依赖,并且根据规则正确判断每个依赖的范围,对于一些依赖冲突,也能进行调节,以确保任何一个构件只有唯一的版本在依赖中存在。在这些工作之后,最后得到的那此依赖被称为已解析依赖(Resolved Dependency)。可以运行如下的命令查看当前项目的已解析依赖:

mvn dependency:list

在一个项目中执行该命令,结果如下

$ mvn dependency:list
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building account-email 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ account-email ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    org.springframework:spring-beans:jar:2.5.6:compile
[INFO]    javax.mail:mail:jar:1.4.1:compile
[INFO]    javax.activation:activation:jar:1.1:compile
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    org.slf4j:slf4j-api:jar:1.3.1:compile
[INFO]    org.springframework:spring-context-support:jar:2.5.6:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    aopalliance:aopalliance:jar:1.0:compile
[INFO]    com.icegreen:greenmail:jar:1.3.1b:compile
[INFO]    org.springframework:spring-core:jar:2.5.6:compile
[INFO]    org.springframework:spring-context:jar:2.5.6:compile
[INFO]    junit:junit:jar:4.12:test
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.120 s
[INFO] Finished at: 2017-07-03T09:31:58+08:00
[INFO] Final Memory: 13M/217M
[INFO] ------------------------------------------------------------------------

mvn:dependency:tree

会显式项目中所有已解析依赖,同时,每个依赖的范围也得以明确标识。

在此基础上,还能进一步了解已解析依赖的信息。将直接在当前项目POM声明的依赖定义为顶层依赖,而这些顶层依赖的依赖则定义为第二层依赖,以此类推,有第三,第四层依赖。当这些依赖经Maven解析后,就会构成一个依赖树,通过这个依赖树就能很清楚地某个依赖是通过哪条传递路径引入的。可以运行如下命令查看当前项目的依赖树:

mvn:dependency:tree
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building account-email 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ account-email ---
[INFO] com.jianglei.account:account-email:jar:1.0.0-SNAPSHOT
[INFO] +- org.springframework:spring-context:jar:2.5.6:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-beans:jar:2.5.6:compile
[INFO] |  \- org.springframework:spring-core:jar:2.5.6:compile
[INFO] +- org.springframework:spring-context-support:jar:2.5.6:compile
[INFO] +- javax.mail:mail:jar:1.4.1:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- com.icegreen:greenmail:jar:1.3.1b:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.3.1:compile
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.123 s
[INFO] Finished at: 2017-07-03T09:10:26+08:00
[INFO] Final Memory: 13M/217M
[INFO] ------------------------------------------------------------------------

我们可以看到,虽然我们没有声明org.slf4j:slf4j-api:1.3.1这一依赖,但它还是经过com.icegreen:greenmail:1.3.1b成为了当前项目的传递依赖,而且其范围是compile

mvn:dependency:analyze

results matching ""

    No results matching ""