Sunday, May 17, 2009

Simple Webapp Using Maven and Tomcat

As far as I can tell, the projects Maven will generate are complex and thus not suited to beginners or for simple projects.  Here we will be creating the simplest possible web app using some nice features Maven provides.

The directory structure we will use has a root directory we will name with the project name.  The necessary files are the pom.xml, App.java and web.xml.
mavenquickstart
--pom.xml
--src
----main
------java
--------com
----------bookstore
------------App.java
------webapp
--------WEB-INF
----------web.xml

The pom.xml file is as follows.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mavenquickstart</groupId>
  <artifactId>mavenquickstart</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mavenquickstart</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>mavenquickstart</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
          <url>http://localhost:8080/manager</url>
          <server>myserver</server>
          <warFile>
             ${CATALINA_HOME}/webapps/mavenquickstart.war
          </warFile>
        </configuration>
       </plugin> 
    </plugins>
  </build>
</project>
The web.xml file:
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>simple</servlet-name>
    <servlet-class>com.bookstore.App</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>simple</servlet-name>
    <url-pattern>/simple</url-pattern>
  </servlet-mapping>
</web-app>
You may need to create the settings.xml file in your .m2 folder.  Add the necessary parts of this XML into the settings.xml file.  (Remember, this is Maven code, only for the developer's workstation so this solution does not expose user-ids and passwords in the runtime environment.)
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
        <id>myserver</id> 
        <username>Tomcat-Manager-Userid</username>
        <password>Tomcat-Manager-Password</password>
    </server>
  </servers>
</settings>
A very simple servlet.
package com.bookstore;
import java.io.*;
import javax.servlet.*;                                                         
import javax.servlet.http.*;
/**
 *  report on status of servlet 
 */
public class App extends HttpServlet {
    public void doGet( HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println( "SimpleServlet Executed" );
        out.flush();
        out.close();
    }
}

Run the appropriate command to compile and deploy or redeploy the application.

mvn clean package tomcat:deploy
or

mvn clean package tomcat:redeploy
To view the output from the servlet, navigate your browser to http://localhost:8080/mavenquickstart/simple.