Wednesday, January 23, 2013

Quick Start Maven archetype for simple Servlet application - Hello World

Hey Joe,

Maven is the better way to go. For a very simple "Hello World!" Servlet app, you can get it going in 5 minutes or less. Just follow my steps below.

You need to install mvn. 3..0.3 is the version that I am using.


lei:webapps lei$ mvn -version
Apache Maven 3.0.3 (r1075438; 2011-02-28 09:31:09-0800)
Maven home: /usr/local/apache-maven-3.0.3


Prerequisite: assume you have a Servlet container installed on your system. I have apache-tomcat-7 installed at /usr/local/apache-tomcat-7.0.12.

Step 1: Use Maven archetype maven-archetype-webapp to start the mvn project. 


lei:tmp lei$ mvn archetype:create -DgroupId=com.lei.webapp.quickstart  -DartifactId=webapp-quick-start   -DarchetypeArtifactId=maven-archetype-webapp  
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-archetype-plugin:2.2:create (default-cli) @ standalone-pom ---
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.lei.webapp.quickstart
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.lei.webapp.quickstart
[INFO] Parameter: packageName, Value: com.lei.webapp.quickstart
[INFO] Parameter: package, Value: com.lei.webapp.quickstart
[INFO] Parameter: artifactId, Value: webapp-quick-start
[INFO] Parameter: basedir, Value: /Users/lei/tmp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/lei/tmp/webapp-quick-start
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.860s
[INFO] Finished at: Wed Jan 23 10:55:02 PST 2013
[INFO] Final Memory: 7M/265M
[INFO] ------------------------------------------------------------------------
lei:tmp lei$ 


Step 2: Add servlet API javax.servlet as a Maven dependency to the mvn project file pom.xml. 


lei:tmp lei$ cd webapp-quick-start 
lei:webapp-quick-start lei$ vi pom.xml 

Add the following to pom.xml: 

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
</dependency>


Step 3: Add HelloServlet.java

lei:webapp-quick-start lei$ mkdir -p src/main/java/com/lei/webapp/quickstart 
lei:webapp-quick-start lei$ vi src/main/java/com/lei/webapp/quickstart/HelloServlet.java 

Add the following Java code:

package com.lei.webapp.quickstart;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// com.lei.webapp.quickstart.HelloServlet
public class HelloServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println( "Hello World!" );
        out.flush();
        out.close();
    }
}


Step 4: Modify web.xml:

lei:webapp-quick-start lei$ vi ./src/main/webapp/WEB-INF/web.xml 

Add the following content: 

<!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>HelloWorld</servlet-name>
        <servlet-class>com.lei.webapp.quickstart.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Step 5: Compile and package:

lei:webapp-quick-start lei$ mvn compile test package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building webapp-quick-start Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ webapp-quick-start ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ webapp-quick-start ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ webapp-quick-start ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ webapp-quick-start ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ webapp-quick-start ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/lei/tmp/webapp-quick-start/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ webapp-quick-start ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ webapp-quick-start ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/lei/tmp/webapp-quick-start/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ webapp-quick-start ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ webapp-quick-start ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ webapp-quick-start ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/lei/tmp/webapp-quick-start/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ webapp-quick-start ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ webapp-quick-start ---
[INFO] No tests to run.
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ webapp-quick-start ---
[INFO] Packaging webapp
[INFO] Assembling webapp [webapp-quick-start] in [/Users/lei/tmp/webapp-quick-start/target/webapp-quick-start]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/lei/tmp/webapp-quick-start/src/main/webapp]
[INFO] Webapp assembled in [148 msecs]
[INFO] Building war: /Users/lei/tmp/webapp-quick-start/target/webapp-quick-start.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.091s
[INFO] Finished at: Wed Jan 23 11:13:02 PST 2013
[INFO] Final Memory: 5M/265M
[INFO] ------------------------------------------------------------------------
lei:webapp-quick-start lei$ 

Step 6: Deploy the webapp-quick-start.war to the weapps, where you have your Tomcat installed:

lei:webapp-quick-start lei$ cp -v /Users/lei/tmp/webapp-quick-start/target/webapp-quick-start.war /usr/local/apache-tomcat-7.0.12/webapps 
/Users/lei/tmp/webapp-quick-start/target/webapp-quick-start.war -> /usr/local/apache-tomcat-7.0.12/webapps/webapp-quick-start.war


Step 7: Start the Tomcat, and verify the following URL to verify it's working. 

http://localhost:4080/webapp-quick-start/hello

As you can tell, I have my Tomcat installed on part 4080. 

Have fun and enjoy the journey.




2 comments:

  1. Well described post on quick Start Maven archetype for simple Servlet application! I read you complete post and sound it very informative. Thanks!
    trading in shares

    ReplyDelete