Maven的依赖特性
Scope类型
<scope>compile|provided|test|runtime</scope>
compile:默认依赖范围,编译与打包时加入依赖
provided:编译与测试时加入依赖,但是打包时不会加入依赖
test:只在测试时加入依赖,已有在编译test目录中的内容时,才会依赖(所以呀,不要把测试类的东西写到main目录下)
runtime:运行是才依赖
依赖的传递性
1. 当scope是compile时,依赖才会传递
2. 当依赖级别相同时(依赖路劲相同),A依赖与X的1.0版本,B依赖与X的2.0版本,C依赖与A,B。那么C依赖X的哪个版本?
C对X的依赖,取决于C对A,B依赖的先后顺序,如果C在pom.xml中先依赖了A,那么C就会依赖与X的1.0,如果C在pom.xml中先依赖了B,那么C就会依赖X的2.0
3. 当依赖级别不同时,按照路劲最短的优先依赖
4. 限制依赖,在dependency中使用exclusions,可以屏蔽对所在项目的exclusions中包的依赖
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId></artifactId>
</exclusion>
</ exclusions>
Maven的聚合
首先,我们在maven-c02目录下建立2个项目pro_a,pro_b
目录结构如下
pro_a的pom.xml如下,依赖了commons-lang,json和junit
<?xml version="1.0" encoding="UTF-8"?>
<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>org.pro</groupId>
<artifactId>pro_a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>pom</packaging> -->
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
A.java类如下,在A.java中使用了StringUtils类
package org.pro.pro_a;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
public class A {
public String sayHello(String name) {
JSONArray ja = new JSONArray();
if(StringUtils.isNotBlank(name)) {
return "A SayHello to " + name;
} else {
return null;
}
}
}
下面,我们搞一个root的项目,把pro_a与pro_b需要的依赖都集成起来
pro_root的pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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>org.pro</groupId>
<artifactId>pro_root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
然后修改pro_a的pom.xml文件
1.加入了<parent>标签
2.删除了<dependency>中的<version>
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.pro</groupId>
<artifactId>pro_root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>pro_a</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
在经过对pro_root的mvn install之后,在对pro_a执行mvn compile即可成功了
这里,在pro_a的pom.xml中就可以省略依赖的版本号,简化代码了那个啥,pro_b在这个例子中好像也没啥用,忽略了吧