.War VS .Ear File

.war vs .ear file

From GeekInterview:

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

JAR:
EJB modules which contain enterprise java beans (class files) and EJB deployment descriptor are packed as JAR files with .jar extension

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

EAR:
All the above files (.jar and .war) are packaged as a JAR file with .ear (enterprise archive) extension and deployed into Application Server.

Benefits over using an EAR vs. WAR?

  • Easier to deploy (only one package instead of multiples).
  • Some server (example Weblogic, but not wildfly) allow shared session for an entire EAR.
  • In general, EAR provide more option to configure with AS.
  • Special folder (APP-INF) that let you define a config file application.xml.

If your application would consist only of multiple WARs then you may not find it such a big deal to maintain multiple deploy. However, consider an application which use WARs, EJBs, JMS etc. It will be a lot easier to mananage the interaction between all these components in an EAR.

You might want to read the packaging application part of the Java EE tutorial.

Why use an ear instead of a war?

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).

WAR vs EAR for web application with no EJB?

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)

Is a .war file nested in a .ear file nested inside another .ear file ever a valid configuration?

No, that is not valid. An EAR file may contain other module-level archives, such as WAR, EJB JARs, RARs, or library JARs, but it may not contain other EAR files.

More details here: https://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html

A portlet application should be deployed as war file or ear file

I'll assume you're using WebSphere Portal. From the product documentation:

Portlet applications are usually packaged into single WAR files. These
WAR files can be directly deployed into the portal by using portal
administration means (...). You might want to deploy portlet
applications together with EJBs, or bundle several WAR files into the
same EAR file, or work with similar scenarios. For this type of
requirement WebSphere Portal provides the predeployed mode with the
XML configuration interface (...)

And then it shows several additional steps to make your EAR work with WebSphere Portal. You can see that Portlet Application WAR files are the recommended, simpler and cleaner way to install Portlets in WebSphere Portal and will save you from very annoying administrative tasks. Also, a single WAR file can contain several Portlets so you're covered in that aspect too.

As an additional detail, when you deploy a WAR file to WebSphere Portal it wraps the file in an EAR before deploying so there's no big difference if you choose one or another.



Related Topics



Leave a reply



Submit