(十)Maven依赖详解

1.何为依赖?

比如你是个男的,你要生孩子,呸呸呸...男的怎么生孩子,所以你得依赖你老婆,不过也不一定咯,你也可以依赖其她妹子。

我们在平时的项目开发中也是同理,你需要依赖一些东西才能实现相应的功能,但相应的功能或许也可以依赖其它的东西实现,比如数据库操作吧,你可以依赖hibernate,但你也可以通过mybatis来做。

这就是所谓的依赖关系咯。

以前我们需要手动的去找hibernate或者mybatis的jar包,系统抛异常我们还不知哪里报错,通过琢磨才明白没有引入相应的jar包,然后就去找啊找,找到了然后引入到工程当中。在这里我们就看到maven的好处了,它就是一个仓库,仓库里面有各种各样的包,想要什么就在pom.xml中依赖一下就好了,就算仓库中没有的包也可以把它扔到仓库中,想用的时候就依赖一下。

 

2.依赖的配置

 1 <project>      
 2     <dependencies> 
 3         <dependency>
 4             <groupId>junitgroupId>      
 5             <artifactId>junitartifactId>      
 6             <version>3.8.1version>
 7             <type>...type>
 8             <scope>testscope>
 9             <optional>...optional>
10             <exclusions>      
11                 <exclusion>      
12                   <groupId>...groupId>      
13                   <artifactId>...artifactId>      
14                 exclusion> 
15           exclusions>      
16         dependency>         
17       dependencies>      
18 project>                    

根元素下project下的dependencies可以包含一个或者多个dependency元素,以声明一个或者多个项目依赖。每个依赖可以包含的元素有:

groupId,artifactId和version:依赖的基本坐标,对于任何一个依赖来说,基本坐标是最重要的,Maven根据坐标才能找到需要的依赖。

type:依赖的类型,对应于项目坐标定义的packaging。大部分情况下,该元素不必声明,其默认值是jar。

scope:依赖的范围,后面会进行详解。

optional:标记依赖是否可选。

exclusions:用来排除传递性依赖,后面会进行详细介绍。

大部分依赖声明只包含基本坐标,然而在一些特殊情况下,其他元素至关重要,我们来看看。

 

3.依赖范围说明

由于不同的包在不同的地方用到,像junit我们只有在做测试的时候会用到这个包,在我们项目发布的时候,用不到这个包;还有servlet-api,在项目编译的时候将会用到这个包,而项目发布的时候就不会用到这个包,因为一般容器已经自带这个包,如果我们导入,有可能会出现冲突,所以maven引入了依赖范围这个概念,即我们上面提到的scope来解决这个问题。Maven中有主要有以下这几种依赖范围:

1)     test:指的是测试范围有效,在编译打包、运行时都不会使用这个依赖。例如:junit jar包。

2)     compile:指的是编译范围有效,在编译、测试、打包、运行时都会将依赖存储进去。如果没有指定,就会默认使用该依赖范围。例如:hibernate jar包。

3)     provided:在编译和测试的过程有效,最后生成包时不会加入,运行时自然也没效果。例如:servlet-api,因为servlet-api,tomcat等web服务器已经存在该jar包了,如果再打包可能会有冲突。

4)     runtime:在测试、运行的时候依赖,在编译的时候不依赖。例如:JDBC驱动,项目代码只需要jdk提供的jdbc接口,只有在执行测试和运行项目的时候才需要实现jdbc的功能。

5)     system:系统依赖范围。该依赖范围与provided所表示的依赖范围一致,对于编译和测试有效,但在运行时无效。只是使用system范围依赖时必须通过systemPath元素显式地指定依赖文件的路径。由于此类依赖不是通过Maven仓库解析的,而且往往与本机系统绑定,可能造成构建的不可移植,因此应该谨慎使用,systemPath元素可以引用环境变量。例如:

 1 <dependency>       
 2  
 3          <groupId>javax.sqlgroupId>         
 4 
 5          <artifactId>jdbc-stdextartifactId>       
 6 
 7          <version>2.0version>       
 8 
 9          <scope>systemscope>       
