1.如图我们有三个项目,项目Age,项目Bge,项目Cge
2.我们使Age项目依赖到Bge项目,Bge项目依赖到Cge项目
Age项目和Bge项目分别执行命令:mvn install 打包*.jar包,且放到本地仓库中
配置项目Bge的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.imooc.Bge</groupId> <artifactId>Bge</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Bge</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>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.imooc.Age</groupId> <artifactId>Age</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
配置项目Cge的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.imooc.Cge</groupId> <artifactId>Cge</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Cge</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>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.imooc.Bge</groupId> <artifactId>Bge</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
发现maven依赖如下图:
证实maven的依赖拥有传递性质!
3.阻止maven的依赖性如项目Cge中,不要出现项目Age,那么我们在项目Cge中配置pom.xml如下
项目Cge的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.imooc.Cge</groupId> <artifactId>Cge</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Cge</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>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.imooc.Bge</groupId> <artifactId>Bge</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 排除依赖 --> <exclusions> <exclusion> <groupId>com.imooc.Age</groupId> <artifactId>Age</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
项目Cge编译后的效果如下: