溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

深入理解Maven的坐標(biāo)與依賴

發(fā)布時(shí)間:2020-10-16 19:01:21 來源:腳本之家 閱讀:236 作者:溫柔狠角色 欄目:編程語言

在前邊兩節(jié)中,我們學(xué)習(xí)了Maven的基本概念以及何為Maven倉庫,并且如何配置settings.xml文件等相關(guān)知識(shí)點(diǎn)。Maven的主要作用是可以幫助我們自動(dòng)下載在pom.xml中配置添加的依賴。那么在本節(jié)中,我們將學(xué)習(xí)如何引入依賴。

知識(shí)點(diǎn)包括:

Maven的坐標(biāo),Maven的依賴配置,依賴范圍,傳遞性依賴,依賴調(diào)解,可選依賴,排除依賴,歸類依賴和優(yōu)化依賴

Maven的坐標(biāo)

Maven的倉庫中擁有著無數(shù)的構(gòu)件,每一個(gè)構(gòu)件都是一個(gè)jar或者war等文件,如果沒有坐標(biāo),那么我們將無法唯一標(biāo)識(shí)該構(gòu)件,結(jié)果就是Maven將無法幫助我們自動(dòng)下載構(gòu)件。所以,Maven定義了一組規(guī)則:世界上任何一個(gè)構(gòu)件都可以使用Maven坐標(biāo)來唯一標(biāo)識(shí)。Maven坐標(biāo)的主要元素包括groupId,artifactId,version,packaging(可選)和classifier(可選),通過這些元素,我們可以明確標(biāo)識(shí)任何一個(gè)構(gòu)件。

groupId:該元素定義了當(dāng)前Maven項(xiàng)目隸屬的實(shí)際項(xiàng)目,一般情況下該項(xiàng)元素都與公司域名相對(duì)應(yīng),比如com.taobao.

artifactId:該元素定義了實(shí)際項(xiàng)目中的一個(gè)Maven Module

version:該元素表示當(dāng)前構(gòu)件的版本,包括穩(wěn)定(release)版本和測試(snapshot)版本

packaging:該元素定義Maven項(xiàng)目的打包方式,默認(rèn)為jar,還有war和pom方式

classifer:該元素用來幫助定義構(gòu)件輸出的一些附屬構(gòu)件,例如通過配置插件,可以在打包的同時(shí)生成-javadoc.jar和sources.jar等構(gòu)件。

示例如下:

<groupId>com.baidu</groupId>
<artifactId>passport-agent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<package>jar</package>
<classifier>jdk15-javadoc</classifier>

Maven的依賴

依賴配置

上邊生成的jar包為passport-agent-0.0.1-SNAPSHOT-jdk15-javadoc.jar,若其它項(xiàng)目中需要依賴該jar包,那么需要引入的配置如下:

<dependency>
 <groupId>com.baidu</groupId>
 <artifactId>passport-agent</artifactId>
 <version>0.0.1-SNAPSHOT</version> 
 <classifier>jdk15-javadoc</classifier>
</dependency>

上邊的配置<dependency>標(biāo)簽其實(shí)就是在將我們項(xiàng)目中所需要的依賴配置在pom.xml,標(biāo)簽已經(jīng)定義好了該構(gòu)件的坐標(biāo),那么Maven會(huì)根據(jù)該配置從倉庫中下載構(gòu)件。既然學(xué)會(huì)了如何在項(xiàng)目中配置引入依賴,那么我們接下來說說依賴相關(guān)的事兒吧。

依賴范圍

Maven有如下6種依賴范圍:

  1. compile: 編譯依賴范圍(Default,大多數(shù)情況下我們都是在使用compile編譯范圍)
  2. test: 測試依賴范圍 (編譯主代碼和運(yùn)行時(shí)無效)
  3. provided: 已提供依賴范圍(就是說在運(yùn)行的時(shí)候容器已經(jīng)給我們提供該依賴了,比如說servlet-api)
  4. runtime: 運(yùn)行時(shí)依賴范圍
  5. system: 系統(tǒng)依賴范圍(生成的構(gòu)建一般與本機(jī)系統(tǒng)綁定,不具備移植性不建議使用)
  6. import: 導(dǎo)入依賴范圍(將其它地方官依賴配置導(dǎo)入,后續(xù)講到依賴管理dependencyManagement詳細(xì)闡述)

我們以表格來說明下各個(gè)依賴的生效范圍:

依賴范圍

對(duì)于編譯有效

對(duì)于測試有效

對(duì)于運(yùn)行有效

compile

Y

Y

Y

test

Y

Y

N

provided

Y

Y

N

runtime

N

Y

Y

system

Y

Y

N

傳遞性依賴

傳遞性依賴的意思是依賴具有傳遞性。比如,在A 中添加對(duì)B 的依賴,在B 中添加對(duì)C 的依賴,如果依賴范圍是compile 的,A 不僅會(huì)有B 的jar 包,也會(huì)有C 的jar 包。如果在C 中添加了某個(gè)依賴,那么根據(jù)傳遞性,A 和B 也可以使用C添加的依賴,而不需要自己再重新引入依賴。 我們使用公式來表示依賴的傳遞性:

 A->B并且B->C,那么A->C,也就是C是一個(gè)A的傳遞性依賴

依賴調(diào)解

依賴調(diào)解是指當(dāng)存在多個(gè)傳遞性依賴時(shí),出現(xiàn)了當(dāng)前項(xiàng)目對(duì)于同一個(gè)依賴有多個(gè)版本被引入依賴樹中該如何選擇的原則。

比如說存在以下情況:

存在:A->-B>-C->X(1.0)和A->D->X(2.0)

原則:路徑最近原則(指依賴通過幾層傳遞性依賴引入),X(2.0)將會(huì)被解析

存在:A->B->Y(1.0)和A->C->Y(2.0)

原則:第一聲明優(yōu)先原則,哪個(gè)現(xiàn)在pom中聲明(也就是在pom文件的上邊),就以哪個(gè)為準(zhǔn)(Maven2.0.9開始使用,在此之前是不確定的,導(dǎo)致構(gòu)建項(xiàng)目具有一定的隨機(jī)性)

可選依賴

A->B,并且B->X(可選),B->Y(可選),那么X,Y將不會(huì)對(duì)A有任何影響。

可選依賴使用<optional>true</optional>設(shè)置??蛇x依賴違反了單一職責(zé)的原則,一般不建議使用。

排除依賴

我們通過在pom中配置<dependency></dependency>來引入依賴,但是該依賴存在多個(gè)傳遞性依賴,如果某個(gè)間接依賴不是我們需要的,影響到了我們項(xiàng)目的正常構(gòu)建,那么我們可以使用<exclusions><exclusion></exclusion></exclusions>來干掉它。示例如下:

 <dependency>
  <groupId>com.xx.miliao</groupId>
  <artifactId>xx-serviceapi</artifactId>
  <exclusions>
  <exclusion>
   <artifactId>xx-thrift-micloud-common</artifactId>
   <groupId>com.xx</groupId>
  </exclusion>
  <exclusion>
   <artifactId>thrift</artifactId>
   <groupId>org.apache.thrift</groupId>
  </exclusion>
  <exclusion>
   <groupId>com.xx</groupId>
   <artifactId>ipWrapper</artifactId>
  </exclusion>
  </exclusions>
 </dependency>

在上邊的配置中,我們引入了xx-serviceapi的依賴,但是我們將該依賴所引入的org.apache.thrift和ipWrapper依賴都給排除掉了,這兩個(gè)依賴將不會(huì)出現(xiàn)在我們構(gòu)建的項(xiàng)目中。

歸類依賴

歸類依賴看著高大上,其實(shí)說白了就是為了統(tǒng)一管理依賴,如果某些依賴的version都是一致的或者是存在某些特殊的關(guān)系,我們可以在pom中使用<properties></properties>配置一些變量,在下邊的時(shí)候使用$變量名來搞定。

優(yōu)化依賴

優(yōu)化依賴的意思是通過優(yōu)化,使得我們的項(xiàng)目對(duì)于引入的依賴優(yōu)化一點(diǎn)^_^,比如說去除多余的依賴等操作。那么我們?nèi)绾螌?shí)現(xiàn)呢?這個(gè)時(shí)候我們的插件(后邊詳細(xì)介紹)maven-dependency-plugin該上場了。不知各位是否還記得我們前面所說的超級(jí)Pom文件,在該文件中,定義了該插件(見下圖),所以我們?cè)趍aven項(xiàng)目中可以直接使用該dependency插件。

深入理解Maven的坐標(biāo)與依賴

我們主要使用該插件的三個(gè)任務(wù)(goal):list,tree, anaylze(何為插件,何為插件goal我們后邊詳細(xì)街上,此處只要知道插件可以用來幫你干活就OK了哈)

我們先來看一下這條命令mvn dependency:list的執(zhí)行結(jié)果吧。

深入理解Maven的坐標(biāo)與依賴

深入理解Maven的坐標(biāo)與依賴

這條命令的執(zhí)行結(jié)果告訴你當(dāng)前項(xiàng)目passport-common中引入的依賴有哪些,并且直接列出。但是其實(shí)這樣的結(jié)果對(duì)于我們程序員來說意義并不是很大,只是一些簡單的依賴羅列,看不出某個(gè)具體的依賴是直接還是被間接引入本項(xiàng)目的。這個(gè)時(shí)候我們需要使用mvn dependency:tree 將依賴以樹的形式展示出來。結(jié)果如下:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ passport-common ---
[INFO] com.xxx:passport-common:jar:1.0.25-SNAPSHOT
[INFO] +- com.xxx.miliao:miliao-serviceapi:jar:1.1.5-PASSPORT:compile
[INFO] | +- com.rabbitmq:amqp-client:jar:2.4.1:compile
[INFO] | | \- commons-cli:commons-cli:jar:1.1:compile
[INFO] | +- com.xxx:xxx-common-mq:jar:2.0.3:compile
[INFO] | +- com.xxx:messaging-api:jar:1.0.1:compile
[INFO] | | \- com.xxx:xxx-thrift-messaging:jar:1.0.1:compile
[INFO] | +- com.xxx:xxx-thrift-api:jar:1.6.35:compile
[INFO] | | \- com.xxx:xxx-thrift-sns:jar:0.0.1:compile
[INFO] | +- com.xxx:xxx-thrift-vip:jar:0.0.3:compile
[INFO] | +- com.xxx:xxx-thrift-newsfeed:jar:0.0.3:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.springframework:spring:jar:2.5.6.SEC03:compile
[INFO] | +- org.springframework:spring-webmvc:jar:2.5.6.SEC03:compile
[INFO] | | +- org.springframework:spring-beans:jar:2.5.6.SEC03:compile
[INFO] | | +- org.springframework:spring-context:jar:2.5.6.SEC03:compile
[INFO] | | +- org.springframework:spring-context-support:jar:2.5.6.SEC03:compile
[INFO] | | +- org.springframework:spring-core:jar:2.5.6.SEC03:compile
[INFO] | | \- org.springframework:spring-web:jar:2.5.6.SEC03:compile
[INFO] | +- net.paoding:paoding-rose:jar:1.1.1:compile
[INFO] | | +- javax.persistence:persistence-api:jar:1.0:compile
[INFO] | | +- commons-fileupload:commons-fileupload:jar:1.2.1:compile
[INFO] | | +- org.apache.velocity:velocity:jar:1.6.2:compile
[INFO] | | +- net.paoding:paoding-rose-scanning:jar:1.1.1:compile
[INFO] | | \- org.apache.velocity:velocity-tools:jar:1.3:compile
[INFO] | +- com.basho.riak:riak-client:jar:1.1.0:compile
[INFO] | | +- com.basho.riak.protobuf:riak-pb:jar:1.2.1:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.1.2:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-core:jar:2.1.2:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-databind:jar:2.1.2:compile
[INFO] | +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] | +- kafka:kafka:jar:0.7.6:compile
[INFO] | +- org.scala-lang:scala-library:jar:2.8.1:compile
[INFO] | +- com.senseidb:sensei-java-client:jar:2.0.0-SNAPSHOT:compile
[INFO] | +- voldemort:voldemort:jar:0.90.1:compile
[INFO] | +- com.google.guava:guava:jar:16.0.1:compile
[INFO] | +- redis.clients:jedis:jar:2.1.0:compile
[INFO] | +- com.xxx:xxx-thrift-hotspots:jar:1.0-SNAPSHOT:compile
[INFO] | +- IKAnalyzer:IKAnalyzer:jar:4.0.6:compile
[INFO] | +- org.apache.lucene:lucene-core:jar:3.5.0:compile
[INFO] | +- commons-lang:commons-lang:jar:2.4:compile
[INFO] | +- com.xxx.miliao:vip-shared:jar:0.0.8:compile
[INFO] | \- com.xxx.miliao:newsfeed-shared:jar:0.0.1:compile
[INFO] +- com.xxx.miliao:accessTrack:jar:0.0.1:compile
[INFO] | +- com.xxx.channel:scribe-log4j:jar:0.0.1:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.6.0:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | \- com.xxx:xxx-common-logger:jar:2.0.3:compile
[INFO] +- com.xxx:xxx-common-thrift:jar:2.7.7-beta:compile
[INFO] | +- com.xxx.common.perfcounter:xxx-common-perfcounter:jar:2.6.21:compile
[INFO] | | +- org.aspectj:aspectjrt:jar:1.8.4:compile
[INFO] | | \- org.aspectj:aspectjweaver:jar:1.8.4:compile
[INFO] | +- commons-collections:commons-collections:jar:3.2:compile
[INFO] | +- com.xxx:xxx-thrift-shared:jar:2.0.3:compile
[INFO] | +- com.xxx:xxx-thrift-scribe:jar:2.0.3:compile
[INFO] | +- com.xxx:xtrace-client:jar:1.0.43:compile
[INFO] | | +- com.xxx:xtrace-base:jar:1.0.43:compile
[INFO] | | | \- com.xxx:xtrace-thrift:jar:1.0.43:compile
[INFO] | | \- com.lmax:disruptor:jar:3.3.0:compile
[INFO] | +- com.xxx:xtrace-common:jar:1.0.43:compile
[INFO] | +- commons-pool:commons-pool:jar:1.6:compile
[INFO] | +- org.apache.commons:cli:jar:1.2:compile
[INFO] | +- com.yammer.metrics:metrics-core:jar:2.2.0:compile
[INFO] | \- commons-validator:commons-validator:jar:1.5.1:compile
[INFO] +- com.googlecode.jmockit:jmockit:jar:1.2:test
[INFO] +- commons-io:commons-io:jar:2.4:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.4:compile
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] | +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | \- commons-codec:commons-codec:jar:1.4:compile
[INFO] +- com.xxx:passport-security:jar:0.0.19-SNAPSHOT:compile
[INFO] | +- com.xxx:passport-security-core:jar:3.3.19:compile
[INFO] | \- org.apache.thrift:thrift:jar:0.5.0-mdf1.1.6-beta-passport:compile
[INFO] +- com.xxx:passport-service-api:jar:0.3.6:compile
[INFO] +- cglib:cglib:jar:2.2.2:compile
[INFO] | \- asm:asm:jar:3.3.1:compile
[INFO] +- commons-net:commons-net:jar:3.3:compile
[INFO] +- com.xxx:passport-sso-conf:jar:0.0.5:compile
[INFO] | \- com.xxx:keycenter-client:jar:2.0.7:compile
[INFO] | +- com.xxx:keycenter-common:jar:2.0.7:compile
[INFO] | +- org.apache.commons:org.apache.commons.collections:jar:3.2.1:compile
[INFO] | \- org.xerial.snappy:snappy-java:jar:1.0.4.1:compile
[INFO] +- com.xxx:localization:jar:1.0.5-SNAPSHOT:compile
[INFO] | +- com.xxx.passport:passport-commons-region:jar:1.0.1:compile
[INFO] | \- com.ibm.icu:icu4j:jar:4.8:compile
[INFO] +- com.xxx:globalconf-lib:jar:1.0.9-SNAPSHOT:compile
[INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] | \- com.typesafe:config:jar:1.0.2:compile
[INFO] +- com.xxx:passportsdk:jar:3.3.27-SNAPSHOT:compile
[INFO] | +- com.xxx:xxx-common-legacy:jar:2.7.5:compile
[INFO] | \- com.xxx:passportsdk-basic:jar:3.3.27-SNAPSHOT:compile
[INFO] | +- it.unimi.dsi:fastutil:jar:6.5.9:compile
[INFO] | \- com.sun.grizzly:grizzly-http-utils:jar:1.9.2:compile
[INFO] +- com.xxx:passport-canal-redis:jar:1.0.0-SNAPSHOT:compile
[INFO] | +- com.xxx:passport-canal-common:jar:1.0.1:compile
[INFO] | | +- com.xxx:passport-canal-thrift:jar:1.0.1:compile
[INFO] | | +- com.xxx.passport:passport-commons-utility:jar:1.1.0:compile
[INFO] | | +- org.apache.commons:commons-pool2:jar:2.6.0:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.19:compile
[INFO] | +- biz.paluch.redis:lettuce:jar:3.5.0.Final:compile
[INFO] | | +- io.reactivex:rxjava:jar:1.1.6:compile
[INFO] | | +- io.netty:netty-common:jar:4.0.37.Final:compile
[INFO] | | +- io.netty:netty-transport:jar:4.0.37.Final:compile
[INFO] | | | \- io.netty:netty-buffer:jar:4.0.37.Final:compile
[INFO] | | \- io.netty:netty-handler:jar:4.0.37.Final:compile
[INFO] | | \- io.netty:netty-codec:jar:4.0.37.Final:compile
[INFO] | \- com.google.inject:guice:jar:4.2.0:compile
[INFO] | +- javax.inject:javax.inject:jar:1:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- com.xxx:passport-canal-talos:jar:1.0.0-SNAPSHOT:compile
[INFO] | \- com.xxx.infra.galaxy:galaxy-talos-sdk:jar:2.3.1:compile
[INFO] | +- com.xxx.infra.galaxy:galaxy-client-java:jar:1.2.5:compile
[INFO] | | \- com.fasterxml.uuid:java-uuid-generator:jar:3.1.3:compile
[INFO] | \- com.xxx.infra.galaxy:galaxy-thrift-api:jar:1.2.8:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] +- io.netty:netty-all:jar:4.1.1.Final:compile
[INFO] +- com.alibaba:fastjson:jar:1.2.47:compile
[INFO] +- junit:junit:jar:4.8.2:compile
[INFO] +- com.xxx:xxx-common-dal:jar:2.7.4-beta:compile
[INFO] | +- com.xxx:xxx-common-utils:jar:2.7.28:compile
[INFO] | +- mysql:mysql-connector-java:jar:5.1.20:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | \- net.sf:jsqlparser:jar:0.7.0:compile
[INFO] +- com.xxx:miliao-common:jar:0.0.2-SNAPSHOT:compile
[INFO] | +- com.xxx:xxx-common-xclient:jar:2.6.30:compile
[INFO] | +- org.apache.lucene:core:jar:3.0.3:compile
[INFO] | +- com.danga:java-memcached:jar:2.5.1.2-xxx:compile
[INFO] | +- javax.servlet:servlet-api:jar:2.4:compile
[INFO] | +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] | +- jmagick:jmagick:jar:6.40:compile
[INFO] | +- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] | +- net.sf.json-lib:json-lib:jar:jdk15:2.2.3:compile
[INFO] | +- org.json:json:jar:20090211:compile
[INFO] | +- joda-time:joda-time:jar:1.6:compile
[INFO] | +- oro:oro:jar:2.0.8:compile
[INFO] | +- commons-digester:commons-digester:jar:1.8:compile
[INFO] | \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- taglibs:standard:jar:1.1.2:compile
[INFO] +- org.apache.struts:struts-taglib:jar:1.3.10:compile
[INFO] | \- org.apache.struts:struts-core:jar:1.3.10:compile
[INFO] | +- antlr:antlr:jar:2.7.2:compile
[INFO] | \- commons-chain:commons-chain:jar:1.2:compile
[INFO] +- taglibs:string:jar:1.1.0:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] +- xerces:xercesImpl:jar:2.9.1:compile
[INFO] +- com.xxx:account-iptrack:jar:0.0.5:compile
[INFO] +- com.xxx.mfs.sdk:mfs-sdk:jar:1.2.2:compile
[INFO] | +- com.xxx.mfs.common:mfs-common:jar:1.2.6:compile
[INFO] | +- org.apache.maven.plugins:maven-compiler-plugin:maven-plugin:2.3.2:compile
[INFO] | | +- org.apache.maven:maven-plugin-api:jar:2.0.6:compile
[INFO] | | +- org.apache.maven:maven-artifact:jar:2.0.6:compile
[INFO] | | +- org.apache.maven:maven-core:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-settings:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-profile:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-model:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:compile
[INFO] | | | +- org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-error-diagnostics:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-plugin-descriptor:jar:2.0.6:compile
[INFO] | | | +- org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
[INFO] | | | \- org.apache.maven:maven-monitor:jar:2.0.6:compile
[INFO] | | +- org.apache.maven:maven-toolchain:jar:1.0:compile
[INFO] | | +- org.codehaus.plexus:plexus-utils:jar:2.0.5:compile
[INFO] | | +- org.codehaus.plexus:plexus-compiler-api:jar:1.8.1:compile
[INFO] | | +- org.codehaus.plexus:plexus-compiler-manager:jar:1.8.1:compile
[INFO] | | \- org.codehaus.plexus:plexus-compiler-javac:jar:1.8.1:runtime
[INFO] | +- org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.10:compile
[INFO] | | +- org.apache.maven.surefire:surefire-booter:jar:2.10:compile
[INFO] | | | \- org.apache.maven.surefire:surefire-api:jar:2.10:compile
[INFO] | | +- org.apache.maven.surefire:maven-surefire-common:jar:2.10:compile
[INFO] | | | \- org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile
[INFO] | | \- org.apache.maven:maven-project:jar:2.0.9:compile
[INFO] | | +- org.apache.maven:maven-plugin-registry:jar:2.0.9:compile
[INFO] | | \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
[INFO] | | \- classworlds:classworlds:jar:1.1-alpha-2:compile
[INFO] | \- org.apache.httpcomponents:httpmime:jar:4.2.5:compile
[INFO] \- com.xxx:xxx-common-zookeeper:jar:2.8.0:compile
[INFO] +- org.apache.zookeeper:zookeeper:jar:3.4.5-mdh2.2.2:compile
[INFO] | +- jline:jline:jar:0.9.94:compile
[INFO] | \- org.codehaus.jettison:jettison:jar:1.1:compile
[INFO] | \- stax:stax-api:jar:1.0.1:compile
[INFO] \- zkclient:zkclient:jar:0.2:compile
[INFO] \- org.apache.hadoop.zookeeper:zookeeper:jar:3.3.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.341 s
[INFO] Finished at: 2018-11-04T10:30:53+08:00
[INFO] Final Memory: 21M/226M
[INFO] ------------------------------------------------------------------------