10 
11          <systemPath>${java.home}/lib/rt.jarsystemPath>
12 
13 dependency>

6)      import(Maven 2.0.9及以上):导入依赖范围。该依赖范围不会对三种classpath产生实际的影响。

        上述除import以外的各种依赖范围与三种classpath的关系如下:

依赖范围(scope) 测试classpath 编译classpath 运行classpath 例子
compile Y Y Y spring-core
provided   Y   servlet-api
test Y     junit
runtime Y   Y JDBC驱动实现
system Y Y   本地的,Maven仓库之外的类库文件

 

4.传递性依赖和依赖范围

Maven的依赖是具有传递性的,比如A->B,B->C,那么A间接的依赖于C,这就是依赖的传递性,其中A对于B是第一直接依赖,B对于C是第二直接依赖,C为A的传递性依赖。

在平时的开发中,如果我们的项目依赖了spring-core,依赖范围是compile,spring-core又依赖了commons-logging,依赖范围也是compile,那么我们的项目对于commons-logging这一传递性依赖的范围也就是compile。第一直接依赖的范围和第二直接依赖的范围决定了传递性依赖的范围。我们通过下面这个表格来说明,其中最左边一栏是第一直接依赖,最上面那一栏为第二直接依赖。中间交叉的是传递性依赖范围。

 

Compile

Test

Provided

Runtime

Compile

Compile

 

 

Runtime

Test

Test

 

 

Test

Provided

Provided

 

Provided

Provided

Runtime

Runtime

 

 

Runtime

例如:第一直接依赖范围是Test,第二直接依赖范围是Compile,那么传递性依赖的范围就是Test,大家可以根据这个表去判断。

仔细观察一下表格,我们可以发现这样的规律:

  • 当第二直接依赖的范围是compile的时候,传递性依赖的范围与第一直接依赖的范围一致;
  • 当第二直接依赖的范围是test的时候,依赖不会得以传递;
  • 当第二直接依赖的范围是provided的时候,只传递第一直接依赖的范围也为provided的依赖,且传递性依赖的范围同样为provided;
  • 当第二直接依赖的范围是runtime的时候,传递性依赖的范围与第一直接依赖的范围一致,但compile例外,此时传递性依赖的范围为runtime。

 

5.依赖调解

下面我们来思考这样一个问题,如果A->B->C->X(1.0),A->D-X(2.0),即A间接依赖X,我们可以看到有两条路径都依赖X,那么maven将会选择哪个版本的X?maven当中有一套自己的规则,我们来说明一下,maven传递性依赖的一些规则以及如何排除依赖冲突。

Maven里面对于传递性依赖有以下几个规则:

1)         最短路径原则:如果A对于依赖路径中有两个相同的jar包,那么选择路径短的那个包,路径最近者优先,上述会选X(2.0)。

2)         第一声明优先原则:如果A对于依赖路径中有两个相同的jar包,路径长度也相同,那么依赖写在前面的优先。例如:A->B->F(1.0),A->C->F(2.0),会选F(1.0)。

3)         可选依赖不会被传递,如A->B,B->C,B->D,A对B直接依赖,B对C和D是可选依赖,那么在A中不会引入C和D。可选依赖通过optional元素配置,true表示可选。如果要在A项目中使用C或者D则需要显式地声明C或者D依赖。

 

6.排除依赖

传递性依赖会给项目隐式的引入很多依赖,这极大的简化了项目依赖的管理,但是有些时候这种特性也会带来问题,它可能会把我们不需要的jar包也引入到了工程当中,使项目结构变得更复杂。或者你想替换掉默认的依赖换成自己想要的jar包,这时候就需要用到依赖排除。

