【问题标题】:Log4j issue with maven and gwt: org.apache.log4j.XAppender object is not assignablemaven 和 gwt 的 Log4j 问题:org.apache.log4j.XAppender 对象不可分配
【发布时间】:2014-07-07 16:12:31
【问题描述】:

我有一个 Maven GWT 项目。我将 Hibernate 和 HSQLDB 作为持久层。运行单元测试很好,但是当它尝试运行服务器组件时,日志记录不起作用,所以我对服务器端的错误视而不见。我遇到的错误是:

[ERROR] log4j:ERROR “org.apache.log4j.RollingFileAppender”对象是 不能分配给“org.apache.log4j.Appender”变量。 [错误] log4j:ERROR 类“org.apache.log4j.Appender”由 [错误] log4j:错误 [sun.misc.Launcher$AppClassLoader@baf1915] 而 [ERROR] log4j:ERROR 类型的对象 “org.apache.log4j.RollingFileAppender”由 [WebApp 类加载器=1312837549@4e404fad]。 [错误] log4j:错误无法 实例化名为“file”的附加程序。 [错误] log4j:错误 A “org.apache.log4j.ConsoleAppender”对象不可分配给 “org.apache.log4j.Appender”变量。 [ERROR] log4j:ERROR 类 “org.apache.log4j.Appender”由 [ERROR] log4j:ERROR 加载 [sun.misc.Launcher$AppClassLoader@baf1915] 而对象类型 [错误] log4j:错误“org.apache.log4j.ConsoleAppender”被加载 [WebAppClas sLoader=1312837549@4e404fad]。 [错误] log4j:错误可能 不实例化名为“stdout”的附加程序。

通过谷歌搜索,我很确定这是因为存在两个 log4j.jar 实例。 有没有解决这个问题的好方法或最佳实践方法?我从控制台以开发模式运行项目。 我的 Pom.xml 看起来像这样:

 <?xml version="1.0" encoding="UTF-8"?>
