adam bien's blog

Java EE 6 / EJB 3.1 / JSF 2.0 WAR Modularization With Maven - Concrete Sample 📎

The backing bean HelloView:

@ManagedBean

@RequestScoped

public class HelloView {

    @EJB HelloService helloService;

    public String getHelloMessage(){

        return helloService.hello();

    }

is packaged directly in the classes folder inside the Java EE 6 WAR. The boundary HelloService:

@Stateless

public class HelloService {

    @EJB CurrentTimeService currentTimeService;

    public String hello(){

        return "Hello: " + currentTimeService.getCurrentDate();

    }

Is located in the JAR-module with the following maven metadata:

[...] 

  <groupId>com.abien.patterns.business</groupId>

  <artifactId>ModularWarWithEJBsBoundary</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>ModularWarWithEJBsBoundary</name>

[...] 

 

It references the CurrentTimeService control,

@Stateless

public class CurrentTimeService {

    public Date getCurrentDate(){

        return new Date();

    }

which gets automatically injected. The CurrentTimeService resides in another module. The compile-scoped dependency is specified in the pom.xml:

    <dependency>

        <groupId>com.abien.patterns.business</groupId>

        <artifactId>ModularWarWithEJBsControl</artifactId>

        <version>1.0-SNAPSHOT</version>

        <scope>compile</scope>

    </dependency>

All EJBs are located in standard JARs. There is no need for special treatment / artifacts.

In the pom.xml of the WAR-project only the dependency to the boundary has to be specified. The transitive dependency to the control is automatically inherited:

<groupId>com.abien.patterns.business</groupId>

   <artifactId>ModularWarWithEJBs</artifactId>

   <packaging>war</packaging>

   <version>1.0-SNAPSHOT</version>

   <name>ModularWarWithEJBs Java EE 6 Webapp</name>

   <url>http://maven.apache.org</url>

   <dependencies>

      <dependency>

         <groupId>com.abien.patterns.business</groupId>

         <artifactId>ModularWarWithEJBsBoundary</artifactId>

         <version>1.0-SNAPSHOT</version>

         <scope>compile</scope>

      </dependency>

   </dependencies>

[...] 

You will find both JAR-modules in the WEB-INF/lib/ folder:

ModularWarWithEJBs/

+--WEB-INF/

+----lib/

+------ModularWarWithEJBsBoundary-1.0-SNAPSHOT.jar 

+------ModularWarWithEJBsControl-1.0-SNAPSHOT.jar 

 

Java EE 6 WAR is the "new" EAR - it allows easy packaging and pragmatic modularization of EJB-modules and web applications. Together with Maven it's the perfectly synergy.

The working maven projects (ModularWarWithEJBs Java EE 6 Webapp) were pushed in into: http://kenai.com/projects/javaee-patterns/.  

 [See Page 267 (Monolithic or Loosely Coupled?) in "Real World Java EE Patterns - Rethinking Best Practices" and Premature Encapsulation Is the Root of All Evil, Page 253]