【发布时间】:2018-01-19 11:55:19
【问题描述】:
使用 Intellij IDEA(版本 2017.2.1)我有一个 Java/Maven 项目,我想在其中包含 slf4j 和 slf4j-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