Saturday, April 4, 2009

A Simple JSF Application Using Eclipse, Maven, and Tomcat

For this example, you will need Eclipse IDE (of course, you need to have Java installed), if you don't have Eclipse, you can downloaded it here (choose the "Eclipse IDE for Java EE Developers"). You will also need Maven plugin for Eclipse, to install see here. Now, create a Dynamic Web Project from Eclipse select File-->New-->Project. Then select Web-->Dynamic Web Project Click Next Specify a Project Name, change Dynamic Web Module version to 2.5, and Configuration to JavaServer Faces v1.2 Project. Click Next Change Content Directory to src/main/webapp and Java Source Directory to src/main/java. Click Next Finally, click Finish Now that we have a JSF enabled web project we need to enable Maven for this project. To enable Maven right click on the project and select Maven-->Enable Dependency Management. A new dialogue will appear change Packaging to war and click Finish. Now, we need to tell Maven what are the dependencies of this project. The dependencies are JSF API, JSF Implementation, Servlet API, and JSTL. So, open the pom.xml source file. Inside the <dependencies> tag add the following:
<dependency>
    <groupid>javax.faces</groupid>
    <artifactid>jsf-api</artifactid>
    <version>1.2_09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupid>javax.faces</groupid>
    <artifactid>jsf-impl</artifactid>
    <version>1.2_09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>servlet-api</artifactid>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupid>jstl</groupid>
    <artifactid>jstl</artifactid>
    <version>1.1.2</version>
</dependency>
Save the file. Maven will automatically download and add the above dependencies to your classpath. Maven may complain about not being able to find some dependencies, we will deal with this problem afterward. After specifying the dependencies, we need to tell Maven how should it compile, package and run the application. So, we will add the following tags:
<build>
<plugins>
</plugins>
</build>
First we will specify the Maven plugin that's responsible of compiling the Java sources. Inside the <plugins> tag we will add the following:
<plugin>
        <artifactid>maven-compiler-plugin</artifactid>
        <configuration>
            <target>1.5</target>
            <source>1.5</source>
        </configuration>
</plugin>
Second we will add the Maven plugin that's responsible of packaging and creating the war file.
<plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-war-plugin</artifactid>
        <version>2.0</version>
</plugin>
Finally, we add the Maven plugins responsible running the container and deploying our application.
<plugin>
        <groupid>org.codehaus.mojo</groupid>
        <artifactid>tomcat-maven-plugin</artifactid>
        <configuration>
            <url>http://localhost:8080/manager/html</url>
         </configuration>
</plugin>
<plugin>
        <groupid>org.codehaus.cargo</groupid>
        <artifactid>cargo-maven2-plugin</artifactid>
</plugin>
In this example I'm using tomcat as a container, therefore I used the tomcat-maven-plugin. If you want to use another container see the cargo website. You may need to add the following repositories to the pom.xml file, so that Maven can download the dependencies and plugins used in this example:
<repositories>
  <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/1
      </url>
      <layout>legacy</layout>
  </repository>
  <repository>
      <id>codehaus</id>
      <name>Codehaus maven repository</name>
      <url>http://dist.codehaus.org/</url>
      <layout>legacy</layout>
  </repository>
</repositories>
Its time to run the application. From Eclipse select Run-->Run Configurations and double click on Maven Build. Specify a name for this configuration and click on Browse Workspace and choose this project. In the Goals field enter, clean package tomcat:run. Click Apply and then Run. Maven will compile, package, and deploy the application to an embedded tomcat (if you want to deploy your application to a stand-alone tomcat see here). As a result a new folder named target will be created which contains tomcat, the war directory, and the war file. Now under src/main/webapp create a JSF page and test it. You can download the sample project here.

No comments:

Post a Comment