halloleo halloleo - 8 months ago 136
Java Question

How to include slf4j-simple in the artifact JAR when build from inside Intellij IDEA

Using Intellij IDEA (version 2017.2.1) I have a Java/Maven project in which I want to include

slf4j
with the
slf4j-binding
.

I know that StackOverflow as an abundance of questions about
slf4j
and its missing binding, but most refer to Eclipse. My problem however occurs under Intellij.


In the
pom.xml
I list under
<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>


Now when I let Intellij build an artifact JAR and I then run the JAR from the command line I get the (dreaded) error:

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


Upon inspection the JARdoes indeed not contain
slf4j-simple
classes (
slf4j
classes are present though). How can I fix this and instruct Intellij to incorporate slf4j-simple as listed ion the
pom.xml
?

For completeness, here full project
pom.xml
Intellij is using:

<?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>





Final note: At the beginning when I set up the project logging didn't even work inside the IDE, despite the correct entries in the
pom.xml
. But somehow, after additionally adding (and subsequent removing) of a dependency entry for
slf4j-log4j12
, the IDE picks the
slf4j-simple
up, but still does not bundle it when writing out the artefact JAR...

Answer Source

The problem is that IntelliJ's XML configuration for the JAR artifact does not contain the slf4j-simple library.

This explains that IntelliJ finds the lib ray (downloaded according by maven according to the `POM/XML~) and uses it for internal execution of the project, but it does not pack it into the JAR.

The solution is to add the libarary to the XML config, either

  • manually, by editing the XML file. (You find it under [projectroot].idea/artifacts/[projectname_jar.xml)

or

  • via IntelliJ's GUI, by opening the Artifacts dialog (File --> Project structure --> Artifacts) and then adding the Libra to the Output Layout list.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download