Oliver Watkins
Oliver Watkins

Reputation: 13539

How do I specify EJB reference in WAR project in Maven?

I currently have a WAR project and an EJB project, as well as an EAR project which bundles them together.

Actually it is exactly the project Maven > Enterprise Application in the samples for Netbeans.

I then create a web service in the WAR project, and want it to call a session bean in my EAR project. Simple enough.

My code looks something like this :

@WebService(serviceName = "NewWebService")
public class NewWebService {


    @EJB
    NewSessionBean bean;

At first my compiler complains that it can't find this NewSessionBean class which is fair enough since they are in different projects.

Through Netbeans 'hints' I see that Netbeans is suggesting to me to add the EJB project as a dependency, so I do.

My Maven XML now has this in the WAR POM :

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenEJBExample-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>jar</type>
</dependency>

Great!

But now when I right click the EAR file and click on Run (aka Deploy), it says that the Web Service can't find the EJB reference :(

Why does deploying the EAR not respect the dependency of the WAR on the EJB projcect? Even when I have the dependency in my Maven XML?

Schwerwiegend:   Ausnahme beim Deployment der Anwendung [mavenEJBExample-ear]
Schwerwiegend:   Exception during lifecycle processing
java.lang.IllegalArgumentException: Referenz [Remote ejb-ref name=com.mycompany.NewWebService/bean,Remote 3.x interface =com.mycompany.NewSessionBean,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] kann nicht aufgelöst werden, da [2]-EJBs in der Anwendung mit Schnittstelle com.mycompany.NewSessionBean vorhanden sind. \nEinige der möglichen Gründe: \n1. Die EJB-Bean-Klasse wurde in einer EAR-Library verpackt (oder durch ein anderes Library-Verfahren, das die Library für alle Komponentenmodule sichtbar macht). Dadurch schließen alle Komponentenmodule diese Bean-Klasse indirekt ein. \n2. Die EJB-Bean-Klasse wurde in einem Komponentenmodul verpackt, das das EJB entweder direkt oder indirekt über Manifest, WEB-INF/lib referenziert. \nDie EJB-Bean-Klasse darf nur im deklarierenden EJB-Modul und nicht in den referenzierenden Modulen verpackt werden. Die referenzierenden Module dürfen nur EJB-Schnittstellen einschließen.
    at com.sun.enterprise.deployment.util.ComponentValidator.accept(ComponentValidator.java:356)
    at com.sun.enterprise.deployment.util.DefaultDOLVisitor.accept(DefaultDOLVisitor.java:78)
    at com.sun.enterprise.deployment.util.ComponentValidator.accept(ComponentValidator.java:123)
    at com.sun.enterprise.deployment.util.ApplicationValidator.accept(ApplicationValidator.java:147)

Upvotes: 0

Views: 1793

Answers (1)

jjd
jjd

Reputation: 2288

As far as you are going to use EJB proxy, I would suggest to change the type of the dependency. Also because you are deploying an .ear archive I would expect you already have a jar with EBJs added to ear. You can check it by exploring your .ear. If you find the jar with your EJBs already added to the .ear archive, you may want to set scope to provided so it were not added twice.

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenEJBExample-ejb</artifactId>
    <type>ejb-client</type>
    <scope>provided</scope>
    <version>1.0-SNAPSHOT</version>
</dependency>

Upvotes: 2

Related Questions