例如:

 1 <dependency>    
 2      <groupId>org.springframeworkgroupId>  
 3      <artifactId>spring-coreartifactId>  
 4      <version>3.2.8version>  
 5      <exclusions>  
 6            <exclusion>      
 7                 <groupId>commons-logginggroupId>          
 8                 <artifactId>commons-loggingartifactId>  
 9            exclusion>  
10      exclusions>  
11 dependency> 

例子中spring-core包依赖了commons-logging包,我们使用exclusions元素声明排除依赖,exclusions可以包含一个或者多个exclusion子元素,因此可以排除一个或者多个传递性依赖。需要注意的是,声明exclusions的时候只需要groupId和artifactId,而不需要version元素,这是因为只需要groupId和artifactId就能唯一定位依赖图中的某个依赖。换句话说,Maven解析后的依赖中,不可能出现groupId和artifactId相同,但是version不同的两个依赖。

 

7.把依赖归为一类

在项目开发中往往会引入同一个项目中的多个jar包,比如最常见的spring,如果我们项目中用到很多关于Spring Framework的依赖,它们分别是spring-core-3.2.8.RELEASE, spring-beans-3.2.8.RELEASE,spring-context-3.2.8.RELEASE,它们都是来自同一项目的不同模块。因此,所有这些依赖的版本都是相同的,而且可以预见,如果将来需要升级Spring Framework,这些依赖的版本会一起升级。因此,我们应该在一个唯一的地方定义版本,并且在dependency声明引用这一版本,这一在Spring Framework升级的时候只需要修改一处即可。

首先使用properties元素定义Maven属性,实例中定义了一个子元素,其值为3.2.8.RELEASE,有了这个属性定义之后,Maven运行的时候会将pom.xml中所有的${springframework.version}替换成实际的值:3.2.8.RELEASE。也就是可以使用$和{}的方式引用Maven的属性。然后将所有springframework依赖的版本替换成${springframework.version}这个样子,就和在Java代码中定义了一个不变的常量一样,以后要升级版本就只需要把这个值改了。

