EJB explanation regarding EAR vs WAR

Anand Sunderraman picture Anand Sunderraman · Oct 13, 2010 · Viewed 18.4k times · Source

I have just begun reading up on EJBs.

Even as I venture into it I have a few questions based on what I have heard about them.

  1. Do applications using EJBs always have to be deployed as an EAR?
  2. Can applications containing EJBs be deployed just like other other java web projects using ECLIPSE and without using ANT?

Answer

JoseK picture JoseK · Oct 13, 2010

1.Do applications using ejb's always have to be deployed as EAR ?

No.

The EJB module is assembled into a JAR, lets call it myapp-ejb.jar as a naming convention.

This contains the EJB code as well as the deployment descriptor file called ejb-jar.xml in EJB 2.x. In EJB 3.0, the code itself contains the annotations required for the server to understand for deployment, so the deployment descriptors are optional. The deployment descriptor/annotations cover basic stuff needed for EJB deployment like the JNDI, DataSource look up etc.

A collection of multiple EJB modules and other Web modules (war) together make up an EAR. As @Isaac pointed out, an EAR doesn't have to include any WAR file in it. The only condition for an EAR file is to contain at least one J2EE module of any kind.

The EAR needs a META-INF/application.xml which lists all the EJB jars and wars present in the EAR. So you go for an EAR when you have multiple EJB modules which is usually the case, hence the usual deployment is of an EAR.

An example of this file is shown below taken from http://download.oracle.com/docs/cd/B32110_01/web.1013/b28221/undejdev003.htm. This is a good article for you to read.

<application>
  <display-name>master-application</display-name>
  <module>
    <ejb>ejb1.jar</ejb>
  </module>
  <module>
    <ejb>ejb2.jar</ejb>
  </module>
  <module>
    <java>appclient.jar</java>
  </module>
  <module>
    <web>
        <web-uri>clientweb.war</web-uri>
        <context-root>webapp</context-root>
    </web>
  </module>
  <module>
    <ejb>ejb3.jar</ejb>
  </module>

2.Can applications containing EJB's be deployed just like other other java web projects using ECLIPSE and without using ANT

Yes, once an EAR/JAR is assembled it can be deployed into a server (via Eclipse if you wish).

Ant is a build tool which has nothing to do with the actual deploying of the EJB code. It is used to compile and assemble the JAR - which can be done from Eclipse as well.

Here is a tutorial which does just that.

Further Reading

Packaging Applications

Packaging EJB3 Applications