【问题标题】:How to include slf4j-simple in the artifact JAR when build from inside Intellij IDEA从 Intellij IDEA 内部构建时如何在工件 JAR 中包含 slf4j-simple
【发布时间】:2018-01-19 11:55:19
【问题描述】:

使用 Intellij IDEA(版本 2017.2.1)我有一个 Java/Maven 项目,我想在其中包含 slf4jslf4j-binding

我知道 StackOverflow 有很多关于 slf4j 及其缺失绑定的问题,但大多数都是指 Eclipse。然而,我的问题发生在 Intellij 下。

pom.xml我在<dependencies>下列出:

<dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.25</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
 <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-simple</artifactId>
     <version>1.7.25</version>
 </dependency>

现在,当我让 Intellij 构建一个工件 JAR,然后我从命令行运行该 JAR 时,我得到了(可怕的)错误:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

经过检查,JAR 确实不包含 slf4j-simple 类(尽管存在slf4j 类)。我该如何解决这个问题并指示 Intellij 将 slf4j-simple 合并为 pom.xml 中列出的内容?

为了完整起见,这里完整的项目pom.xml Intellij 正在使用:

<?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>au.gov.acic.travelalert</groupId>
    <artifactId>extract-data</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.3</version>
        </dependency>
    </dependencies>

</project>

最后说明:在我开始设置项目日志时,在 IDE 中甚至都不起作用,尽管 pom.xml 中的条目正确。但不知何故,在为slf4j-log4j12 额外添加(以及随后删除)依赖项之后,IDE 会选择slf4j-simple,但在写出人工制品 JAR 时仍然没有捆绑它......

【问题讨论】:

  • 你使用的是什么 maven 命令?您的项目中是否还有其他依赖项,并且 slf4j 是唯一没有出现在您的 jar 文件中的依赖项?
  • 如果您使用maven 构建.jar,则IDE 无关紧要。您应该发布 .pom 的所有相关部分,因为当您发布 mvn package 时,有些内容不包括它
  • @Michael 我自己没有发出 maven 命令 - 我让 Intellij 使用 artefact -> build 菜单来执行此操作。
  • @JarrodRoberson 我已经添加了完整的pom.xml。顺便说一句,这个问题不是 Maven 命令行问题;这是一个关于 Intellij 以及如何在其中使用 maven 的问题,因此它与其他问题不重复。
  • 当您从 gui 使用时,Idea 在命令行上执行完全相同的命令。如果 Idea 正在调用命令或者您是相同的命令,则无关紧要。

标签: java maven intellij-idea jar slf4j


【解决方案1】:

你可以试试这个解决方案;转到home目录并删除.m2目录(隐藏)更新maven项目并尝试重建。如果您使用的是(Indigo、Juno 和 Kepler)版本的 eclipse,请检查您的 eclipse 版本,使用最新版本进行升级,或者如果您想在其中工作,则可以参考以下内容;这个stackoverflow链接在这个问题上有最合适的答案:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". errorKonstantinos Margaritis 的问题和许多 Java 专家的回答。

【讨论】:

  • 如问题中所述,我使用 Eclipse。
【解决方案2】:

执行 mvn 依赖:树并检查:

  • slf4j-api 和 slf4j-simple 必须存在。
  • 不应存在其他 slf4j 绑定库
  • 可以存在 slf4j 网桥(库名称以“-slf4j”后缀结尾的网桥;例如,log4j-over-slf4j)。
  • 确保所有 slf4j 库版本匹配
  • 确保没有不同版本的重复 slf4j 库。

如果一切正常,slf4j-api 没有理由出现在工件上,但没有 slf4j-simple。打包jar是做什么的?

【讨论】:

  • 我自己没有发出 maven 命令 - 我让 Intellij 使用人工制品 -> 构建菜单来执行此操作。我将尝试找出如何在 Intellij 中发布此问题 - 或者你知道吗?
  • 为什么是负面的?无论如何,是的,我知道您正在使用intellij。有什么不同?无论如何,您都可以执行任何 maven 目标:stackoverflow.com/questions/27246065/…
【解决方案3】:

问题在于 JAR 工件的 IntelliJ 的 XML 配置不包含 slf4j-simple 库。

这说明IntelliJ找到了lib ray(maven根据`POM/XML~下载的)并将其用于项目的内部执行,但并没有将其打包到JAR中。

解决方案是将库添加到 XML 配置中,或者

  • 手动编辑 XML 文件。 (您可以在[projectroot].idea/artifacts/[projectname_jar.xml 下找到它)

  • 通过 IntelliJ 的 GUI,打开 Artifacts 对话框(File --> Project structure --> Artifacts),然后将 Libra 添加到 Output Layout 列表。

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 2018-04-26
    • 1970-01-01
    • 2015-08-27
    • 2017-05-14
    • 2011-02-21
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多