Maven全局排除某引用的一种方式

来源:互联网 发布:数据重采样 编辑:程序博客网 时间:2024/06/09 14:36
做了一个用maven构建的storm工程,公司为了和其他工程统一管理,将其纳入到一个整体项目中,并对所有子工程一键部署打包,在各个子工程中的pom.xml中引入parent节点来管理公共引用。

xxx.xxxxxxx.octopusoctopus-parent0.0.1../

出现了一个问题是,我的项目使用的是slf4j来进行日志管理,而公共引用parent中使用了springboot,而springboot中默认会使用logbak。项目重新启动后会报如下异常:

SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/E:/Workspaces/Maven/.m2/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/E:/Workspaces/Maven/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.4.1/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]


异常显示出现两个可绑定的日志配置,最后一行显示了实际使用的是logbak中的配置。这就尴尬了,不仅冲突了,而且jvm选择的绑定还不是slf4j。而且parent是公共依赖,我也不能直接修改,于是开始想办法。

1.对于普通引用,maven可以直接在dependency节点下的exclusions直接进行配置,即可将其排除,但是尝试对parent节点进行exclusion的配置,发现没有,继续。

2.想maven加载冲突jar有没有加载顺序(偷懒),在官网查了下:点击打开链接

The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random.
说明slf4j具体选哪个binding是jvm随机决定的。那么偷懒不成,再继续。


3.找到springboot引用logbak的地方是spring-boot-starter-logging项目。于是乎,想了个办法在pom中添加了如下依赖,并将其全部exclusion

    org.springframework.boot    spring-boot-starter-logging                        *            *            

4.再次尝试启动项目,没有再报这个问题,搞定!!!