Build the enterprise with EJB 3, JBoss Seam, and Maven 2

Enterprise resource management with JBoss Seam and Maven 2

1 2 3 4 5 6 7 Page 4
Page 4 of 7

EJBs project POM and file structure

Figure 2 is a breakdown of the internal structure of the EJB project folder.

The EJB project folder
Figure 2. The EJBs project folder (click to enlarge)

The resources directory has three very important files that are core to the EJB 3 mechanism, especially because Seam is now an integral part of the project architecture. Figure 3 illustrates the layout of the resources folder.

The layout of the resources folder
Figure 3. The layout of the resources folder (click to enlarge)

Note the explicit META-INF directory. Maven will not create this for you if it is missing (in case the EJB project structure was created manually, for instance), so it is recommended that Maven's archetype plugin be used to create Maven project types. All files in this directory will end up in the root of the JAR file.

In the ejb-jar.xml descriptor file, shown in Listing 6, the application attempts to bind Seam's Interceptor class to one or more EJB modules deployed within the EAR file. This means that we can have multiple EJB JARs deployed and packaged separately for functional reasons (perhaps the EJBs were produced from build systems other than Maven and are required in the current EAR, but Seam does not need to know about those modules) and removed from the Interceptor's list of modules to manage.

Listing 6. ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
         version="3.0">
   <interceptors>
     <interceptor>
       <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
     </interceptor>
   </interceptors>
   <assembly-descriptor>
      <interceptor-binding>
         <ejb-name>*</ejb-name>
         <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
      </interceptor-binding>
   </assembly-descriptor>
</ejb-jar>

Seam's standard interceptor class, org.jboss.seam.ejb.SeamInterceptor, is declared in Listing 6. In the assembly descriptor, the * would indicate all packaged EJB modules. You would need to explicitly declare all EJB modules under the control of the interceptor. (It's a shame that you can't exclude EJB modules by name; the descriptor file would be a lot shorter.)

Listing 7 shows the persistence.xml file, which is really the application's gateway into the application server's data source configurations. You can see that Hibernate is the default persistence provider, with a reference to a configured data source (in the JBoss application server). The file is short, with no real reference to Seam-specific details.

Listing 7. persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
   <persistence-unit name="userDatabase">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/DefaultDS</jta-data-source>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

Note that Seam.properties is an empty file, but it needs to be present because Seam will not work without it.

Figure 4 is a look at the finally packaged ejbs.jar module.

The ejbs.jar module file structure
Figure 4. The ejbs.jar module file structure (click to enlarge)

And Listing 8 shows the pom file for the ejb-jar project.

Listing 8. pom.xml

<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>root.project</groupId>
   <artifactId>ejbs</artifactId>
   <packaging>ejb</packaging>
   <version>1.0</version>
   <name>enterprise java beans</name>
    <build>
  <sourceDirectory>src/main/java</sourceDirectory>
      <plugins>
         <plugin>
            <artifactId>maven-ejb-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      </plugins>
   </build>
    <dependencies>
   ...
  </dependencies>
</project>

Note that the dependencies section was moved below the build configuration and tree-collapsed to help you better understand the maven-ejb-plugin configuration.

1 2 3 4 5 6 7 Page 4
Page 4 of 7