Adam Bien's Weblog

Tuesday Dec 08, 2009

Is Java EE 6 War The New EAR? The Pragmatic Modularization And Packaging

It is not only possible to put EJBs and JSR-299 Beans together directly into a WAR - EJBs can be also packaged separately into a JAR. After copying the “ejb-jar” into the [WAR]/WEB-INF/lib folder, the included beans become automatically available to the web app. No additional configuration or tweaking is required. In the contrary to the the old EAR packaging, all the classes are loaded with the same classloader. There is no difference between packaging the beans in a standalone JAR, or putting the byte code directly into the WEB-INF/classes folder.

You could even sprinkle the beans in as many JARs as you like - the Dependency Injection between them will just work. Because all the JARs inside the WEB-INF/lib are loaded by exactly the same classloader - it just behaves like a single JAR.

It’s only deployment - but makes it very interesting for maven:

You can partition your project into one WAR-module, which has a compile-scoped dependency to several <packaging>jar</packaging> typed modules. Maven will create the (EJB)-JARs and copy them for you into the WEB-INF/lib location. The new Java EE 6 WAR-packaging is interesting for the majority of the “business” projects with “weak” requirements regarding the modularization. You can partition your software into dependent pieces, without paying the costs of true JAR-isolation.

The EAR packaging is still interesting for projects with higher modularization requirements - here the EJB-JARs will be loaded with an isolated classloader separately. In the majority of all cases it is just an overhead: it is unlikely, that someone will replace a single EJB-JAR without rebuilding and testing the whole EAR...


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

Nice to bring this feature to the forefront; a question we often hear in our EJB3 forums is about JEE5 injection of EJBs into servlets and why it's not correctly working.

Regarding the unified packaging itself, though; It's the Maven/Ant configurations for packaging that lead us to start the ShrinkWrap[1] project; too often I had to change or fight with my project structure to get artifacts in their expected form.

[1] http://jboss.org/shrinkwrap

Posted by Andrew Lee Rubinger on December 08, 2009 at 09:07 PM CET #

@Andrew,

looks very good. I especially like the fluent-API. In the process of setting-up a test :-)

Thanks for the pointer!,

adam

Posted by Adam Bien on December 09, 2009 at 12:06 AM CET #

Nice! Its very painfull work with EAR mainly if your modules have to be deployed every time in development. Using IDE's with hot deployment don't work with ear projects.

Posted by Giovanni Candido on May 21, 2011 at 07:41 PM CEST #

@Giovani,

look at: http://java.net/projects/x-ray Also a Java EE 6-WAR example,

thanks!,

adam

Posted by Adam Bien on May 21, 2011 at 09:26 PM CEST #

Yeah, I love this feature!

I guess that you just need to remember about possible name clashes, as all the EJB components are sharing the same environment and namespace in opposed to the EJB-JAR.

Cheers!

Posted by PedroKowalski on August 08, 2011 at 06:21 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License