V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
stephCurry
V2EX  ›  程序员

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

  •  
  •   stephCurry · 2019-09-19 23:45:08 +08:00 · 2151 次点击
    这是一个创建于 1677 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 友有解决方法吗? 🙏🙏🙏

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

    dependencies {
    ....
    }
    stephCurry
        6
    stephCurry  
    OP
       2019-10-17 17:02:33 +08:00
    @wei745359223 谢谢, 但是我想 exclude 的是被引用 jar 的 slf4j, 但是那个 jar 中的 slf4 无法 exclude, 因为那个 jar 里面的 package 里就有 org.slf4j, 好像除了找到那个文件源码删除有关 slf4j 的部分, 别无它法?
    stephCurry
        7
    stephCurry  
    OP
       2019-10-17 17:29:43 +08:00
    找到了这个 jar 文件源码, 但是看了下通过修改源码的方式放弃了。。。。
    https://github.com/secure-software-engineering/FlowDroid/releases#soot-infoflow-cmd-jar-with-dependencies.jar
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4898 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:56 · PVG 17:56 · LAX 02:56 · JFK 05:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.