Integrating Seam with Maven, Netbeans and GlassFish


The application that resulted from my previous article on Maven, Netbeans and GlassFish uses Hibernate for JPA and Facelets for JSF. This is an ideal situation to make the move to Seam. In the past few weeks I have spent some time on using Seam in GlassFish applications but I haven’t been very successful. However, the jpa example that is shipped with the Seam 2.0.0 GA distribution shows that working with Seam on Glassfish can be very simple. I just built the GlassFish war suing ant and deployed that war to GlassFish. The application just ran without any problems at all! Today I finally managed to build a Seam application for GlassFish. Moving that application to Maven then was very easy. This article will tell you how to do it.

I will assume that you have followed the instructions for
creating a Maven enabled Hibernate/Facelets application in Netbeans. If you haven’t then that’s no problem. This article should provide you with enough hints to enable Seam on any Enterprise Application created with Netbeans.


Moving the EJB module to Seam

The number of jars that are needed to enable Seam on the EJB module is very very small: just 1. It is the jboss-seam.jar file. The reason only this jar is needed, is that the jars, that the jboss-seam.jar depends on, already have been installed into the GlassFish lib directory when we moved from Toplink Essentials to Hibernate. These jars are dom4j.jar, el-api.jar, hibernate-validator.jar and javassist.jar. Well, the el-api.jar file classes are actually contained in the GlassFish javaee.jar file. Besides these jars, the jboss-seam.jar file also depends on the jboss-el.jar file. We won’t need this jar for the application that we’ll build in this article, but you may want to include that jar anyway in case you’d want to use EL expressions in your entity classes.

Please note that I have updated the list of jars files needed for Hibernate in my previous article. You should now also copy the hibernate-validator.jar file from the Hibernate EntityManager distribution to the GlassFish lib directory. If you haven’t done so, please do it now!

Before we can make Maven download the seam jars, we need to add another repository to our pom.xml file. Since this repository will also be used by the WAR module, I have included it in the Parent Pom Module pom.xml file. The lines to add are these

        &lt;repository&gt;<br />            &lt;id&gt;;/id&gt;<br />            &lt;name&gt;;/name&gt;<br />            &lt;url&gt;;/url&gt;<br />        &lt;/repository&gt;<br />

Next, these lines in the EJB module pom.xml file will make Maven download the needed jars

        &lt;dependency&gt;<br />            &lt;groupId&gt;org.jboss.seam&lt;/groupId&gt;<br />            &lt;artifactId&gt;jboss-seam&lt;/artifactId&gt;<br />            &lt;version&gt;2.0.0.GA&lt;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;groupId&gt;dom4j&lt;/groupId&gt;<br />                    &lt;artifactId&gt;dom4j&lt;/artifactId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;el-api&lt;/artifactId&gt;<br />                    &lt;groupId&gt;javax.el&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;javassist&lt;/artifactId&gt;<br />                    &lt;groupId&gt;jboss&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />

At this stage you may want to Clean And Build the project so the jars get downloaded and Netbeans is capable of locating the classes in the jars. Once done we can annotate our DataBean session bean with the @Name annotation. Besides that, we can switch over to Seam managed persistency instead of container managed persistency. This means that we need to replace the @PersistenceContext annotation with the @In annotation.

Besides that we’ll need to adjust the DataBean class to conform the Java Bean design guidelines. These guidelines state that Beans contain (private) member variables with getters and setters. To be able to keep on using the "countEmployees" call in our index.xhtml file, we’ll need to introduce a (private) int countEmployees and rename the countEmployees() method to getCountEmployees(). Please note that renaming the method in the DataBean class means that you’ll have to rename the countEmployees method definition in the DataLocal interface as well!

The final DataBean class looks like this

package nl.amis.maven.enterprise.ejb.session;<br /><br />import javax.ejb.Stateless;<br />import javax.persistence.EntityManager;<br />import org.jboss.seam.annotations.In;<br />import org.jboss.seam.annotations.Name;<br /><br />@Stateless<br />@Name(&quot;dataBean&quot;)<br />public class DataBean implements DataLocal {<br />    <br />    @In EntityManager em;<br />    <br />    private int countEmployees;<br />    <br />    public int getCountEmployees() {<br />        return em.createNamedQuery(&quot;Employees.getAll&quot;).getResultList().size();<br />    }<br />    <br />}<br />

That nearly completes the modifications that the EJB module needs. We only need to create two more files, one of which is empty, and modify our persistence.xml file. Lets start with the empty file.

