eclipse 如何将 log4j 1.2 完全集成到 Maven 构建中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18500883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:08:07  来源:igfitidea点击:

How to integrate log4j 1.2 completely into a maven build

eclipsemavenlog4j

提问by Furlong

I've been pulling my hair out for a week trying to figure out what I'm doing wrong here, including trying to find solutions on this site and through Google, but I think maybe I don't know what I'm searching for. Any help is greatly appreciated!

我已经花了一个星期的时间试图找出我在这里做错了什么,包括试图在这个网站上和通过谷歌找到解决方案,但我想也许我不知道我在寻找什么. 任何帮助是极大的赞赏!

My goal is to use log4j 1.2.17 in a Maven 2 Java project which I created in Eclipse Kepler. I've recreated the problem from the Maven archetype: quickstart, then added the log4j dependency in pom.xml like so...

我的目标是在我在 Eclipse Kepler 中创建的 Maven 2 Java 项目中使用 log4j 1.2.17。我从 Maven 原型中重新创建了这个问题:quickstart,然后像这样在 pom.xml 中添加了 log4j 依赖项......

    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

so that running 'maven package' builds happily...

这样运行'maven package'就可以愉快地构建......

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.log4j.BasicConfigurator;

public class App 
{
    private static Logger logger = 
            LogManager.getLogger( App.class );

    public static void main( String[] args )
    {
        BasicConfigurator.configure();
        logger.debug( "Entering the main class" );

    System.out.println( "Hello World!" );
    }
}

But running 'java -cp target/dc-0.1.jar DC.dc.App' on the command line after a maven build results in this:

但是在 maven 构建后在命令行上运行“java -cp target/dc-0.1.jar DC.dc.App”会导致:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
at DC.dc.App.<clinit>(App.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

I'm complying with the Maven directory structure and log4j.properties is stored in /src/main/resources. This is the log4j file.

我遵守 Maven 目录结构,log4j.properties 存储在 /src/main/resources 中。这是 log4j 文件。

log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.rootLogger = DEBUG, consoleAppender

回答by Furlong

After spelling it out, the solution was much easier to find. I added the following to pom.xml :

拼写出来后,解决方案就容易多了。我在 pom.xml 中添加了以下内容:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

Then ran mvn clean compile assembly:singleThen ran the application java -cp target/dc-0.1-jar-with-dependencies.jar DC.dc.App

然后运行mvn clean compile assembly:single然后运行应用程序java -cp target/dc-0.1-jar-with-dependencies.jar DC.dc.App

And everything was happy. I found the solution at how-can-i-create-an-executable-jar-with-dependencies-using-maven.

一切都很愉快。我在how-can-i-create-an-executable-jar-with-dependencies-using-maven找到了解决方案。

回答by jseteny

I like the answer provided by Furlong. If it does not work for any reason, you can try this solution below.

我喜欢弗隆提供的答案。如果由于任何原因它不起作用,您可以尝试下面的这个解决方案。

I think you need to use the following maven plugin:

我认为您需要使用以下 maven 插件:

This plugin provides the capability to package the artifact in an uber-jar, 
including its dependencies and to shade - 
i.e. rename - the packages of some of the dependencies.

You can find it here

你可以在这里找到

When you have the plugin and execute the following:

当您拥有插件并执行以下操作时:

mvn package

It will build a so called uberjar. It contains the content of all the dependent jar files as a singe big fat jar file.

它将构建一个所谓的uberjar。它包含所有依赖 jar 文件的内容作为一个单一的大胖 jar 文件。

So it will contain the org.apache.log4j.LogManager class, too.

所以它也将包含 org.apache.log4j.LogManager 类。

回答by Drew MacInnis

The log4j jar is missing from the java classpath. If you know the location of the log4j jar on your file system you can append it to your -cpclasspath argument.

java 类路径中缺少 log4j jar。如果您知道文件系统上 log4j jar 的位置,则可以将其附加到-cp类路径参数中。

You could look into using the maven-dependency-pluginif you want to copy or bundle your dependencies, refer to the usage. Lots of alternatives discussed on this SO post.

如果要复制或捆绑依赖项,可以考虑使用maven-dependency-plugin,请参阅用法。在此 SO 帖子中讨论了许多替代方案。

Another way to test your jar project and have maven build the runtime classpath would be to via a maven unit or integration test.

另一种测试 jar 项目并让 maven 构建运行时类路径的方法是通过 maven 单元或集成测试