且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

log4j2 java.lang.NoClassDefFoundError:org / apache / logging / log4j / LogManager

更新时间:2022-04-10 23:09:30

从命令行运行应用程序jar时您的依赖jar在运行时不可用。您需要将这两个插件中的任何一个包含到pom.xml中,以便在运行时使用您的依赖项。

When you are running your application jar from command line your dependent jar are not available at runtime. You need to include any of these two plugins to pom.xml so have your dependencies available at runtime.

使用:maven-shade-plugin

Using: maven-shade-plugin

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

使用:maven-dependency-plugin

Using:maven-dependency-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
             <id>copy-dependencies</id>
             <phase>package</phase>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <configuration>
                 <outputDirectory>${project.build.directory}/lib</outputDirectory>
             </configuration>
        </execution>
     </executions>
</plugin>

当你执行 mvn包时将生成uber jar /或将依赖项复制到outputDirectory。我更喜欢使用maven-shade-plugin生成一个jar将所有依赖项。

When you will execute the mvn package it will generate uber jar / or copy the dependencies to outputDirectory. I will prefer maven-shade-plugin to generate one jar will all dependencies.