源码下载
协调
<groupId>com.demo.animal</groupId> <artifactId>animal-core</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>groupId:定义当前maven项目隶属的实际项目
artifactId:定义实际项目中的一个maven项目(模块),推荐实际项目项目做为artifactId的前缀,如:nexus-indexer
version:maven项目的当前版本
packaging:maven项目的打包方式
依赖
pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.animal</groupId> <artifactId>animal-core</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>animal-core</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.kenai.nbpwr</groupId> <artifactId>javax-inject</artifactId> <version>1.0-201002241208</version> </dependency> </dependencies> </project>maven通过在dependency元素中设置坐标来使用依赖,參数例如以下:
groupId,artifactId,version:依赖的基本坐标,最重要
type:依赖类型,相应于项目坐标定义的packaging,大部份情况下,这个不用配置,默觉得jar
scope:依赖范围
optional:标记依赖是否可选
exclusions:用来排除传递性依赖
依赖范围
依赖范围就是用来控制与这三种classpath的关系(编译classpath,測试classpath,执行classpath)
依赖范围的种类:
compile:编译领带范围,默认,对于编译,測试,执行三种classpath都有效,如:spring-coretest:測试依赖范围,仅仅对測试classpath有效, 如:junit
provided:已提供依赖范围,对编译与測试classpath有效,如:servlet-api
runtime:执行时范围,对測试与执行有效,如:jdbc驱动
system:系统依赖范围,与provided一样
依赖范围与classpath的有关系
传递性依赖
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
执行命令能够查看依赖树
dependency:tree查看结果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
依赖冲突解决的方法
test-a:
源码下载test-b:
源码下载watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
将test-a与test-b,安装到本地仓库,供test-c依赖
test.c
源码下载
为什么会是依赖commons-logging-1.1.3.jar?由于:当第二直接依赖与第三直接依赖都存在时,它是优先选择第二直接依赖(路径短)
继续
test.d
将test-d安装到本地仓库。供test-c依赖
test-c
源码下载
假设须要使用test-b的commons-logging-1.1.3.jar
能够进行例如以下配置,在test-d中增加exclusions
<dependency> <groupId>com.demo.test</groupId> <artifactId>test-d</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
版权声明:本文博客原创文章,博客,未经同意,不得转载。