Environment setup

  1. Download and install Java 1.6 JDK
  2. Download and install Eclipse Indigo 32bit
  3. Install m2eclipse from Eclipse Marketplace
  4. Install TestNG plugin from Eclipse marketplace
  5. Install JBoss Tools for Eclipse through the update site.
    1. First install “All JBoss Tools”
    2. Restart Eclipse
    3. Secondly, install JBoss Maven Integration
  6. Download and unzip Seam 2.2.2.Final
  7. Open Eclipse; go to Window –> Preferences –> JBoss Tools –> Web –> Seam and add the Seam 2 installation.
  8. Download and unzip JBoss AS 7
  9. Open Eclipse, go to Window –> Preferences –> Server –> Runtime enviroments and add the JBoss AS 7 runtime environment

Project creation

  1. Create a new “Seam Web Project”
    • Target Runtime: the JBoss AS 7 runtime you added before
    • Dynamic Web module version: make sure you select 2.5, because Seam 2 doesn’t work with Servlet 3.0
    • Configuration: click Modify to select the Project Facets
      • Ensure CDI is unchecked (because Seam 2 is incompatible with CDI/Weld)
      • Ensure Dynamic Web Module is set to 2.5 (because Seam 2 is incompatible with Servlet 3.0)
      • Ensure Java is 1.6
      • Ensure JavaServer Faces is 1.2 (because Seam 2 is incompatible with JSF 2.0)
      • Ensure Seam is checked and set to 2.2
      • JPA: check if you want to enable Eclipse’s JPA tooling. This has weird side-effects in the project structure that Eclipse creates. I’ll talk in detail later.
  2. Click Next
  3. Source folders: click Next
  4. JPA Facet:
    • Platform: Hibernate (JPA 2.x)
    • JPA implementation: Library Provided by Target Runtime
    • Connection: if you checked JPA before, you must select a connection.
    • Persistent class management: Discover annotated classes automatically (for easier life)
  5. Click Next
  6. Web Module: check “Generate web.xml deployment descriptor”
  7. Click Next
  8. JBoss M2 capabilities: this tutorial assumes you choose “war” as Packaging.
  9. Click Next
  10. JSF capabilities
    • JSF implementation library: Library Provided by Target Runtime
  11. Click Next
  12. Seam Facet
    • Seam Runtime: the seam runtime you installed in “Environment Setup”
    • Deploy as: must be the same as the Packaging selected at (8 – JBoss M2 capabilities)
    • Database Type: must be the type of the database you chose at (4 – JPA Facet)
  13. Click Finish
  14. Wait a lot for Maven to download the artifacts for the first time.

Fixing the project

JBoss Tools is very messy, bugged and outdated; we’ll have to manually fix some mistakes made by our IDE.

  1. The first thing to notice is that our project will have a different directory structure, depending on whether or not you checked the “JPA” Project Facet.
  2. If we added the JPA Project Facet, the generated persistence.xml file will be empty!! WTF??!
  3. Depending on whether we added the JPA Project Facet, the pom.xml file will be different as well.
  4. If you didn’t add the JPA Facet, your pom.xml will give the following error: Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.5:add-source (execution: add-source, phase: generate-sources).  Read my other post to fix the error.

Here are some other issues that the project will have anyway, and you have to fix them manually:

  1. The Seam version in the parent project’s pom.xml will not match the version of Seam you installed. Fix it by manually changing the seam.version property.
  2. Your project will contain a WEB-INF/jboss-web.xml that is incompatible with JBoss AS 7. To make it compatible, comment out the <class-loading> element.
  3. The project is not set to export the Maven dependencies into WEB-INF/lib once deployed. To solve that, quick-fix the following warning: Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result. Select “Mark the associated raw classpath entry as a publish/export dependency” and click Finish.
  4. Seam 2 works only with hibernate-validator version 3.1.0.GA, which is included in JBoss AS 6. However, JBoss AS 7 has a newer version of hibernate-validator, that is incompativle with Seam 2. To solve this issue, open the project parent’s pom.xml and change the scope of hibernate-validator from “provided” to “compile”.
  5. JBoss Tools forget to add a couple of dependencies that Seam 2 needs. Add the following dependencies to the project:
  6. <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-servlet</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    
  7. JBoss Tools creates a datasource file (projectname/resources/projectname-ds.xml) that is meant to work with JBoss AS 6. However, in JBoss AS 7 datasources are defined in a very different way. Right-click the file and click “Unmark as deployable”
  8. Even though you selected JPA 2.0, the generated persistence.xml will be set to JPA 1.0. Fix that by setting the version attribute to 2.0 and change the .xsd to *2_0.xsd
  9. Here is a persistence.xml that will work out-of-the-box. It is set to use the ExampleDS that is already defined in JBoss AS 7. Remember to change the “myproject” string with your real project name.

    <!--?xml version="1.0" encoding="UTF-8"?--><?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_2_0.xsd"
        version="2.0">
    
        <persistence-unit name="myprojectPU" transaction-type="JTA">
    
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
            <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
                <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true"/>
                <property name="jboss.entity.manager.factory.jndi.name" value="java:/myprojectEntityManagerFactory"/>
            </properties>
    
        </persistence-unit>
    
    </persistence>
    

That’s it! Now you have setup a fully working Seam 2 project that can be deployed in JBoss AS 7.

 

2 Responses to The definitive guide to create a Seam 2 project for JBoss AS 7 in Eclipse

  1. Pinar says:

    We have created seam project with maven in jboss AS 7.1 by following your instructions. It has been deployed successfully ,but when we request any page of the application browser throws “Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.”. We have search about it and found that it is related with wrong place of the faces-config.xml but we couldn’t find any solution. Do you have any idea about this problem?
    Thanks in advance.

Leave a Reply