Expand the Other Sources -> resources -> META-INF node. Right click the META-INF node and select New -> Other … and choose Properties File from the Other category. Name this file "seam" (without the queotes and without the .properties extension) and click Finish. This file will trigger Seam to initiate and load all classes and resources in the EJB module.

While we’re in this node, let’s create the second file. This file will be an XML file (New -> Other … and choose XML document from the XML Category) and name this file "ejb-jar". This file will contain some Seam specific interceptor definitions. The contents of this file should be

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br /><br />&lt;ejb-jar xmlns = &quot;; <br />         version = &quot;3.0&quot; <br />         xmlns:xsi = &quot;; <br />         xsi:schemaLocation = &quot;;&gt;<br />    <br />    &lt;interceptors&gt;<br />        &lt;interceptor&gt;<br />            &lt;interceptor-class&gt;org.jboss.seam.ejb.SeamInterceptor&lt;/interceptor-class&gt;<br />        &lt;/interceptor&gt;<br />    &lt;/interceptors&gt;<br />    <br />    &lt;assembly-descriptor&gt;<br />        &lt;interceptor-binding&gt;<br />            &lt;ejb-name&gt;*&lt;/ejb-name&gt;<br />            &lt;interceptor-class&gt;org.jboss.seam.ejb.SeamInterceptor&lt;/interceptor-class&gt;<br />        &lt;/interceptor-binding&gt;<br />    &lt;/assembly-descriptor&gt;<br />    <br />&lt;/ejb-jar&gt;<br />

Finally, we need to modify the persistence.xml file to define Seam managed persistency. Double click the persistence.xml file in the META-INF node and make the contents look like this

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;persistence version=&quot;1.0&quot; xmlns=&quot;; <br />                           xmlns:xsi=&quot;; <br />                           xsi:schemaLocation=&quot;;&gt;<br />    &lt;persistence-unit name=&quot;MavenEnterpriseApplicationEjbPU&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;<br />        &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;<br />        &lt;non-jta-data-source&gt;jdbc/hr&lt;/non-jta-data-source&gt;<br />        &lt;properties/&gt;<br />    &lt;/persistence-unit&gt;<br />&lt;/persistence&gt;<br />

Please note two important things. first of all, we switched from transaction type JTA to RESOURCE_LOCAL. This means that Seam will be in charge of the transactions etc. This means that we will need to modify our data source definition from jta-data-source to non-jta-data-source. Besides that I slightly modified the persistence-unit name. It was MavenEnterpriseApplication-ejbPU but unfortunately Seam won’t accept the dash ("-") in the name.

That’s it for the EJB


Moving the WAR module to Seam

Moving the WAR project to Seam also isn’t very complicated. First we need to include the needed dependencies in our pom.xml file. The WAR module needs two of them: jboss-seam-ui.jar and jboss-el.jar. Again, these two jars depend on several jars that already have been installed in the GlassFish lib dir, so we may exclude them. Add these lines to your pom.xml file

        &lt;dependency&gt;<br />            &lt;groupId&gt;org.jboss.seam&lt;/groupId&gt;<br />            &lt;artifactId&gt;jboss-seam-ui&lt;/artifactId&gt;<br />            &lt;version&gt;2.0.0.GA&lt;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;commons-beanutils&lt;/artifactId&gt;<br />                    &lt;groupId&gt;commons-beanutils&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;dom4j&lt;/artifactId&gt;<br />                    &lt;groupId&gt;dom4j&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;el-api&lt;/artifactId&gt;<br />                    &lt;groupId&gt;javax.el&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;javassist&lt;/artifactId&gt;<br />                    &lt;groupId&gt;jboss&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;jboss-seam&lt;/artifactId&gt;<br />                    &lt;groupId&gt;org.jboss.seam&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;jsf-api&lt;/artifactId&gt;<br />                    &lt;groupId&gt;javax.faces&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />        <br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.jboss.seam&lt;/groupId&gt;<br />            &lt;artifactId&gt;jboss-el&lt;/artifactId&gt;<br />            &lt;version&gt;2.0.0.GA&lt;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;el-api&lt;/artifactId&gt;<br />                    &lt;groupId&gt;javax.el&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />

Clean and Build the project so the jars get downloaded. Doing so will make Maven resolve all dependencies. This will make Maven see that the EJB module depends on jboss-seam.jar and this library will show up in the list. however, this jar is not needed here. If you like you can exclude it by adding these lines to the EJB modulle dependency in the WAR module pom.xml file

            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;groupId&gt;org.jboss.seam&lt;/groupId&gt;<br />                    &lt;artifactId&gt;jboss-seam&lt;/artifactId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />

Since Seam will be in charge on injecting (and outjecting) bean references, we won’t need the Managed Bean reference in the faces-config.xml file anymore. So, expand the Web Pages -> WEB-INF node and remove the managed bean code from the faces-config.xml file. For Seam to be able to resolve the bean references correctly, the web.xml file also needs to be modified. Open the file and add these lines at the top of the file

    &lt;listener&gt;<br />        &lt;listener-class&gt;org.jboss.seam.servlet.SeamListener&lt;/listener-class&gt;<br />    &lt;/listener&gt;<br />

Finally, Seam needs another configuration file called components.xml. This file will, among onther things, enable Seam to setup the persistence unit and to find the session bean in our EJB module. Simply create another XML Document in the WEB-INF directory and name it "components". This is the components.xml file I stole from the Seam jpa example

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;components xmlns=&quot;;<br />            xmlns:core=&quot;;<br />            xmlns:persistence=&quot;;<br />            xmlns:xsi=&quot;;<br />            xsi:schemaLocation=<br />                &quot; <br />       <br />       ;&gt;<br /><br />    &lt;persistence:entity-manager-factory name=&quot;MavenFaceletsSeamEjbPU&quot;/&gt;<br /><br />    &lt;persistence:managed-persistence-context name=&quot;em&quot;<br />        auto-create=&quot;true&quot; <br />        entity-manager-factory=&quot;#{MavenFaceletsSeamEjbPU}&quot;/&gt;<br />    <br />    &lt;core:init jndi-pattern=&quot;java:comp/env/#{ejbName}&quot; /&gt;<br />    <br />&lt;/components&gt;<br />

Please note that the persistence name should be the same as in the persistence.xml file in your EJB module. Besides that, Seam won’t be able to resolve the JNDI names of session beans on GlassFish. I’m not sure why this is so, but Seam NEEDS the jndi-pattern line. The pattern can be anything you like. For me the "java:comp/env/#{ejbName}" pattern will make the DataBean bean be registered under the JNDI name "java:comp/env/DataBean" as this GlassFish log shows

Component: org.jboss.seam.core.init, scope: APPLICATION, type: JAVA_BEAN, class: org.jboss.seam.core.Init<br />Installing components...<br />Component: MavenEnterpriseApplicationEjbPU, scope: APPLICATION, type: JAVA_BEAN, class: org.jboss.seam.persistence.EntityManagerFactory<br />Component: dataBean, scope: STATELESS, type: STATELESS_SESSION_BEAN, class: nl.amis.maven.enterprise.ejb.session.DataBean, JNDI: java:comp/env/DataBean<br />Component: em, scope: CONVERSATION, type: JAVA_BEAN, class: org.jboss.seam.persistence.ManagedPersistenceContext<br />

There is one more catch to resolve. Since our session bean is packaged in a jar file, the reference to the bean needs to be made explicit in web.xml. This can be done with a "ejb-local-ref" element like this

    &lt;ejb-local-ref&gt;<br />        &lt;ejb-ref-name&gt;DataBean&lt;/ejb-ref-name&gt;<br />        &lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;     <br />        &lt;local&gt;nl.amis.maven.enterprise.ejb.session.DataLocal&lt;/local&gt;<br />    &lt;/ejb-local-ref&gt;<br />

That’s it! The ejb-ref-name element makes sure that the "#{ejbName}" part in the jndi-pattern line in the components.xml file is referring to the right value. Please note that this needs to be done for ALL beans that are packaged in the EJB module jar and that need to be references from your xhtml pages. That was easy right?

O wait, one more thing and then we’re finished. Promise. We need to modify the index.xhtml file to make use of the DataBean bean instead of the NumberBackingBean. So, open the index.xhtml file and modify the EL expression to


Once more rebuild the application and now fire the exec:exec goal on the EAR module. Once your application has been deployed, browse to e.g. http://localhost:8280/MavenEnterpriseApplication-war/index.jsf and you should once more see

Next steps

