一.依赖传递

什么是依赖传递:projectA依赖于JAR包和projectB,而JAR包又依赖于黄色的JAR包。而projectB依赖于projectC和其他JAR包。因此projectA依赖于projectB,projectC和图中的所有JAR包。这就是依赖的传递性。其中蓝绿色部分成为直接依赖。在当前项目中通过依赖配置建立的依赖关系。而与除projectB和直接依赖JAR包以外的其他JAR包和工程为间接依赖,即图中黄色部分。
通过projectA中的pom.xml,我们可以看到这种依赖关系。
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>com.itheima</groupId>
<artifactId>maven-projectA</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-projectB</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 排除依赖 排除依赖时不需要指定版本-->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
在<dependencies>标签中可以清楚地看到这些依赖关系。(排除依赖部分先忽略)
二.排除依赖
排除依赖指主动断开依赖的资源,被排除的资源无需指定版本。
当我们不想让某个项目依赖某个依赖时,我们可以使用排除依赖。如图所示:projectA依赖于projectB,而projectB依赖于JAR包。则projectA间接依赖JAR包,当我们不想让projectA间接依赖于JAR包时,我们可以使用排除依赖。



我们在projectA中书写<exclusions> 标签,指定要排除的依赖,比如我们要排除junit依赖,则在<groupId>标签和<artifactId>标签中指定。排除依赖时不需要指定版本。
XML
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven-projectB</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 排除依赖 排除依赖时不需要指定版本-->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
三.查看项目依赖
当我们想要知道项目有哪些依赖时,有两种查看方法。
方式一:看项目右侧maven项目中的Dependencies

如图展示了projectA的依赖。
方式二:将光标移到所要查看的项目模块的pom.xml文件中。然后单击鼠标右键->Diagrams->Show Diagrams...
这样就看到了projectA的以来图表。

