Tuesday, August 7, 2012

How to create java war (web archive) file using Maven?

Maven by default creates a JAR package. But we can build package in any other format easily through Maven. This blog explains how to generate a WAR package, customizing the MANIFEST and web.xml files within it, using simple HelloWorld example.

Project structure
<proj-home>
        pom.xml
        src
          main
            java
               App.java
        target
           simple-1.1-SNAPSHOT.jar


First we create a Java file which prints Hello World.
  Maven projects expects Java source files under  src/main/java directory. Hence lets create our Hellow World program App.java under it.

The content of App.java is


public class App
{
        public static void main (String args[])
        {
                System.out.println("Hello World");
        }
}

Create a simple pom.xml file to compile, package (generate JAR file by default)
   Create the below pom.xml file under <proj-home>
   <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.rsa.siddesh.simple</groupId>
        <artifactId>simple</artifactId>
        <packaging>jar</packaging>
        <version>1.1-SNAPSHOT</version>
        <name>simple</name>
    </project>

Compile the Simple project
   Run the command
        mvn package

   On it's successful completion it will create simple-1.1-SNAPSHOT.jar file under <proj-home>/target directory.

Creating WAR package
  As explained above, maven created by default the JAR package. Now its time create a WAR package for the same project. A web application (war) needs to have some additional files like web.xml, jsp files, servlet, images, etc. The below directory structure depicts the needy files for one of the sample web application. I took this sample application code from Spring Source site
 http://static.springsource.com/projects/tc-server/6.0/getstart/tgsdevtut.html
This site explains building web app using Ant, but I migrated the same code to build with Maven.

<proj-home>
        pom.xml
        src
          main
            java
               examples
                  Hello.java
            resources
               images
                  springsource.png
           webapp
               images
                  springsource.png
               jsp
                  hello.jsp
              WEB-INF
                  web.xml             
         target 
             simple-1.1-SNAPSHOT.war                    

       
Now lets look at the code for all source files mentioned above.
pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.rsa.siddesh.simple</groupId>
  <artifactId>simple</artifactId>
  <packaging>war</packaging>
  <version>1.1-SNAPSHOT</version>
  <name>simple</name>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>
</project>


Hello.java - It's a Java servlet file.

package examples;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Simple Hello servlet.
 */

public final class Hello extends HttpServlet {


    /**
     * Respond to a GET request for the content produced by
     * this servlet.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are producing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();        
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>Sample Application Servlet Page</title>");
        writer.println("</head>");
        writer.println("<body bgcolor=white>");

        writer.println("<table border=\"0\" cellpadding=\"10\">");
        writer.println("<tr>");
        writer.println("<td>");
        writer.println("<img src=\"images/springsource.png\">");
        writer.println("</td>");
        writer.println("<td>");
        writer.println("<h1>Sample Application Servlet</h1>");
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("</table>");

        writer.println("This is the output of a servlet that is part of");
        writer.println("the Hello, World application.");

        writer.println("</body>");
        writer.println("</html>");
    }
}

springsource.png - Download this image from springsource web link given above
hello.jsp

<html>
  <head>
    <title>Sample Application JSP Page</title>
  </head>

  <body bgcolor=white>

  <table border="0" cellpadding="10">
    <tr>
      <td align=center>
        <img src="../images/springsource.png">
      </td>
      <td>
         <h1>Sample Application JSP Page</h1>
      </td>
    </tr>
  </table>

  <br />
  <p>This is the output of a JSP page that is part of the HelloWorld application.</p>

  <%= new String("Hello!") %>

  </body>
</html>


web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>examples.Hello</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>



Run the command
     mvn package

It will create a war file target/simple-1.1-SNAPSHOT.war, which can be deployed on web server like Apache tomcat. A war file contains MANIFEST files which is created by Maven and also the web.xml files.
Customizing it to add our own values is described in next blog








No comments: