6

I have a Java EE web application that does not make use of EJBs. I am targeting Jetty/Tomcat for deployment some of the time and thus need a WAR packaging. However, I am also target JBoss and Websphere some of the time.

My understanding is that full-blown Java EE application servers can take either EAR or WAR formats. When would I use one over the other and why? I understand they are both standard compressed file formats and I have read 10 different snippets that try to explain them (including this one), but am no closer to understanding the pros and cons of each.

Community
  • 1
  • 1
Dave
  • 21,524
  • 28
  • 141
  • 221

3 Answers3

5

If you have only web modules - use WAR file. If you have different Java EE modules - use EAR. Though you can deliver only web modules in EAR - there is no point in doing this (and you will need to do more complex deployment configuration)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Andriy Sholokh
  • 852
  • 2
  • 6
  • 15
  • 1
    What are examples of other "J2EE modules"? – Dave Sep 27 '10 at 15:36
  • For example, EJB, WebServices – Andriy Sholokh Sep 27 '10 at 16:44
  • 1
    My application has web services (SOAP), but I am not sure why this would benefit from an EAR...could you elaborate? – Dave Sep 27 '10 at 20:03
  • First of all, are you sure that your web services configured to work on Tomcat? If you have couple modules you will need to deploy them as separate WAR files to Tomcat (As Tomcat does not support EARs) Here is benefit of Application Servers like WebSphere and EARs... You can put all your web modules and web services to single deployment file (EAR). And they will be installed all togather. – Andriy Sholokh Sep 27 '10 at 20:22
  • I was using 2 servlets within the same WAR. Is there any reason it would better to make them 2 WARs within an EAR? – Dave Sep 28 '10 at 01:14
0

In J2EE application, modules are packaged as EAR, JAR and WAR based on their functionality

If You are using only Servlet, JSP, GIF and HTML files. Then use .WAR

 WAR:(Web ARchive) Web modules which contain Servlet class files, JSP
 Files, supporting files, GIF and HTML files are packaged as JAR file
 with .war (web archive) extension.

If you are using different different JAVA EE modules like (EJB + Servlet, JSP, GIF and HTML files + Other technology). Then use .EAR

EAR: (Enterprise ARchive) All above files (.jar and .war) are packaged
as JAR file with .ear (enterprise archive) extension and deployed into
Application Server.
dhS
  • 3,739
  • 5
  • 26
  • 55
Bharti Rawat
  • 1,949
  • 21
  • 32
0

The crucial point is if you need anything provided in an EAR file (which may contain said WAR file). If so, then it makes sense to deploy as an EAR.

It can also wire up some of the configuration you need to do manually in Tomcat etc. A typical example is the URL bound to the web application, where you need to override the default heuristic with a container specific configuration file for a WAR, but you can put it directly in the EAR configuration file.

Also, during development WARs can frequently be hotdeployed directly in exploded form, where EARs must be unpacked and deployed. For Glassfish from Eclipse the difference is quite noticeable.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • I could not find in the Tomcat documentation how one deploys an EAR there. Is this using the Embedded JBoss? – Dave Sep 27 '10 at 15:55
  • You cannot deploy EAR's in Tomcat. But you can have a naked WAR and the exact same WAR wrapped nicely in an EAR for separate servers. – Thorbjørn Ravn Andersen Sep 27 '10 at 16:03
  • Yes, you can't deploy EARs to Tomcat, because Tomcat is a servlet container not an application server. So, if you need to deploy your web application both to Tomcat and JBoss and WebSphere use WAR file. It will work for all of them – Andriy Sholokh Sep 27 '10 at 16:46