2

maven | Dependencies和Exclusions

 1 year ago
source link: https://benpaodewoniu.github.io/2023/01/30/maven8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
  • Dependencies:是可选依赖(Optional Dependencies)
  • Exclusions:是依赖排除(Dependency Exclusions)

Dependencies

当一个项目 A 依赖另一个项目 B 时,项目 A可能很少一部分功能用到了项目B,此时就可以在A中配置对B的可选依赖。举例来说,一个类似hibernate的项目,它支持对mysqloracle等各种数据库的支持,但是在引用这个项目时,我们可能只用到其对mysql的支持,此时就可以在这个项目中配置可选依赖。

<project>
...
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
</dependencies>
</project>

假设以上配置是项目A的配置,即:Project-A –> Project-B。在编译项目A时,是可以正常通过的。如果有一个新的项目X依赖A,即:Project-X -> Project-A

此时项目X就不会依赖项目B了。如果项目X用到了涉及项目B的功能,那么就需要在pom.xml中重新配置对项目B的依赖。假设A->B, B->x(可选), B->y(可选)。

这里由于xy是可选依赖,依赖不会传递,xy将不会对a有任何影响。

Exclusions

当一个项目 A依赖项目B,而项目B同时依赖项目C,如果项目A中因为各种原因不想引用项目C,在配置项目B的依赖时,可以排除对C的依赖。

示例(假设配置的是Apom.xml,依赖关系为:A –> B; B –> C):

<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectC</groupId>
<artifactId>Project-C</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

spring boot 关于 redis 的案例

引入 spring-boot-starter-data-redis 包,SpringBoot2 中默认的客户端是 Lettuce, 所以需要excludelettuce-core包,并引入jedis的包。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK