Adam Bien's Weblog
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...
Special Event: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 Tweet Follow @AdamBien