将外部jar文件添加到我的maven项目时SLF4J绑定

时间:2017-02-23 10:40:21

标签: spring-mvc apache-spark logging spring-boot thymeleaf

我对Spring启动很新。我使用intellij ide。我正在尝试运行在外部jar文件中找到的类。此jar文件包含用scala编写的SPARK应用程序。所以,我已经将jar文件添加到依赖项中。当我在一个单独的类中测试它时它工作正常。但是,当我尝试在我的spring boot mvc项目中运行它时,我遇到SLF4Js绑定阻碍了我的应用程序启动。

我试图在一些stackoverflow帖子之后从我的依赖项中排除log4j(参见下面的代码)。但是,它没有成功。

PS。我还添加了spark.assembly jar文件。

我该如何解决这个问题?

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>softuni</groupId>
    <artifactId>mvc-blog</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.10.0</version>
        </dependency>

        <dependency>
            <groupId>cosine-lsh</groupId>
            <artifactId>cosine-lsh</artifactId>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>


    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <spark.version>1.4.1</spark.version>
    </properties>


</project>

我得到的错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:.../BLOG/cosine-lsh.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:.../BLOG/spark-1.4.1/lib/spark-assembly-1.4.1-hadoop2.6.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/n12017/.m2/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.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.Log4jLoggerFactory]
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: 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.Log4jLoggerFactory loaded from file:/S:/Staff_files/Mehmet/Projects/JAVA_SPRING_PROJECTS/BLOG/cosine-lsh.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.slf4j.impl.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

提前致谢!

修改

这是我所经历的并解决了这个问题。这对像我这样的初学者来说可能是有益的。

我之前首先做的是手动将jar文件添加到项目结构下的依赖项中 - &gt;模块 - &gt;依赖。然后我通过添加

来改变我的pom.xml
<dependency>
            <groupId>cosine-lsh</groupId>
            <artifactId>cosine-lsh</artifactId>           
</dependency> 

但是,正如我所理解的,这不是一个正确或安全的方法来将外部jar文件添加到mvn项目中。无论如何,通过应用下面的程序,我的问题就解决了。

首先,我将目录更改为具有相关pom.xml文件的目录。 然后,我应用了下面的脚本。请注意,我明确告诉mvn groupid的名称,工件ID和版本。

mvn install:install-file -
Dfile=...\...\cosine-lsh.jar \ -DgroupId=cosine-lsh -DartifactId=cosinelsh \ -Dversion=1.0 -Dpackaging=jar

作为最后一步,我将依赖项添加到我的pom.xml文件中,如下所示:

<dependency>
            <groupId>cosine-lsh</groupId>
            <artifactId>cosinelsh</artifactId>
            <version>1.0</version>
</dependency>

至于我的SPARK配置:

上面的更改使弹簧代码能够在不抱怨绑定的情况下运行,尽管它不断发出警告,如下所示:

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/C:/Users/n12017/.m2/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/C:/Users/n12017/.m2/repository/cosine-lsh/cosinelsh/1.0/cosinelsh-1.0.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]

最后但并非最不重要的是,使用SPARK时出现了另一个绑定问题,即Jackson对象。我通过添加如下所示的排除来解决这个问题:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-databind</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
            </exclusions>
</dependency>

0 个答案:

没有答案