Gradle 导入本地 jar, Springboot 启动时和引入的 jar 中的 slf4j 冲突

2019-09-19 23:45:08 +08:00
 stephCurry

SpringBoot 项目导入了本地特别的 jar, 通过 build.gradle 方式如下:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    ...省略
    compile files("libs/myjar.jar")
}

然后启动 SpringBoot 时提示 slf4j 冲突如下:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/thePath/myjar.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/user/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.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 [org.slf4j.impl.SimpleLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.SimpleLoggerFactory loaded from file:/Users/pop/projects/cncert/vehicle-engine/libs/DexKiller.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.SimpleLoggerFactory

然而尝试了 gradle exclude 也不好使。。。尝试如下:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    ...省略
    compile files("libs/myjar.jar"){
    	exclude group(或者 module):"org.slf4j"
    }
}

然后随后把打成 jar 的工程里的直接用到 Logutils 都删了, 重新打包后查看 jar 里面资源, 还是有 org.slf4j, 可能是引用其他工具包的必要依赖吧。

v 友有解决方法吗? 🙏🙏🙏

2158 次点击
所在节点    程序员
7 条回复
janus77
2019-09-20 11:10:02 +08:00
gradle 有全局的 exclude,不要在某个特定的包下面 exclude
stephCurry
2019-09-20 15:35:55 +08:00
@janus77 感谢解答,但是如果全局 exclude 那把 springboot 的 slf4j 也 exclude 掉了啊
chengyiqun
2019-09-20 15:54:03 +08:00
本地的 jar 对吧, 如果没有签名, 可以直接把本地 jar 的 slf4j 相关 class 和 package 都删了.
chengyiqun
2019-09-20 15:54:55 +08:00
可以用 7zip 之类的软件打开 jar 包, 或者直接改后缀名为 zip
wei745359223
2019-10-17 16:49:46 +08:00
增加这段试试
configurations {
compile.exclude module: 'spring-boot-starter-logging'
}

dependencies {
....
}
stephCurry
2019-10-17 17:02:33 +08:00
@wei745359223 谢谢, 但是我想 exclude 的是被引用 jar 的 slf4j, 但是那个 jar 中的 slf4 无法 exclude, 因为那个 jar 里面的 package 里就有 org.slf4j, 好像除了找到那个文件源码删除有关 slf4j 的部分, 别无它法?
stephCurry
2019-10-17 17:29:43 +08:00
找到了这个 jar 文件源码, 但是看了下通过修改源码的方式放弃了。。。。
https://github.com/secure-software-engineering/FlowDroid/releases#soot-infoflow-cmd-jar-with-dependencies.jar

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/602335

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX