Archive for October 2nd, 2007

JBoss Seam project setup with Maven — Part 1: WAR deployment

Tuesday, October 2nd, 2007

Pete Muir recently posted that Seam 2.0 will support Maven integration. It is perfect timing for me since I have just started to use Maven in my new projects. So, I went through the exercise to setup simple Seam web projects in Maven. It is really quite easy. The project builds a single WAR for deployment (you can only use Seam POJO in the WAR -- no EJBs). You can configure it to generate WAR for JBoss AS or Tomcat 6. The beauty of using Maven is that you do not need to worry about things like finding the right version of JARs, setting up the project structure, managing build script etc. In fact, there is no build script to write. So, here is the structure of the source project.

CODE:
  1. .
  2.  |-- pom.xml
  3.  `-- src
  4.      `-- main
  5.          |-- java
  6.          |   `-- com
  7.          |       `-- example
  8.          |             `-- SampleAction.java (and other Java source files)
  9.          |-- resources
  10.          |   |-- META-INF
  11.          |   |   `-- persistence.xml
  12.          |   `-- seam.properties
  13.          `-- webapp
  14.              |-- WEB-INF
  15.              |    | -- components.xml
  16.              |    | -- pages.xml
  17.              |    | -- face-config.xml
  18.              |     `-- web.xml
  19.              `-- hello.xhtml (and other web content, CSS, templates, and images)

As you can see, you put your application code and configuration in the following directories.

  • All the Java source code (e.g., entity beans, Seam POJOs) go into src/main/java.
  • All Seam and web-related configuration files go into src/main/webapp/WEB-INF.
  • The JPA configuration goes into resources/META-INF/persistence.xml.
  • All web content goes into src/main/webapp.
  • The resources/seam.properties file is an empty file. It is require for Seam to scan annotations in this WAR

The dependency JARs are declared in the pom.xml file. Below is the pom.xml I used to generate a WAR for JBoss AS deployment. A couple of things to notice here:

  • I set the Seam official POM as my "parent" so that I can let Seam figure out the correct versions of the dependency JARs. Notice that I do not need to specify the Facelets JAR version in my POM. Seam will choose a compatible version for me.
  • The compiler setting must be "1.5" so that it can handle annotations.
  • The javax.el.el-api jar needs to be in the provided scope as it conflicts with the one already in JBoss and Tomcat 6.

That's it. Here is the pom.xml.

XML:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.  
  5.   <repositories>
  6.     <repository>
  7.       <id>jboss-snapshot</id>
  8.       <name>The JBoss maven repo</name>
  9.       <url>http://snapshots.jboss.org/maven2</url>
  10.     </repository>
  11.   </repositories>
  12.  
  13.   <parent>
  14.     <groupId>org.jboss.seam</groupId>
  15.     <artifactId>root</artifactId>
  16.     <version>2.0.0-SNAPSHOT</version>
  17.   </parent>
  18.  
  19.   <modelVersion>4.0.0</modelVersion>
  20.  
  21.   <groupId>mytest</groupId>
  22.   <artifactId>seam-maven</artifactId>
  23.   <name>helloworld</name>
  24.   <version>1.0</version>
  25.   <packaging>war</packaging>
  26.   <url>http://maven.apache.org</url>
  27.  
  28.   <build>
  29.     <finalName>helloworld</finalName>
  30.     <plugins>
  31.       <plugin>
  32.         <groupId>org.apache.maven.plugins</groupId>
  33.         <artifactId>maven-compiler-plugin</artifactId>
  34.         <configuration>
  35.           <source>1.5</source>
  36.           <target>1.5</target>
  37.         </configuration>
  38.       </plugin>
  39.     </plugins>
  40.   </build>
  41.  
  42.   <dependencies>
  43.  
  44.     <dependency>
  45.       <groupId>org.jboss.seam</groupId>
  46.       <artifactId>jboss-seam</artifactId>
  47.     </dependency>
  48.  
  49.     <dependency>
  50.       <groupId>org.jboss.seam</groupId>
  51.       <artifactId>jboss-el</artifactId>
  52.       <exclusions>
  53.         <exclusion>
  54.           <groupId>javax.el</groupId>
  55.           <artifactId>el-api</artifactId>
  56.         </exclusion>
  57.       </exclusions>
  58.     </dependency>
  59.  
  60.     <dependency>
  61.       <groupId>org.jboss.seam</groupId>
  62.       <artifactId>jboss-seam-ui</artifactId>
  63.     </dependency>
  64.  
  65.     <dependency>
  66.       <groupId>com.sun.facelets</groupId>
  67.       <artifactId>jsf-facelets</artifactId>
  68.     </dependency>
  69.  
  70.   </dependencies>
  71.  
  72. </project>

Now, what if I want the WAR to be deployed in plain Tomcat 6? No problem, just add the JSF and Hibernate dependencies as follows. There is no need to meddle with commons jars and other 3rd party JARs since JSF and Hibernate dependencies automatically resolve them! Of course, plain Tomcat 6 does not provide a JTA transaction manager, so you will need to provide your own or use RESOURCE_LOCAL transaction -- but you should already knew that if you choose Tomcat ...

XML:
  1. <!-- The following dependencies are needed for Tomcat 6 deployment -->
  2.  
  3.     <dependency>
  4.       <groupId>javax.faces</groupId>
  5.       <artifactId>jsf-api</artifactId>
  6.     </dependency>
  7.  
  8.     <dependency>
  9.       <groupId>javax.faces</groupId>
  10.       <artifactId>jsf-impl</artifactId>
  11.     </dependency>
  12.  
  13.     <dependency>
  14.       <groupId>org.hibernate</groupId>
  15.       <artifactId>hibernate</artifactId>
  16.     </dependency>
  17.  
  18.     <dependency>
  19.       <groupId>org.hibernate</groupId>
  20.       <artifactId>hibernate-validator</artifactId>
  21.     </dependency>
  22.  
  23.     <dependency>
  24.       <groupId>org.hibernate</groupId>
  25.       <artifactId>hibernate-annotation</artifactId>
  26.     </dependency>
  27.  
  28.     <dependency>
  29.       <groupId>org.hibernate</groupId>
  30.       <artifactId>hibernate-entitymanager</artifactId>
  31.     </dependency>

So, go and have fun with Maven now! Let me know if there is any issues!