【问题标题】:Integrating slf4j with maven将 slf4j 与 maven 集成
【发布时间】:2015-02-24 12:22:41
【问题描述】:

大家好,我是 Maven 新手,我正在尝试将 slf4j 集成到 maven 项目中 这是我的 pom.xml 文件

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

我的 main 函数中有这两行

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

项目已成功编译和打包,但是当我尝试运行它时 通过
java -cp 目标/maven-1.0-SNAPSHOT.jar com.goutam.maven.App 抛出以下异常

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)

【问题讨论】:

    标签: java maven


    【解决方案1】:

    build fat jar with 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
    

    注意:选择jar-with-dependencies的jar文件

    【讨论】:

      【解决方案2】:

      Maven 本身不会为您捆绑所有依赖项,因此您生成的“maven-1.0-SNAPSHOT.jar”不包含 sl4j(也不包含它自己的任何依赖项)。

      如果你想让你的命令行工作,你需要在你的 jar 中包含所有的库。这可以使用Maven Assembly Plugin 来实现,例如:

      <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.5.3</version>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>org.sample.App</mainClass>
              </manifest>
            </archive>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id> 
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      

      【讨论】:

        【解决方案3】:

        另外两个答案推荐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>
        

        【讨论】:

          【解决方案4】:

          我不喜欢构建肥罐的想法,因为我们在这种方法中失去了一些灵活性。

          宁愿在 maven 中提倡 copy-dependencies plugin

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

          将它与 maven-jar-plugin 结合起来。阅读this了解详情。

          假设第三方依赖被复制到 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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-08
            相关资源
            最近更新 更多