Slf4j integration with maven

Hi guys, I'm new to Maven, and I'm trying to integrate slf4j into a maven project. Here is my pom.xml file

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.7.9</version>
</dependency>

And I have these two lines in my main function

Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Hello World");

The project is compiled and successfully packaged, but when I try to run it through
java -cp target / maven-1.0-SNAPSHOT.jar com.goutam.maven.App the following exception is thrown

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at com.goutam.maven.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
+4
source share
4 answers

I do not like the idea of ​​creating thick cans, as we lose some flexibility in this approach.

Rather, it will protect the dependency plugin in maven.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>install</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>false</overWriteSnapshots>
          <overWriteIfNewer>true</overWriteIfNewer>
        </configuration>
      </execution>
    </executions>
  </plugin>

Align it with maven-jar-plugin. Read this for more details .

, target/lib.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>main class</mainClass>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>
+1

maven

pom.xml

<build>
  <plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.3</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>

        </configuration>
        <executions>
            <execution>
                <id>assemble-all</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>

java -cp target/maven-jar-with-dependencies-1.0-SNAPSHOT.jar com.goutam.maven.App

note: jar jar-with-dependencies

+2

Maven , "maven-1.0-SNAPSHOT.jar" sl4j ( - ).

, , librairies . Maven Assembly Plugin - :

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> 
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
+1

maven-assembly-plugin. , maven-shade-plugin.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
0

Source: https://habr.com/ru/post/1569139/


All Articles