Since Seam now is in charge of the transaction and Entity Manager management, the Hibernate jars are no longer needed in the GlassFish lib direct
. You may remove them and then restart GlassFish. This will mean that you need to deploy those jars with your application. This, of course, can be done by modifying the pom.xml files and add the correct dependencies. To do this, I used the list below in the EJB pom.xml and made no modifications to the WAR pom.xml file. Please note that the list below only replaces the Hibernate dependencies. No modifications are needed for the Seam dependencies. Adding the dependencies to the EJB pom.xml will make Maven include the jars both in the EJB jar and in the WAR thus making sure that both modules have the jars they need. It would help to clean up the redundant jars but I’ll leave that up to you to do Smiley

    &lt;dependencies&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br />            &lt;artifactId&gt;hibernate&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;groupId&gt;javax.transaction&lt;/groupId&gt;<br />                    &lt;artifactId&gt;jta&lt;/artifactId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br />            &lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br />            &lt;artifactId&gt;hibernate-commons-annotations&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br />            &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;jboss-common-core&lt;/artifactId&gt;<br />                    &lt;groupId&gt;jboss&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br />            &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;antlr&lt;/groupId&gt;<br />            &lt;artifactId&gt;antlr&lt;/artifactId&gt;<br />            &lt;version&gt;2.7.7&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;asm&lt;/groupId&gt;<br />            &lt;artifactId&gt;asm&lt;/artifactId&gt;<br />            &lt;version&gt;1.5.3&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;asm&lt;/groupId&gt;<br />            &lt;artifactId&gt;asm-attrs&lt;/artifactId&gt;<br />            &lt;version&gt;1.5.3&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;c3p0&lt;/groupId&gt;<br />            &lt;artifactId&gt;c3p0&lt;/artifactId&gt;<br />            &lt;version&gt;0.9.1&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;cglib&lt;/groupId&gt;<br />            &lt;artifactId&gt;cglib&lt;/artifactId&gt;<br />            &lt;version&gt;2.1_3&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;commons-collections&lt;/groupId&gt;<br />            &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;<br />            &lt;version&gt;2.1.1&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;commons-logging&lt;/groupId&gt;<br />            &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;<br />            &lt;version&gt;1.1&lt;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;avalon-framework&lt;/artifactId&gt;<br />                    &lt;groupId&gt;avalon-framework&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;log4j&lt;/artifactId&gt;<br />                    &lt;groupId&gt;log4j&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;logkit&lt;/artifactId&gt;<br />                    &lt;groupId&gt;logkit&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;<br />                    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;concurrent&lt;/groupId&gt;<br />            &lt;artifactId&gt;concurrent&lt;/artifactId&gt;<br />            &lt;version&gt;1.3.4&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;dom4j&lt;/groupId&gt;<br />            &lt;artifactId&gt;dom4j&lt;/artifactId&gt;<br />            &lt;version&gt;1.6.1&lt;/version&gt;<br />            &lt;exclusions&gt;<br />                &lt;exclusion&gt;<br />                    &lt;artifactId&gt;xml-apis&lt;/artifactId&gt;<br />                    &lt;groupId&gt;xml-apis&lt;/groupId&gt;<br />                &lt;/exclusion&gt;<br />            &lt;/exclusions&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;<br />            &lt;artifactId&gt;ehcache&lt;/artifactId&gt;<br />            &lt;version&gt;1.2.3&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;jboss&lt;/groupId&gt;<br />            &lt;artifactId&gt;javassist&lt;/artifactId&gt;<br />            &lt;version&gt;;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;jboss&lt;/groupId&gt;<br />            &lt;artifactId&gt;jboss-archive-browsing&lt;/artifactId&gt;<br />            &lt;version&gt;5.0.0alpha-200607201-119&lt;/version&gt;<br />        &lt;/dependency&gt;<br />        &lt;dependency&gt;<br />            &lt;groupId&gt;javax.ejb&lt;/groupId&gt;<br />            &lt;artifactId&gt;ejb-api&lt;/artifactId&gt;<br />            &lt;version&gt;3.0&lt;/version&gt;<br />            &lt;scope&gt;provided&lt;/scope&gt;<br />        &lt;/dependency&gt;<br />


About Author


  1. Wouter van Reeven on

    Hi. Please check out the NB Facelets project at The plugin now has a release for NB 6.0 which means there is code completion and there is a Facelets Palette. No visual editing yet, but the Visual Web module of Netbeans allows for visual CSS editing.

  2. Thanks for your post. I’ve been developing with Seam since v 1.0 and i must say that i love it. However working with developing in Seam on NetBeans can now not be compared to doing the same on Eclipse since the release of the JBoss Tools plugin. What efforts are being made by Sun to support Seam? After all it’s the learning ground for those of us interested in the Web Beans specification, which has the full blessing of Sun.