提问者:小点点

maven和gwt的Log4j问题:org. apache.log4j.XAppender对象不可分配


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

[ERROR]log4j: ERROR"org.apache.log4j.RollingFileAppender"对象不能与"org.apache.log4j.Appender"变量同级。[ERROR]log4j:ERROR类"org.apache.log4j.Appender"由[ERROR]log4j:ERROR[sun.misc.Launcher$AppClassLoader@baf1915]加载,而类型为[ERROR]log4j:ERROR"org.apache.log4j.RollingFileAppender"的对象由[WebApp ClassLoader=1312837549@4e404fad]加载。[ERROR]log4j:ERROR无法实例化名为"file"的appender。[ERROR]log4j:ERROR A"org.apache.log4j.ConsoleAppender"对象未分配e给"org.apache.log4j.Appender"变量。[ERROR]log4j:ERROR类"org.apache.log4j.Appender"

从googling我很确定这是因为存在两个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:

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

但还是没有成功…

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


共3个答案

匿名用户

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

匿名用户

将log4j依赖范围更改为maven. check中提供的是否有多个记录器jar。

匿名用户

您可能面临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,它们也不应该存在。