Using Ant to build and deploy webapps
A possible structure for a web app development folder
What you need to do
What goes where?
What will go where?
The build script: top_level\build.xml
<?xml
version="1.0"?>
<!-- ====================================================
-->
<!-- Build file for our first
web application -->
<project name="AntWarWebApp"
default="all" basedir=".">
<property name="dirs.base"
value="${basedir}"/>
<property name="classdir"
value="${dirs.base}/build/src"/>
<property name="src"
value="${dirs.base}/src"/>
<property name="web" value="${dirs.base}/web"/>
<property
name="tomcat.webapps"
value="C:\tomcat6.018\apache-tomcat-6.0.18\webapps" />
<property name="warFile"
value="AntWarWebApp.war"/>
<property name="distDir"
value="${dirs.base}/build/dist"/>
<property name="warDir"
value="${dirs.base}/build/war"/>
<property name="srcDir"
value="${dirs.base}/build/src"/>
<property name="deploymentdescription"
value="${dirs.base}/build/deploymentdescriptors"/>
<target
name="clean"
description="Delete old build
directories">
<delete
dir="${distDir}"/>
<delete
dir="${warDir}"/>
<delete dir="${srcDir}"/>
</target>
<target name="init" depends=
"clean">
<!-- Create Web-inf and classes directories -->
<mkdir
dir="${warDir}"/>
<mkdir
dir="${distDir}"/>
<mkdir
dir="${srcDir}"/>
<mkdir
dir="${warDir}/WEB-INF"/>
<mkdir
dir="${warDir}/WEB-INF/classes"/>
</target>
<!-- Main target -->
<target name="all" depends="init,build,buildWar"/>
<!-- Compile Java Files and
store in /build/src directory -->
<target name="build" >
<echo message="compiling servlet
...."/>
<javac
srcdir="${src}" destdir="${classdir}"
debug="true" includes="**/*.java" />
</target>
<!-- Create the War Dir and War
file -->
<target name="buildWar"
depends="init,build">
<echo message="creating war file...."/>
<copy todir="${warDir}/WEB-INF/classes">
<fileset
dir="${classdir}"
includes="**/*.class" />
</copy>
<copy todir="${warDir}/WEB-INF">
<fileset
dir="${deploymentdescription}"
includes="web.xml" />
</copy>
<copy todir="${warDir}">
<fileset
dir="${web}" includes="**/*.*" />
</copy>
<!--
Create war file and place in dist directory -->
<jar jarfile="${distDir}/${warFile}" basedir="${warDir}"
/>
<copy todir="${tomcat.webapps}">
<fileset dir="${distDir}"
/>
</copy>
</target>
</project>
My index.jsp:
<%@page language="java" %>
<html>
<head>
<title>Welcome to ant
tutorial</title>
</head>
<body bgcolor="#FFFFCC">
<p align="center"><font
color="#000080" size="4">Congralutations
you have successfully
installed your war file
</font></p>
<p align="center"><font
color="#000080" size="4"><a href="/AntWarWebApp/AntWarWebApp">Click here
to execute Hello World Servlet.</a> </font></p>
</body>
</html>
My java file:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AntWarWebApp
extends HttpServlet {
public
void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n"
+
"<HEAD><TITLE>A webapp
deployed to default webapps by
ant</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello from Higgins in the world of servlets...
using ANT to deploy webapps</H1>\n" +
"</BODY></HTML>");
}}
Running Ant in DOS (Convenient to have a shortcut in
the toplevel dir)
C:\AntWarWebApp>ant
Buildfile: build.xml
clean:
[delete]
Deleting directory C:\AntWarWebApp\build\dist
[delete]
Deleting directory C:\AntWarWebApp\build\war
[delete]
Deleting directory C:\AntWarWebApp\build\src
init:
[mkdir] Created dir: C:\AntWarWebApp\build\war
[mkdir] Created dir:
C:\AntWarWebApp\build\dist
[mkdir] Created dir:
C:\AntWarWebApp\build\src
[mkdir] Created dir:
C:\AntWarWebApp\build\war\WEB-INF
[mkdir] Created dir:
C:\AntWarWebApp\build\war\WEB-INF\classes
build:
[echo] compiling servlet ....
[javac] Compiling 1 source file to
C:\AntWarWebApp\build\src
buildWar:
[echo] creating war file....
[copy] Copying 1 file to
C:\AntWarWebApp\build\war\WEB-INF\classes
[copy] Copying 1 file to C:\AntWarWebApp\build\war\WEB-INF
[copy] Copying 1 file to C:\AntWarWebApp\build\war
[jar] Building jar:
C:\AntWarWebApp\build\dist\AntWarWebApp.war
[copy] Copying 1 file to
C:\tomcat6.018\apache-tomcat-6.0.18\webapps
all:
BUILD SUCCESSFUL
Total time: 5 seconds
C:\AntWarWebApp>
Running the WebApp