在這個(gè)依賴樹中,開頭從最左邊開始的,說明這個(gè)依賴是被直接引入的依賴A,我們還可以看到該直接依賴A又給本項(xiàng)目引入了哪些間接依賴。依賴樹的好處就在于,當(dāng)我們想使用<exclusions><exclusion></exclusion></exclusions>標(biāo)簽將某些依賴排除掉時(shí),可以準(zhǔn)確的確定出該間接依賴所對(duì)應(yīng)的直接依賴。

那么,當(dāng)我們想進(jìn)一步優(yōu)化該項(xiàng)目的依賴呢?比如我們想看看哪些依賴是沒有被使用的?或者哪些依賴沒有顯示聲明配置,然而卻被直接調(diào)用呢?也就是優(yōu)化依賴,使得項(xiàng)目更加簡潔穩(wěn)定。這個(gè)時(shí)候我們需要使用mvn dependency:analyze 了,該命令可以分析項(xiàng)目中依賴的具體使用情況。結(jié)果如下:

[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ passport-common ---
[WARNING] Used undeclared dependencies found:
[WARNING] net.sf:jsqlparser:jar:0.7.0:compile
[WARNING] commons-validator:commons-validator:jar:1.5.1:compile
[WARNING] org.springframework:spring:jar:2.5.6.SEC03:compile
[WARNING] commons-collections:commons-collections:jar:3.2:compile
[WARNING] net.paoding:paoding-rose:jar:1.1.1:compile
[WARNING] commons-lang:commons-lang:jar:2.4:compile
[WARNING] com.xxx:xxx-common-legacy:jar:2.7.5:compile
[WARNING] org.apache.thrift:thrift:jar:0.5.0-mdf1.1.6-beta-passport:compile
[WARNING] javax.servlet:servlet-api:jar:2.4:compile
[WARNING] org.scala-lang:scala-library:jar:2.8.1:compile
[WARNING] net.sf.json-lib:json-lib:jar:jdk15:2.2.3:compile
[WARNING] com.xxx:xxx-common-xclient:jar:2.6.30:compile
[WARNING] org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[WARNING] commons-codec:commons-codec:jar:1.4:compile
[WARNING] com.xxx:passport-canal-common:jar:1.0.1:compile
[WARNING] com.xxx:passport-security-core:jar:3.3.19:compile
[WARNING] com.xxx:passportsdk-basic:jar:3.3.27-SNAPSHOT:compile
[WARNING] org.slf4j:slf4j-api:jar:1.7.25:compile
[WARNING] com.xxx:xxx-common-utils:jar:2.7.28:compile
[WARNING] com.xxx.common.perfcounter:xxx-common-perfcounter:jar:2.6.21:compile
[WARNING] org.json:json:jar:20090211:compile
[WARNING] commons-httpclient:commons-httpclient:jar:3.1:compile
[WARNING] Unused declared dependencies found:
[WARNING] com.xxx.miliao:accessTrack:jar:0.0.1:compile
[WARNING] com.xxx:passportsdk:jar:3.3.27-SNAPSHOT:compile
[WARNING] com.xxx:passport-canal-talos:jar:1.0.0-SNAPSHOT:compile
[WARNING] io.netty:netty-all:jar:4.1.1.Final:compile
[WARNING] com.xxx:miliao-common:jar:0.0.2-SNAPSHOT:compile
[WARNING] taglibs:standard:jar:1.1.2:compile
[WARNING] org.apache.struts:struts-taglib:jar:1.3.10:compile
[WARNING] taglibs:string:jar:1.1.0:compile
[WARNING] log4j:log4j:jar:1.2.17:compile
[WARNING] xerces:xercesImpl:jar:2.9.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.162 s
[INFO] Finished at: 2018-11-04T10:40:37+08:00
[INFO] Final Memory: 23M/360M
[INFO] ------------------------------------------------------------------------

分析結(jié)果中,將依賴分為了兩種,即Used undeclared dependencies found 和Unused declared dependencies found兩種

(1)Used undeclared dependencies found :使用了但是沒有顯示的聲明該依賴

這種情況對(duì)于我們的項(xiàng)目構(gòu)建是不利的,存在著潛在的風(fēng)險(xiǎn),這些依賴是通過間接依賴引入項(xiàng)目的,當(dāng)我們需要升級(jí)直接依賴的版本時(shí),可能會(huì)導(dǎo)致間接依賴的版本出現(xiàn)變動(dòng),從而影響到項(xiàng)目的構(gòu)建,所以我們需要顯示聲明任何項(xiàng)目中直接使用到的依賴。

(2)Unused declared dependencies found:聲明了該依賴但是并沒有被使用

出現(xiàn)這種情況也我們不能簡單的將該依賴的聲明和配置刪掉了事,應(yīng)該具體分析。因?yàn)閙vn dependency:analyze只會(huì)分析編譯主代碼和測試代碼需要用到的依賴,一些執(zhí)行測試和與運(yùn)行時(shí)需要的依賴它就分析不出來。所以需要具體分析項(xiàng)目依賴情況。

總結(jié)

這篇文章我們學(xué)習(xí)了maven的坐標(biāo)和依賴的相關(guān)知,結(jié)合前邊章節(jié)的學(xué)習(xí),我們知道了倉庫是用來存儲(chǔ)構(gòu)件(依賴)的,項(xiàng)目中需要的各種依賴通過Maven坐標(biāo)存在各個(gè)倉庫(本地倉庫和遠(yuǎn)程倉庫等)中。配置文件settings.xml和項(xiàng)目中的pom.xml都是對(duì)倉庫,依賴和插件進(jìn)行各種配置的,最終目的就是通過配置依賴,使得maven可以幫助我們自動(dòng)下載依賴,減去了我們需要去網(wǎng)站上的各個(gè)地方去手動(dòng)搜索和下載依賴的煩惱。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI