Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an ear instead of a war?

I read this and this which are somewhat related to my question. But I came across this article that says that EJBs can be packaged in a war file. If this is the case, why is there a need for an ear? An explanation with an example will be highly appriciated.

like image 870
Can't Tell Avatar asked Oct 21 '11 06:10

Can't Tell


People also ask

What is the difference between WAR and ear?

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 JAR vs WAR vs ear?

Usage. JAR file allows Java Runtime Environment (JRE) to deploy an entire application including the classes and related resources in a single request. WAR file allows testing and deploying web applications easily while EAR file allows deploying different modules onto an application server simultaneously.

How do ears turn into WAR?

Simplest solution will be to use Eclipse export function. Or generate two identical projects (e.g. 'ear2war'), first as EAR second as WAR, have a look at configuration files and finaly adjust your project.

What is the use of EAR file?

An EAR file is a critical piece in deploying a service application to a production server. An enterprise archive (EAR) file is a compressed file that contains the libraries, enterprise beans, and JAR files that the application requires for deployment.


2 Answers

Using EAR or WAR depends on the server you want to deploy it, your application, and on your personal preferences. From Java EE6 you can package your EJBs together with other servlets, jsps etc into WAR file (you end up with web application which you can deploy only on java ee 6 compatible server). If you package your app the old way with ejbs in a separate package and war separately, you can use java ee 5 server if you haven't used other features of java ee6 within your application, you can separate the deployments of your EJBs and WARs to have a clear separation of your business layer (EJB) and your view (Servlets, JSP's etc).

like image 119
Kris Avatar answered Nov 12 '22 13:11

Kris


Using an EAR affords a clean separation between business (usually stateless EJB beans that provide back-end / db-related services and can in principle be used by non-web clients) and front-end (xhtml files, JSF backing beans, etc).

I usually follow the below convention, for a given project, say "foo":

  • foo-ejb.jar has the EJB beans
  • foo-client.jar defines the interface of the EJB beans ('client' may be a misnomer, 'foo-if.jar' or 'foo-api.jar' may be better names)
  • foo-war.war has the web resources

Building foo-war.war only requires foo-client.jar

Building foo-ejb.jar only requires foo-client.jar.

The structure in the EAR is:

foo.ear
 |
 |-- foo-war.war
 |
 |-- foo-ejb.jar
 |
 \-- lib
      |---- foo-client.jar
      |
      \---- (other common jars)

There maybe a way to have a similarly clean separation when your code is deployed as a WAR but the above is what I am using and seems to work for me (I am open to suggestions of course).

like image 37
Marcus Junius Brutus Avatar answered Nov 12 '22 14:11

Marcus Junius Brutus