1. 程式人生 > >Maven的座標與依賴

Maven的座標與依賴

(尊重勞動成果,轉載請註明出處:https://blog.csdn.net/qq_25827845/article/details/83628045冷血之心的部落格)

快速導航:

Maven基礎概念和安裝配置教程

Maven的倉庫和settings.xml配置檔案

Maven的座標與依賴

目錄

Maven的座標

Maven的依賴

依賴配置

依賴範圍

排除依賴

歸類依賴

優化依賴

總結:


       在前邊兩節中,我們學習了Maven的基本概念以及何為Maven倉庫,並且如何配置settings.xml檔案等相關知識點。Maven的主要作用是可以幫助我們自動下載在pom.xml中配置新增的依賴。那麼在本節中,我們將學習如何引入依賴。

知識點包括:

Maven的座標,Maven的依賴配置,依賴範圍,傳遞性依賴,依賴調解,可選依賴,排除依賴,歸類依賴和優化依賴

 

Maven的座標

        Maven的倉庫中擁有著無數的構件,每一個構件都是一個jar或者war等檔案,如果沒有座標,那麼我們將無法唯一標識該構件,結果就是Maven將無法幫助我們自動下載構件。所以,Maven定義了一組規則:世界上任何一個構件都可以使用Maven座標來唯一標識。Maven座標的主要元素包括groupIdartifactIdversionpackaging(可選)和classifier(可選)

,通過這些元素,我們可以明確標識任何一個構件。

groupId:該元素定義了當前Maven專案隸屬的實際專案,一般情況下該項元素都與公司域名相對應,比如com.taobao.

artifactId:該元素定義了實際專案中的一個Maven Module

version:該元素表示當前構件的版本,包括穩定(release)版本和測試(snapshot)版本

packaging:該元素定義Maven專案的打包方式,預設為jar,還有war和pom方式

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

示例如下:

<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,若其它專案中需要依賴該jar包,那麼需要引入的配置如下:

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

上邊的配置<dependency>標籤其實就是在將我們專案中所需要的依賴配置在pom.xml,標籤已經定義好了該構件的座標,那麼Maven會根據該配置從倉庫中下載構件。既然學會了如何在專案中配置引入依賴,那麼我們接下來說說依賴相關的事兒吧。

依賴範圍

Maven如下6種依賴範圍:

  1. compile: 編譯依賴範圍(Default,大多數情況下我們都是在使用compile編譯範圍)
  2. test: 測試依賴範圍 (編譯主程式碼和執行時無效)
  3. provided: 已提供依賴範圍(就是說在執行的時候容器已經給我們提供該依賴了,比如說servlet-api
  4. runtime: 執行時依賴範圍
  5. system: 系統依賴範圍(生成的構建一般與本機系統繫結,不具備移植性不建議使用)
  6. import: 匯入依賴範圍(將其它地方官依賴配置匯入,後續講到依賴管理dependencyManagement詳細闡述)

我們以表格來說明下各個依賴的生效範圍:

依賴範圍

對於編譯有效

對於測試有效

對於執行有效

compile

Y

Y

Y

test

Y

Y

N

provided

Y

Y

N

runtime

N

Y

Y

system

Y

Y

N

傳遞性依賴

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

                          A->B並且B->C,那麼A->C,也就是C是一個A的傳遞性依賴

依賴調解

       依賴調解是指當存在多個傳遞性依賴時,出現了當前專案對於同一個依賴有多個版本被引入依賴樹中該如何選擇的原則。

比如說存在以下情況:

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

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

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

         原則:第一宣告優先原則,哪個現在pom中宣告(也就是在pom檔案的上邊),就以哪個為準(Maven2.0.9開始使用,在此之前是不確定的,導致構建專案具有一定的隨機性)

可選依賴

A->B,並且B->X(可選)B->Y(可選),那麼XY將不會對A有任何影響。

可選依賴使用<optional>true</optional>設定。可選依賴違反了單一職責的原則,一般不建議使用。

排除依賴

        我們通過在pom中配置<dependency></dependency>來引入依賴,但是該依賴存在多個傳遞性依賴,如果某個間接依賴不是我們需要的,影響到了我們專案的正常構建,那麼我們可以使用<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依賴都給排除掉了,這兩個依賴將不會出現在我們構建的專案中。

歸類依賴

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

優化依賴

      優化依賴的意思是通過優化,使得我們的專案對於引入的依賴優化一點^_^,比如說去除多餘的依賴等操作。那麼我們如何實現呢?這個時候我們的外掛(後邊詳細介紹)maven-dependency-plugin該上場了。不知各位是否還記得我們前面所說的超級Pom檔案,在該檔案中,定義了該外掛(見下圖),所以我們在maven專案中可以直接使用該dependency外掛。

我們主要使用該外掛的三個任務(goal):list,tree, anaylze(何為外掛,何為外掛goal我們後邊詳細街上,此處只要知道外掛可以用來幫你幹活就OK了哈)

我們先來看一下這條命令mvn dependency:list的執行結果吧。

這條命令的執行結果告訴你當前專案passport-common中引入的依賴有哪些,並且直接列出。但是其實這樣的結果對於我們程式設計師來說意義並不是很大,只是一些簡單的依賴羅列,看不出某個具體的依賴是直接還是被間接引入本專案的。這個時候我們需要使用mvn dependency:tree 將依賴以樹的形式展示出來。結果如下:

[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-mdh1.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] ------------------------------------------------------------------------

在這個依賴樹中,開頭從最左邊開始的,說明這個依賴是被直接引入的依賴A,我們還可以看到該直接依賴A又給本專案引入了哪些間接依賴。依賴樹的好處就在於,當我們想使用<exclusions><exclusion></exclusion></exclusions>標籤將某些依賴排除掉時,可以準確的確定出該間接依賴所對應的直接依賴。

        那麼,當我們想進一步優化該專案的依賴呢?比如我們想看看哪些依賴是沒有被使用的?或者哪些依賴沒有顯示宣告配置,然而卻被直接呼叫呢?也就是優化依賴,使得專案更加簡潔穩定。這個時候我們需要使用mvn dependency:analyze 了,該命令可以分析專案中依賴的具體使用情況。結果如下:

[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] ------------------------------------------------------------------------

分析結果中,將依賴分為了兩種,即Used undeclared dependencies foundUnused declared dependencies found兩種

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

這種情況對於我們的專案構建是不利的,存在著潛在的風險,這些依賴是通過間接依賴引入專案的,當我們需要升級直接依賴的版本時,可能會導致間接依賴的版本出現變動,從而影響到專案的構建,所以我們需要顯示宣告任何專案中直接使用到的依賴

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

出現這種情況也我們不能簡單的將該依賴的宣告和配置刪掉了事,應該具體分析。因為mvn dependency:analyze只會分析編譯主程式碼和測試程式碼需要用到的依賴,一些執行測試和與執行時需要的依賴它就分析不出來。所以需要具體分析專案依賴情況。

 

總結

         這篇文章我們學習了maven的座標和依賴的相關知,結合前邊章節的學習,我們知道了倉庫是用來儲存構件(依賴)的,專案中需要的各種依賴通過Maven座標存在各個倉庫(本地倉庫和遠端倉庫等)中。配置檔案settings.xml和專案中的pom.xml都是對倉庫,依賴和外掛進行各種配置的,最終目的就是通過配置依賴,使得maven可以幫助我們自動下載依賴,減去了我們需要去網站上的各個地方去手動搜尋和下載依賴的煩惱。

 

如果對你有幫助,記得點贊哦~歡迎大家關注我的部落格,可以進群366533258一起交流學習哦~

本群給大家提供一個學習交流的平臺,內設菜鳥Java管理員一枚、精通演算法的金牌講師一枚、Android管理員一枚、藍芽BlueTooth管理員一枚、Web前端管理一枚以及C#管理一枚。歡迎大家進來交流技術。