java org.apache.log4j.Logger 无法解决尽管 Maven 依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38933271/
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-11-03 03:54:08  来源:igfitidea点击:

org.apache.log4j.Logger cannot be resolved inspite of Maven dependencies

javamavenlog4jmaven-plugin

提问by Tom

I am using maven (i.e. maven plugin for eclipse) for dependency management and generally it works. E.g. jasypt gets strong textresolved and I can use it without any problem. However when trying to use Log4J I get issues like "The import org.apache.log4j cannot be resolved" even though I followed the exact steps as desribed on http://logging.apache.org/log4j/2.x/maven-artifacts.html.

我正在使用 maven(即 eclipse 的 maven 插件)进行依赖管理,通常它可以工作。例如 jasypt 可以解析强文本,我可以毫无问题地使用它。但是,在尝试使用 Log4J 时,即使我按照http://logging.apache.org/log4j/2.x/maven-工件.html

There it says I need to include the following snippet into my pom.xml

它说我需要将以下代码段包含到我的 pom.xml 中

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.2</version>
  </dependency>
</dependencies>

However exactly that is included in my pom.xml as given below in my full pom.xml. Does that make sense to anyone? I also tried replacing the two dependencies with the following however without any success

但是,正如下面在我的完整 pom.xml 中给出的那样,它确实包含在我的 pom.xml 中。这对任何人都有意义吗?我还尝试用以下内容替换两个依赖项,但没有成功

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>2.6.2</version>
    <type>pom</type>
</dependency>

Cheers Tom

干杯汤姆

<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>PCenter</groupId>
  <artifactId>PCenter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>PCenter</name>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>ejbModule</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>com.paypal.sdk</groupId>
        <artifactId>rest-api-sdk</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis-jaxrpc</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.6.2</version>
    </dependency>
  </dependencies>
</project>

回答by ppeterka

The problem is that you have Log4j2 in the dependencies, and use the older, Log4j 1.2.x API.

问题是您的依赖项中有 Log4j2,并使用较旧的 Log4j 1.2.x API。

  • Either depend on Log4j 1.2.x

    <dependencies>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
    </dependencies>
    
  • Or use the correct API calls for Log4j 2.x:

  • 要么依赖于 Log4j 1.2.x

    <dependencies>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
    </dependencies>
    
  • 或者对 Log4j 2.x 使用正确的 API 调用:

Calls to org.apache.log4j.Logger.getLogger()must be modified to org.apache.logging.log4j.LogManager.getLogger()

调用org.apache.log4j.Logger.getLogger()必须修改为org.apache.logging.log4j.LogManager.getLogger()

(From Migrating from Log4j 1.x)

(来自从 Log4j 1.x 迁移

回答by Arturas M

Hmm, actually I tried ppeterkas suggested solution with including that older 1.2x api, but that didn't work for me, I still had that problem. And since the code which could not be compiled was not really my code but rather some open source code that I happened to be using in my own code, I didn't really want to modify it's loggers (I should also mention, that I just started having this problem after newly opening my eclipse on a new computer, before it was actually compiling fine with the same pom.xml, so that's weird, but nevertheless it still needed fixing then...).

嗯,实际上我尝试了 ppeterkas 建议的解决方案,包括那个较旧的 1.2x api,但这对我不起作用,我仍然遇到那个问题。而且由于无法编译的代码实际上并不是我的代码,而是我在自己的代码中碰巧使用的一些开源代码,因此我并不是真的想修改它的记录器(我还应该提一下,我只是在新计算机上新打开我的 eclipse 后开始遇到这个问题,在它实际上用相同的 pom.xml 编译之前,这很奇怪,但它仍然需要修复然后......)。

So a solution for me was to include an Apache Log4j 1.x Compatibility API for log4j 2.7:

因此,我的解决方案是为 log4j 2.7 包含一个 Apache Log4j 1.x 兼容性 API:

http://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api/2.7

http://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api/2.7

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.7</version>
</dependency>

Hope this will help other people having the same problem.

希望这会帮助其他有同样问题的人。