2022
我们一起努力

深入理解Maven的坐标与依赖 - 编程语言

在前边两节中,我们学习了Maven的基本概念以及何为Maven仓库,并且如何配置settings.xml文件等相关知识点。Maven的主要作用是可以帮助我们自动下载在pom.xml中配置添加的依赖。那么在本节中,我们将学习如何引入依赖。

知识点包括:

Maven的坐标,Maven的依赖配置,依赖范围,传递性依赖,依赖调解,可选依赖,排除依赖,归类依赖和优化依赖

Maven的坐标

Maven的仓库中拥有着无数的构件,每一个构件都是一个jar或者war等文件,如果没有坐标,那么我们将无法唯一标识该构件,结果就是Maven将无法帮助我们自动下载构件。所以,Maven定义了一组规则:世界上任何一个构件都可以使用Maven坐标来唯一标识。Maven坐标的主要元素包括groupId,artifactId,version,packaging(可选)和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(可选),那么X,Y将不会对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-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] ------------------------------------------------------------------------

在这个依赖树中,开头从最左边开始的,说明这个依赖是被直接引入的依赖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 found 和Unused declared dependencies found两种

(1)Used undeclared dependencies found :使用了但是没有显示的声明该依赖

这种情况对于我们的项目构建是不利的,存在着潜在的风险,这些依赖是通过间接依赖引入项目的,当我们需要升级直接依赖的版本时,可能会导致间接依赖的版本出现变动,从而影响到项目的构建,所以我们需要显示声明任何项目中直接使用到的依赖。

(2)Unused declared dependencies found:声明了该依赖但是并没有被使用

出现这种情况也我们不能简单的将该依赖的声明和配置删掉了事,应该具体分析。因为mvn dependency:analyze只会分析编译主代码和测试代码需要用到的依赖,一些执行测试和与运行时需要的依赖它就分析不出来。所以需要具体分析项目依赖情况。

总结

这篇文章我们学习了maven的坐标和依赖的相关知,结合前边章节的学习,我们知道了仓库是用来存储构件(依赖)的,项目中需要的各种依赖通过Maven坐标存在各个仓库(本地仓库和远程仓库等)中。配置文件settings.xml和项目中的pom.xml都是对仓库,依赖和插件进行各种配置的,最终目的就是通过配置依赖,使得maven可以帮助我们自动下载依赖,减去了我们需要去网站上的各个地方去手动搜索和下载依赖的烦恼。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持云。

赞(0)
文章名称:《深入理解Maven的坐标与依赖 - 编程语言》
文章链接:https://www.fzvps.com/32501.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!