给大家一个完整的Maven配置实例,如果有在使用maven+spring+springMVC+Mybatis+Oracle数据库的朋友可以直接拿去改造成自己项目所需的父pom,配置如下:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2     <modelVersion>4.0.0modelVersion>
  3 
  4     <groupId>com.uidpgroupId>
  5     <artifactId>UidpParentartifactId>
  6     <version>0.0.1-SNAPSHOTversion>
  7     <packaging>pompackaging>
  8 
  9     <name>UidpParentname>
 10     <url>http://maven.apache.orgurl>
 11 
 12     <properties>
 13         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
 14 
 15         <repository-url>http://192.168.0.70:8081/content/groups/public/repository-url>
 16 
 17         <maven-compiler-plugin.version>3.1maven-compiler-plugin.version>
 18         <maven-war-plugin.version>2.4maven-war-plugin.version>
 19         <maven-javadoc-plugin.version>2.9.1maven-javadoc-plugin.version>
 20         <maven-release-plugin.version>2.4.1maven-release-plugin.version>
 21         <maven-deploy-plugin.version>2.7maven-deploy-plugin.version>
 22 
 23 
 24 
 25         <junit.version>4.11junit.version>
 26         <oracle.version>10.2.0.4oracle.version>
 27         <springframework.version>3.2.8.RELEASEspringframework.version>
 28         <mybatis.version>3.2.2mybatis.version>
 29         <mybatis-spring.version>1.2.0mybatis-spring.version>
 30         <mysql-driver.version>5.1.25mysql-driver.version>
 31         <aspectjweaver.version>1.7.3aspectjweaver.version>
 32 
 33         <commons-dbcp.version>1.4commons-dbcp.version>
 34         <commons-pool.version>1.5.5commons-pool.version>
 35         <commons-fileupload.version>1.2.2commons-fileupload.version>
 36 
 37         <log4j.version>1.2.17log4j.version>
 38         <slf4j-api.version>1.7.5slf4j-api.version>
 39         <slf4j-log4j12.version>1.7.5slf4j-log4j12.version>
 40 
 41         <freemarker.version>2.3.19freemarker.version>
 42 
 43         <jackson-core.version>2.5.0jackson-core.version>
 44         <jackson-mapper-asl.version>1.9.7jackson-mapper-asl.version>
 45 
 46         <javax.servlet-api.version>3.0.1javax.servlet-api.version>
 47         <jsp-api.version>2.2jsp-api.version>
 48         <kryo.version>1.04kryo.version>
 49         <snakeyaml.version>1.8snakeyaml.version>
 50         <jedis.version>2.0.0jedis.version>
 51         <commons-lang.version>2.6commons-lang.version>
 52 
 53 
 54         <mockito-core.version>1.8.5mockito-core.version>
 55         <powermock-core.version>1.4.9powermock-core.version>
 56         <powermock-api-mockito.version>1.4.9powermock-api-mockito.version>
 57         <powermock-module-junit4.version>1.4.9powermock-module-junit4.version>
 58 
 59 
 60     properties>
 61 
 62     <dependencyManagement>
 63         <dependencies>
 64 
 65             <dependency>
 66                 <groupId>junitgroupId>
 67                 <artifactId>junitartifactId>
 68                 <version>${junit.version}version>
 69                 <scope>testscope>
 70             dependency>
 71 
 72             
 73             <dependency>
 74                 <groupId>org.springframeworkgroupId>
 75                 <artifactId>spring-webartifactId>
 76                 <version>${springframework.version}version>
 77             dependency>
 78 
 79             <dependency>
 80                 <groupId>org.springframeworkgroupId>
 81                 <artifactId>spring-webmvcartifactId>
 82                 <version>${springframework.version}version>
 83             dependency>
 84 
 85             <dependency>
 86                 <groupId>org.springframeworkgroupId>
 87                 <artifactId>spring-beansartifactId>
 88                 <version>${springframework.version}version>
 89             dependency>
 90 
 91             <dependency>
 92                 <groupId>org.springframeworkgroupId>
 93                 <artifactId>spring-contextartifactId>
 94                 <version>${springframework.version}version>
 95             dependency>
 96 
 97             <dependency>
 98                 <groupId>org.springframeworkgroupId>
 99                 <artifactId>spring-context-supportartifactId>
