EJB explanation regarding EAR vs WAR

18,979

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

Share:
18,979
Anand Sunderraman
Author by

Anand Sunderraman

Technology agnostic, (with the exception of .NET, yet to break ice with it), software enthusiast. Use technology as means to solve a problem.

Updated on June 04, 2022

Comments

  • Anand Sunderraman
    Anand Sunderraman almost 2 years

    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?
  • Isaac
    Isaac over 13 years
    Good explanation. Small correction though: 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.
  • JoseK
    JoseK over 13 years
    @Isaac: Thanks, will clarify that in the answer
  • carpinchosaurio
    carpinchosaurio about 6 years
    necrobumping, can I have libraries embbeded into the ejb jar ?
  • JoseK
    JoseK about 6 years
    @carpinchosaurio: you can have libraries in the WEB-INF/lib of the war and then bundled into the EJB EAR or as standalone JARS within the EAR - see appclient.jar example above
  • carpinchosaurio
    carpinchosaurio about 6 years
    @JoseK, thanks, but in my case I have no WAR, and no EAR, Just a EJB