Java – How to build and deploy a Remote EJB with Maven

ejbjavajboss7.xmaven

I want to use Maven to build and deploy a Remote EJB. Later, I'll want to be able to inject and use this EJB into a separate WAR project.

So I thought I'll need a jar file that defines the interface, and use the jar in both projects (ejb and war).

I have this interface, in a Maven project called example:api, packaged as jar:

package example;

import javax.ejb.Remote;

@Remote
public interface Bean {

    void process();
}

I implement the above interface in another Maven project called example:ejb, packaged as ejb:

package example;

import javax.ejb.Stateless;

@Stateless
public class Ejb implements Bean {

    @Override
    public void process() {
    }
}

The ejb project uses the maven-ejb-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ejb-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <ejbVersion>3.1</ejbVersion>
    </configuration>
</plugin>

And both projects depend on javaee-api:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

Now to put them together, I made a third Maven project, called example:ear, packaged as ear:

<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>example</groupId>
    <artifactId>ear</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>ear</packaging>
    <name>ear</name>

    <dependencies>
        <dependency>
            <groupId>example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>example</groupId>
            <artifactId>ejb</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <configuration>
                    <modules>
                        <ejbModule>
                            <groupId>example</groupId>
                            <artifactId>ejb</artifactId>
                        </ejbModule>
                        <jarModule>
                            <groupId>example</groupId>
                            <artifactId>api</artifactId>
                        </jarModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The problem is that I can't deploy this ear project. I get the following error:

09:39:39,702 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015003: Found ear.ear in deployment directory. To trigger deployment create a file called ear.ear.dodeploy
09:39:39,713 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876: Starting deployment of "ear.ear"
09:39:39,773 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "ejb-0.0.1-SNAPSHOT.jar"
09:39:39,793 WARN  [org.jboss.modules] (MSC service thread 1-8) Failed to define class example.Ejb in Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader: java.lang.LinkageError: Failed to link example/Ejb (Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:396)
[...]
Caused by: java.lang.NoClassDefFoundError: example/Bean
    at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.7.0_03]
[...]
Caused by: java.lang.ClassNotFoundException: example.Bean from [Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)

I'm using JBoss AS 7.1.1, if it makes any difference.

Do you know what I'm doing wrong?

Best Solution

I changed the configuration of maven-ear-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <version>6</version>
        <defaultLibBundleDir>lib</defaultLibBundleDir>
    </configuration>
</plugin>

This way it works.