100                 <version>${springframework.version}version>
101             dependency>
102 
103             <dependency>
104                 <groupId>org.springframeworkgroupId>
105                 <artifactId>spring-coreartifactId>
106                 <version>${springframework.version}version>
107             dependency>
108 
109             <dependency>
110                 <groupId>org.springframeworkgroupId>
111                 <artifactId>spring-jdbcartifactId>
112                 <version>${springframework.version}version>
113             dependency>
114 
115             <dependency>
116                 <groupId>org.springframeworkgroupId>
117                 <artifactId>spring-txartifactId>
118                 <version>${springframework.version}version>
119             dependency>
120 
121             <dependency>
122                 <groupId>org.springframeworkgroupId>
123                 <artifactId>spring-testartifactId>
124                 <version>${springframework.version}version>
125             dependency>
126 
127             <dependency>
128                 <groupId>org.springframeworkgroupId>
129                 <artifactId>spring-expressionartifactId>
130                 <version>${springframework.version}version>
131             dependency>
132 
133             <dependency>
134                 <groupId>org.springframeworkgroupId>
135                 <artifactId>spring-aopartifactId>
136                 <version>${springframework.version}version>
137             dependency>
138             
139 
140             <dependency>
141                 <groupId>org.mybatisgroupId>
142                 <artifactId>mybatisartifactId>
143                 <version>${mybatis.version}version>
144             dependency>
145 
146             <dependency>
147                 <groupId>org.mybatisgroupId>
148                 <artifactId>mybatis-springartifactId>
149                 <version>${mybatis-spring.version}version>
150             dependency>
151 
152             <dependency>
153                 <groupId>mysqlgroupId>
154                 <artifactId>mysql-connector-javaartifactId>
155                 <version>${mysql-driver.version}version>
156             dependency>
157 
158             <dependency>
159                 <groupId>com.oraclegroupId>
160                 <artifactId>ojdbc14artifactId>
161                 <version>${oracle.version}version>
162             dependency>
163 
164             <dependency>
165                 <groupId>org.aspectjgroupId>
166                 <artifactId>aspectjweaverartifactId>
167                 <version>${aspectjweaver.version}version>
168             dependency>
169 
170 
171             <dependency>
172                 <groupId>commons-dbcpgroupId>
173                 <artifactId>commons-dbcpartifactId>
174                 <version>${commons-dbcp.version}version>
175             dependency>
176             <dependency>
177                 <groupId>commons-poolgroupId>
178                 <artifactId>commons-poolartifactId>
179                 <version>${commons-pool.version}version>
180             dependency>
181             <dependency>
182                 <groupId>commons-fileuploadgroupId>
183                 <artifactId>commons-fileuploadartifactId>
184                 <version>${commons-fileupload.version}version>
185             dependency>
186 
187 
188             
189             <dependency>
190                 <groupId>log4jgroupId>
191                 <artifactId>log4jartifactId>
192                 <version>${log4j.version}version>
193             dependency>
194             <dependency>
195                 <groupId>org.slf4jgroupId>
196                 <artifactId>slf4j-apiartifactId>
197                 <version>${slf4j-api.version}version>
198             dependency>
199             <dependency>
200                 <groupId>org.slf4jgroupId>
201                 <artifactId>slf4j-log4j12artifactId>
202                 <version>${slf4j-log4j12.version}version>
203             dependency>
204 
205             
206             <dependency>
207                 <groupId>org.freemarkergroupId>
208                 <artifactId>freemarkerartifactId>
209                 <version>${freemarker.version}version>
210             dependency>
211 
212 
213             
214             <dependency>
215                 <groupId>com.fasterxml.jackson.coregroupId>
216                 <artifactId>jackson-coreartifactId>
217                 <version>${jackson-core.version}version>
218             dependency>
219             <dependency>
220                 <groupId>org.codehaus.jacksongroupId>
221                 <artifactId>jackson-mapper-aslartifactId>
222                 <version>${jackson-mapper-asl.version}version>
223             dependency>
224 
225             <dependency>
226                 <groupId>javax.servletgroupId>
227                 <artifactId>javax.servlet-apiartifactId>
228                 <version>${javax.servlet-api.version}version>
229                 <scope>providedscope>
230             dependency>
231 
232             <dependency>
233                 <groupId>javax.servlet.jspgroupId>
234                 <artifactId>jsp-apiartifactId>
235                 <version>${jsp-api.version}version>
236                 <scope>providedscope>
237             dependency>
238 
239             <dependency>
240                 <groupId>com.googlecodegroupId>
241                 <artifactId>kryoartifactId>
242                 <version>${kryo.version}version>
243             dependency>
244 
245             <dependency>
246                 <groupId>org.yamlgroupId>
247                 <artifactId>snakeyamlartifactId>
248                 <version>${snakeyaml.version}version>
249             dependency>
250 
251             <dependency>
252                 <groupId>redis.clientsgroupId>
253                 <artifactId>jedisartifactId>
254                 <version>${jedis.version}version>
255             dependency>
256 
257             <dependency>
258                 <groupId>commons-langgroupId>
259                 <artifactId>commons-langartifactId>
260                 <version>${commons-lang.version}version>
261             dependency>
262 
263 
264             <dependency>
265                 <groupId>org.mockitogroupId>
266                 <artifactId>mockito-coreartifactId>
267                 <version>${mockito-core.version}version>
268                 <scope>testscope>
269             dependency>
270 
271             <dependency>
272                 <groupId>org.powermockgroupId>
273                 <artifactId>powermock-coreartifactId>
274                 <version>${powermock-core.version}version>
275                 <scope>testscope>
276             dependency>
277 
278             <dependency>
279                 <groupId>org.powermockgroupId>
280                 <artifactId>powermock-api-mockitoartifactId>
281                 <version>${powermock-api-mockito.version}version>
282                 <scope>testscope>
283             dependency>
284 
285             <dependency>
286                 <groupId>org.powermockgroupId>
287                 <artifactId>powermock-module-junit4artifactId>
288                 <version>${powermock-module-junit4.version}version>
289                 <scope>testscope>
290             dependency>
291 
292 
293         dependencies>
294     dependencyManagement>
295     
296     <distributionManagement>
297         <repository>
298             <id>releasesid>
299             <name>publicname>
300             <url>http://59.50.95.66:8081/nexus/content/repositories/releasesurl>
301         repository>
302         <snapshotRepository>
303             <id>snapshotsid>
304             <name>Snapshotsname>
305             <url>http://59.50.95.66:8081/nexus/content/repositories/snapshotsurl>
306         snapshotRepository>
307     distributionManagement>
308     
309     
310     
311 <build>
312     <plugins>
313 
314         <plugin>
315             <groupId>org.apache.maven.pluginsgroupId>
316             <artifactId>maven-compiler-pluginartifactId>
317             <version>${maven-compiler-plugin.version}version>
318             <configuration>
319                 <source>1.7source> 
320                 <target>1.7target> 
321             configuration>
322         plugin>
323 
324         <plugin>
325             <groupId>org.apache.maven.pluginsgroupId>
326             <artifactId>maven-javadoc-pluginartifactId>
327             <version>${maven-javadoc-plugin.version}version>
328         plugin>
329 
330 
331         <plugin>
332             <groupId>org.apache.maven.pluginsgroupId>
333             <artifactId>maven-release-pluginartifactId>
334             <version>${maven-release-plugin.version}version>
335         plugin>
336 
337         <plugin>
338             <groupId>org.apache.maven.pluginsgroupId>
339             <artifactId>maven-deploy-pluginartifactId>
340             <version>${maven-deploy-plugin.version}version>
341             <configuration>
342                 <updateReleaseInfo>trueupdateReleaseInfo>
343             configuration>
344         plugin>
345 
346 
347 
348     plugins>
349 build>
350 
351 
352     <pluginRepositories>
353         <pluginRepository>
354             <id>nexusid>
355             <name>nexusname>
356             <url>${repository-url}url>
357             <releases>
358                 <enabled>trueenabled>
359             releases>
360             <snapshots>
361                 <enabled>trueenabled>
362             snapshots>
363         pluginRepository>
364     pluginRepositories>
365 
366     
367 project>

 

结束语:日月如梭,光阴似箭。不知不觉马上就要到2017年了,很多时候真的觉得不是我们年轻人不想做的更好,大多数时候是被前面的人给压迫的越来越油条了,所谓前人如此,却要求后人如何如何,其实想想也觉得蛮搞笑的。前人尽情的挥洒着智慧,玩着小心思不断的在压榨着年轻人,年轻人无奈的在这么个环境中挣扎求存。本以为离开了一个坑会迎来一个美好的未来,没想到的是不知不觉又跳入了一个更深的大坑,甚至有些坑还是隐形的,没有点特异功能还真不一定能够发现。不过话虽如此,作为新一代的年轻人,一定要经得过惊涛骇浪,何况是这点小风小浪。

 

可爱博主:AlanLee

博客地址:http://www.cnblogs.com/AlanLee

本文出自博客园,欢迎大家加入博客园。

 

转载于:https://www.cnblogs.com/AlanLee/p/6187843.html

你可能感兴趣的:((十)Maven依赖详解)