<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>testproject</groupId>
 <artifactId>trustme-mdm-gwtp</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>GWTP Basic</name>
 <description>Basic GWTP application</description>


 <properties>
 <!-- client -->
 <gwt.version>2.6.0</gwt.version>
 <gwtp.version>1.2.1</gwtp.version>
 <gin.version>2.1.2</gin.version>


 <!-- server -->
 <guice.version>3.0</guice.version>


 <!-- testing -->
 <junit.version>4.7</junit.version>
 <jukito.version>1.4</jukito.version>


 <!-- maven -->
 <gwt-maven-plugin.version>2.6.0</gwt-maven-plugin.version>
 <maven-surefire-plugin.version>2.6</maven-surefire-plugin.version>
 <maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version>
 <maven-resources-plugin.version>2.5</maven-resources-plugin.version>
 <maven-processor-plugin.version>2.0.5</maven-processor-plugin.version>
 <maven-build-helper-plugin.version>1.7</maven-build-helper-plugin.version>




 <target.jdk>1.7</target.jdk>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>


 <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
 </properties>


 <build>
 <outputDirectory>${webappDirectory}/WEB-INF/classes</outputDirectory>


 <plugins>
 <!-- JUnit Testing - skip *.GwtTest cases -->
 <!-- 'mvn test' - runs the Jukito tests -->
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>${maven-surefire-plugin.version}</version>
 <configuration>
 <includes>
 <include>**/*Test.java</include>
 </includes>
 <excludes>
 <exclude>**/*GwtTest.java</exclude>
 </excludes>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
 <source>1.7</source>
 <target>1.7</target>
 </configuration>
 </plugin>


 <!-- GWT -->
 <!-- 'mvn gwt:run' - runs development mode -->
 <!-- 'mvn gwt:debug' - runs debug mode -->
 <!-- 'mvn gwt:compile' - compiles gwt -->
 <!-- 'mvn integration-test' - runs the gwt tests (*GwtTest.java) -->
 <plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>gwt-maven-plugin</artifactId>
 <version>${gwt.version}</version>
 <configuration>
 <!-- With multiple tests use GwtTestSuite.java for speed -->
 <includes>**/*GwtTest.java</includes>
 <extraJvmArgs>-Xss1024K -Xmx1024M -XX:MaxPermSize=256M</extraJvmArgs>


 <copyWebapp>true</copyWebapp>
 <hostedWebapp>${webappDirectory}</hostedWebapp>


 <runTarget>Project.html</runTarget>
 <modules>
 <module>testproject.Project</module>
 </modules>
 </configuration>
 <executions>
 <execution>
 <goals>
 <goal>compile</goal>
 <goal>test</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 </plugins>
 </build>


 <dependencies>
 <!-- Google Web Toolkit -->
 <dependency>
 <groupId>com.google.gwt</groupId>
 <artifactId>gwt-user</artifactId>
 <version>${gwt.version}</version>
 </dependency>


 <!-- GWT-Platform -->
 <dependency>
 <groupId>com.gwtplatform</groupId>
 <artifactId>gwtp-all</artifactId>
 <version>${gwtp.version}</version>
 </dependency>


 <!-- DI -->
 <dependency>
 <groupId>com.google.inject</groupId>
 <artifactId>guice</artifactId>
 <version>${guice.version}</version>
 </dependency>
 <dependency>
 <groupId>com.google.inject.extensions</groupId>
 <artifactId>guice-servlet</artifactId>
 <version>${guice.version}</version>
 </dependency>
 <dependency>
 <groupId>com.google.inject.extensions</groupId>
 <artifactId>guice-assistedinject</artifactId>
 <version>${guice.version}</version>
 </dependency>
 <dependency>
 <groupId>com.google.gwt.inject</groupId>
 <artifactId>gin</artifactId>
 <version>${gin.version}</version>
 </dependency>


 <!-- Test -->
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>${junit.version}</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.jukito</groupId>
 <artifactId>jukito</artifactId>
 <version>${jukito.version}</version>
 <scope>test</scope>
 </dependency>
 <!-- Hibernate -->
 <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>4.3.5.Final</version>
 </dependency>
 <!-- hSQLDB -->
 <dependency>
 <groupId>org.hsqldb</groupId>
 <artifactId>hsqldb</artifactId>
 <version>2.3.2</version>
 </dependency>
 <!-- add slf4j interfaces to classpath -->
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.7.7</version>
 </dependency>
 <!--  GWT-Log -->
 <dependency>
 <groupId>com.allen-sauer.gwt.log</groupId>
 <artifactId>gwt-log</artifactId>
 <version>3.3.0</version>
 </dependency>

 </dependencies>
</project>

当我查看 \target...\lib 文件夹时,这些是已部署的 jar:

antlr-2.7.7.jar aopalliance-1.0.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar dom4j-1.6.1.jar gin-2.1.2.jar guava-16.0.1.jar guava-gwt-16.0.1.jar guice-3.0.jar guice-assistedinject-3.0.jar guice-servlet-3.0.jar gwt-jackson-0.4.0.jar gwt-log-3.3.0.jar gwtp-all-1.2.1.jar gwtp-clients-common-1.2.1.jar gwtp-crawler-1.2.1.jar gwtp-dispatch-common-client-1.2.1.jar gwtp-dispatch-common-shared-1.2.1.jar gwtp-dispatch-rest-1.2.1.jar gwtp-dispatch-rpc-client-1.2.1.jar gwtp-dispatch-rpc-server-1.2.1.jar gwtp-dispatch-rpc-server-guice-1.2.1.jar gwtp-dispatch-rpc-server-spring-1.2.1.jar gwtp-dispatch-rpc-shared-1.2.1.jar gwtp-mvp-client-1.2.1.jar gwtp-mvp-shared-1.2.1.jar gwtp-processors-1.2.1.jar gwtp-tester-1.2.1.jar gwt-user-2.6.1.jar hibernate-commons-annotations-4.0.4.Final.jar hibernate-core-4.3.5.Final.jar hibernate-entitymanager-4.3.5.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar hsqldb-2.3.2.jar jackson-annotations-2.3.0.jar jackson-annotations-2.3.0-sources.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar javax.inject-1.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar json-20090211.jar jsr305-1.3.9.jar jsr311-api-1.1.1.jar maven-artifact-3.1.1.jar plexus-utils-3.0.15.jar servlet-api-2.5.jar spring-aop-3.2.3.RELEASE.jar spring-beans-3.2.3.RELEASE.jar spring-context-3.2.3.RELEASE.jar spring-core-3.2.3.RELEASE.jar spring-expression-3.2.3.RELEASE.jar spring-web-3.2.3.RELEASE.jar 验证-api-1.0.0.GA.jar 验证-api-1.0.0.GA-sources.jar velocity-1.7.jar xml-apis-1.0.b2.jar

我把 pom.xml 改成这样:

<!-- Google Web Toolkit -->
<dependency>
    <groupId>com.google.gwt</groupId>
    <artifactId>gwt-servlet</artifactId>
    <version>${gwt.version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.google.gwt</groupId>
    <artifactId>gwt-user</artifactId>
    <version>${gwt.version}</version>
    <scope>provided</scope>
</dependency>

但还是没有成功……

我插入了依赖关系树的屏幕截图。我的声誉不允许粘贴图像。它存储在这里:

【问题讨论】:

  • 对我们有用的快速解决方法是将 -Dlog4j.ignoreTCL=true 传递给 devMode

标签: java maven gwt log4j


【解决方案1】:

感谢您的帮助(尤其是托马斯)。 我用于项目的指南指出 log4j 和 slf4j 作为 pom.xml 中的外部引用是必要的。
我只是删除了它,它现在正在工作。然而,这教会了我,通过不结合服务器和客户端依赖项,它确实让生活变得更简单。

【讨论】:

    【解决方案2】:

    您可能面临 GWT 2.6.0 的已知问题,即在 DevMode 中错误地加载服务器端类:https://code.google.com/p/google-web-toolkit/issues/detail?id=8585

    尝试使用 GWT 2.6.1,看看是否能解决问题。

    编辑:另外,清理你的依赖范围:你的WEB-INF/lib 中有gwt-userservlet-api,它们不能存在;您还有一堆不应该存在的特定于 GWT 的仅限客户端的 JAR。

    【讨论】:

    • 谢谢,我试过了,但我仍然得到同样的错误:(
    • 您还需要清理依赖项的作用域。
    • 我什至不知道 spring 和 servlet-api 依赖项是如何进入 lib 文件夹的。我怎么能避免把那些罐子放在那里? pom.xml 中的 gwt-config 来自官方 gwtproject 页面。
    • 运行 mvn dependency:tree 以了解您的依赖项来自何处。
    • 我在上面的帖子中链接了依赖树。但我仍然不知道我在寻找什么。 pom 配置似乎在示例项目中很常见。我知道复杂的项目往往会分成服务器项目和客户端项目。但是在一个项目中就不可能同时拥有这两者吗?
    【解决方案3】:

    将 log4j 依赖范围更改为 maven 中提供的。检查是否有多个 logger jar。

    【讨论】:

    • 我将 slf4j-log4j12 包的范围更改为“提供”。这个组合包可能有问题吗?我编辑了我的帖子以在我的 lib 文件夹中包含 jar 列表。
    • 使用 eclipse 并转到 pom.xml 然后查看依赖层次结构并检查 log4j
    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多