由於公司在super-bom里配置了檢查規則,build項目時遇到錯誤:
[ERROR] [XXX Enforcer Rules] find DuplicateClasses
Found in:
org.apache.logging.log4j:log4j-slf4j-impl:jar:2.6.2:compile
ch.qos.logback:logback-classic:jar:1.1.7:compile
Duplicate classes:
org/slf4j/impl/StaticMDCBinder.class
org/slf4j/impl/StaticMarkerBinder.class
org/slf4j/impl/StaticLoggerBinder.class
我在pom里並沒有直接引用logback-classic,看到這個錯誤就懵了,我在pom里沒有引用這個啊。
解決過程:
可以通過IntelliJ Idea的包依賴查看找出是哪個包依賴了logback-classic。操作:View->Tool Window->Maven Projects,這個按鈕:
可以看到package依賴圖,可能會比較復雜、雜亂,Ctrl+F后可以搜索到:
這篇博客有提到如何處理這個問題:http://blog.csdn.net/xudan1010/article/details/52890102,即使用excusions來排除spring-boot-starter對spring-boot-starter-logging的依賴,然后引入log4j-slf4j-impl
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
另可以使用命令來看依賴:mvn dependency:tree | grep log