Logging from EJB module in JavaEE, maven configuration

I have tried for hours to get my project working but the EJB part is still failing with a java.lang.ClassNotFoundException: org.slf4j.LoggerFactory (I want to output some log in my EJB).

The layout of the project is the following:

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

the main 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>

The 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>

The ear 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>

and the 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 :)");
   }
}

Once packaged, the ear file looks like:

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

and the ejb.jar manifest is:

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

Note that logging from the war package works (Servlet logging + EJB logging using the WEB-INF/lib directory) and according to what I found, logging in the EJB should work when deploying the ear (if the manifest is correct).

What am I doing wrong ?


Looking at the exploded ear packaging and the ejb.jar manifest,

nnWeb-ejb.jar is in the same hierarchy as APP-INF .

Thus the following Class-Path entries become invalid...

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

They would need to be

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
链接地址: http://www.djcxy.com/p/82656.html

上一篇: 将WAR文件打包到EAR中,以便提供WAR依赖关系

下一篇: 从JavaEE中的EJB模块进行日志记录,maven配置