在JavaEE,maven配置中从EJB模块记录

时间:2010-10-18 06:51:44

标签: logging ejb maven java-ee-6

我已经尝试了几个小时让我的项目工作但是EJB部分仍然失败了 java.lang.ClassNotFoundException:org.slf4j.LoggerFactory (我想在我的EJB中输出一些日志)。

项目布局如下:

|-- nnWeb-ear
|   |-- pom.xml
|   `-- src
|       `-- main
|           |-- application
|           |   `-- META-INF
|           |       `-- MANIFEST.MF
|           `-- java
|-- nnWeb-ejb
|   |-- pom.xml
|   `-- src
|       |-- main
|       |   |-- java
|       |   |   `-- com
|       |   |       `-- test
|       |   |           `-- packaging
|       |   |               `-- SimpleStateLess.java
|       |   `-- resources
|       |       |-- logback.xml
|       |       `-- META-INF
|       |           `-- MANIFEST.MF
|       `-- test
|           `-- java
|               `-- com
|                   `-- test
|                       `-- packaging
|-- nnWeb-web
|   |-- pom.xml
|   `-- src
|       |-- main
|       |   |-- java
|       |   |   `-- com
|       |   |       `-- test
|       |   |           `-- packaging
|       |   |               |-- SimpleEJB.java
|       |   |               `-- SimpleServlet.java
|       |   |-- resources
|       |   |   `-- logback.xml
|       |   `-- webapp
|       |       |-- index.jsp
|       |       `-- WEB-INF
|       |           `-- sun-web.xml
|       `-- test
|           `-- java
`-- pom.xml

主要的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.packaging</groupId>
  <artifactId>nnWeb</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>nnWeb</name>
  <url>http://maven.apache.org</url>
  <modules>
    <module>nnWeb-ear</module>
    <module>nnWeb-web</module>
    <module>nnWeb-ejb</module>
  </modules>
</project>

ejb 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>nnWeb</artifactId>
        <groupId>com.test.packaging</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.test.packaging</groupId>
    <artifactId>nnWeb-ejb</artifactId>
    <packaging>ejb</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>nnWebEjb</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.18</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.11</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>java.net2</id>
            <name>Java.Net Maven2 Repository, hosts the javaee-api dependency</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>APP-INF/lib</classpathPrefix>
                        </manifest>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>

            </plugin>
        </plugins>
        <finalName>nnWeb-ejb</finalName>
    </build>
    <profiles>
        <profile>
            <id>endorsed</id>
            <activation>
                <property>
                    <name>sun.boot.class.path</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.0.2</version>
                        <configuration>
                            <!-- javaee6 contains upgrades of APIs contained within the JDK itself.
                                 As such these need to be placed on the bootclasspath, rather than classpath of the
                                 compiler.
                                 If you don't make use of these new updated API, you can delete the profile.
                                 On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
                            <compilerArguments>
                                <bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
                            </compilerArguments>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <properties>
        <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
    </properties>
</project>

耳朵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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>nnWeb</artifactId>
        <groupId>com.test.packaging</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.test.packaging</groupId>
    <artifactId>nnWeb-ear</artifactId>
    <packaging>ear</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>nnWebEar</name>
    <url>http://maven.apache.org</url>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <version>6</version>
                   <!-- <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        </archive>-->
                        <defaultJavaBundleDir>APP-INF/lib</defaultJavaBundleDir>
                </configuration>
            </plugin>

                    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-classes</phase>
                        <goals>

                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>nnWeb-ear</finalName>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.test.packaging</groupId>
            <artifactId>nnWeb-ejb</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>com.test.packaging</groupId>
            <artifactId>nnWeb-web</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.18</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.11</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.sli4j</groupId>
            <artifactId>sli4j-slf4j-jdk14</artifactId>
            <version>2.0</version>
        </dependency>
    </dependencies>
    <properties>
        <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
    </properties>
</project>

和SimpleStateless.java EJB:

package com.test.packaging;

import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class SimpleStateLess {
     private static Logger logger = LoggerFactory.getLogger(SimpleStateLess.class);

   public void constructLog(){
       logger.debug("Finally got logging working in ejb module :)");
   }
}

打包后,ear文件如下:

|-- APP-INF
|   `-- lib
|       |-- aopalliance-1.0.jar
|       |-- guice-2.0.jar
|       |-- logback-classic-0.9.18.jar
|       |-- logback-core-0.9.18.jar
|       |-- slf4j-api-1.5.11.jar
|       |-- slf4j-jdk14-1.5.10.jar
|       |-- sli4j-core-2.0.jar
|       `-- sli4j-slf4j-jdk14-2.0.jar
|-- META-INF
|   |-- application.xml
|   |-- MANIFEST.MF
|   `-- maven
|       `-- com.test.packaging
|           `-- nnWeb-ear
|               |-- pom.properties
|               `-- pom.xml
|-- ngWeb-web.war
`-- nnWeb-ejb.jar

和ejb.jar清单是:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: dev
Build-Jdk: 1.6.0_21
Class-Path: APP-INF/lib/logback-classic-0.9.18.jar APP-INF/lib/logback
 -core-0.9.18.jar APP-INF/lib/slf4j-api-1.5.11.jar

请注意,从war包中进行日志记录(使用WEB-INF / lib目录进行Servlet日志记录+ EJB日志记录)并根据我的发现,在部署ear时(如果清单正确),登录EJB应该有效

我做错了什么?

1 个答案:

答案 0 :(得分:0)

查看展开的ear包装和ejb.jar清单

nnWeb-ejb.jarAPP-INF位于同一层次结构中。

因此,以下Class-Path条目无效......

Class-Path: APP-INF/lib/logback-classic-0.9.18.jar APP-INF/lib/logback
 -core-0.9.18.jar APP-INF/lib/slf4j-api-1.5.11.jar

他们需要

Class-Path: ../APP-INF/lib/logback-classic-0.9.18.jar ../APP-INF/lib/logback
 -core-0.9.18.jar ../APP-INF/lib/slf4j-api-1.5.11.jar