Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB explanation regarding EAR vs WAR

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?
like image 875
Anand Sunderraman Avatar asked Oct 13 '10 12:10

Anand Sunderraman


People also ask

What is difference between EAR and WAR?

An EAR file requires a fully Java Platform, Enterprise Edition (Java EE)- or Jakarta Enterprise Edition (EE)-compliant application server, such as WebSphere or JBoss, to run. A WAR file only requires a Java EE Web Profile-compliant application server to run, and a JAR file only requires a Java installation.

What is EJB and EAR?

EJB components are packaged into modules as jar files and are packaged into application enterprise archive as ear file. There are majorly three components of any enterprise application − jar − Java Application aRchive, containing EJB modules, EJB client modules and utility modules.

What is difference between jar and WAR?

JAR files allow to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications. JAR can be created with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.

What does EAR file stand for?

An enterprise archive (EAR) file is a compressed file that contains the libraries, enterprise beans, and JAR files that the application requires for deployment.


1 Answers

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

like image 114
JoseK Avatar answered Sep 23 '22 18:09

JoseK