maven | Dependencies和Exclusions
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
的项目,它支持对mysql
、oracle
等各种数据库的支持,但是在引用这个项目时,我们可能只用到其对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
(可选)。
这里由于x
,y
是可选依赖,依赖不会传递,x
,y
将不会对a
有任何影响。
Exclusions
当一个项目 A
依赖项目B
,而项目B
同时依赖项目C
,如果项目A
中因为各种原因不想引用项目C
,在配置项目B
的依赖时,可以排除对C
的依赖。
示例(假设配置的是A
的pom.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
, 所以需要exclude
掉lettuce-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>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK