maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)

分手后的思念是犯贱 2021-12-19 03:04 705阅读 0赞

在maven的依赖管理中,有两种方式可以对依赖关系进行,分别是 可选依赖(Optional Dependencies)以及依赖排除(Dependency Exclusions)。

1、可选依赖(Optional Dependencies)

  1. optional标签可以防止将devtools依赖传递到其他模块,当开发者将应用打包运行之后,devtools会被自动禁用。
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <version>2.1.2.RELEASE</version>
  6. <optional>true</optional>
  7. </dependency>

2、依赖排除(Dependency Exclusions)

  1. 下面以一个例子来讲解一下以来排除的使用。
  2. 想把springboot项目的日志实现改为log4j2
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-log4j2</artifactId>
  6. </dependency>
  7. springboot默认的日志实现为logback,发现pom.xml中的很多starter都包含spring-boot-starter-logging
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-logging</artifactId>
  11. <version>1.5.2.RELEASE</version>
  12. </dependency>
  13. 两个同时存在会有问题,比如报:

20190702120451817.png

  1. 解决方法:使用依赖排除,将springboot默认的loggingjar排除,然后才能将日志实现改为log4j2
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-logging</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-log4j2</artifactId>
  15. </dependency>

发表评论

表情:
评论列表 (有 0 条评论,705人围观)

还没有评论,来说两句吧...

相关阅读