Ant : As Build Tool :: Ant with Web Application
Ant with WebApplication |
We’ll construct a web application with jsp and servlet and use Ant to build and assemble it for execution and distribution. We’ll have one Java classes i.e Servlet and two jsp pages. and we’ll end up with an web Archive (WAR) file that is ready to deploy on server.
In this example, We have following files as below.
F:\antExample\Example2\
+-- build.xml
+-- src\
+-- UserNameServlet.java
+-- lib\
+-- servlet-api.jar
+-- web\
+-- index.jsp
+-- welcome.jsp
+-- WEB-INF\
+-- web.xml
The .java files will need to be compiled and placed in the proper package directory hierarchy.
Examples |
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Enter your name</title>
</head>
<body>
<h3>Please enter your name</h3>
<form action="login" method="post">
<table>
<tr>
<th>Name:</th>
<th><input name="userName" type="text" /></th>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Welcome Page</title>
</head>
<body>
<%
String userName=(String)session.getAttribute("userName");
%>
<h3 align="center">Welcome <%= userName %> !!!</h3>
<a href="index.jsp">go back</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>a1</servlet-name>
<servlet-class>com.javaskool.UserNameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a1</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</w.jspme-file>
</welcome-file-list>
</web-app>
UserNameServlet.java
package com.javaskool;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Author: Anuj Verma
*/
public class UserNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
private void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException{
String userName = request.getParameter("userName");
HttpSession session = request.getSession();
session.setAttribute("userName", userName);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
}
Existing Structure of files and folder before ant execution
F:\antExample\Example3>dir /S
Volume in drive F is workspace
Volume Serial Number is E846-4436
Directory of F:\antExample\Example3
20-04-2014 18:10 <DIR> .
20-04-2014 18:10 <DIR> ..
20-04-2014 18:09 1,004 build.xml
20-04-2014 18:10 <DIR> lib
20-04-2014 18:10 <DIR> src
20-04-2014 18:10 <DIR> web
1 File(s) 1,004 bytes
Directory of F:\antExample\Example3\lib
20-04-2014 18:10 <DIR> .
20-04-2014 18:10 <DIR> ..
22-03-2013 18:08 177,599 servlet-api.jar
1 File(s) 177,599 bytes
Directory of F:\antExample\Example3\src
20-04-2014 18:10 <DIR> .
20-04-2014 18:10 <DIR> ..
20-04-2014 18:10 1,128 UserNameServlet.java
1 File(s) 1,128 bytes
Directory of F:\antExample\Example3\web
20-04-2014 18:10 <DIR> .
20-04-2014 18:10 <DIR> ..
17-04-2013 17:23 695 index.jsp
20-04-2014 18:10 <DIR> WEB-INF
17-04-2013 17:23 505 welcome.jsp
2 File(s) 1,200 bytes
Directory of F:\antExample\Example3\web\WEB-INF
20-04-2014 18:10 <DIR> .
20-04-2014 18:10 <DIR> ..
17-04-2013 16:16 662 web.xml
1 File(s) 662 bytes
Total Files Listed:
6 File(s) 181,593 bytes
14 Dir(s) 5,005,819,904 bytes free
F:\antExample\Example3>
build.xml
<?xml version="1.0"?>
<project name="firstAntWebProject" default="dist">
<!--
* compile the java sources
* create a jar file
* copy the web files into the build folder
* create a war file containing jar as well as web folder
-->
<target name="clean">
<delete dir="build/classes"/>
<delete dir="build/jar"/>
</target>
<target name="init" depends="clean">
<mkdir dir="build/classes" />
<mkdir dir="build/jar" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" classpath="lib/servlet-api.jar" />
</target>
<target name="jar" depends="compile">
<jar destfile="build/jar/myJarFiles.jar" basedir="build/classes"></jar>
</target>
<target name="targetwebFiles">
<copy todir="build/web">
<fileset dir="web"/>
</copy>
</target>
<target name="dist" depends="jar,targetwebFiles">
<zip destfile="dist/app-1.0.zip">
<fileset dir="build/web"/>
<fileset dir="build/jar"/>
</zip>
</target>
<target name="build-war" depends="dist">
<war destfile="myfirstAntWebProject.war" webxml="web/WEB-INF/web.xml">
<fileset dir="web">
<include name="**/*.*"/>
</fileset>
<!--
<lib dir="thirdpartyjars">
<exclude name="portlet.jar"/>
</lib>
-->
<classes dir="build/classes"/>
</war>
</target>
</project>
Now, run ant as below
F:\antExample\Example3>ant build-war
Buildfile: F:\antExample\Example3\build.xml
clean:
[delete] Deleting directory F:\antExample\Example3\build\classes
[delete] Deleting directory F:\antExample\Example3\build\jar
init:
[mkdir] Created dir: F:\antExample\Example3\build\classes
[mkdir] Created dir: F:\antExample\Example3\build\jar
compile:
[javac] F:\antExample\Example3\build.xml:22: warning: 'includeantruntime' wa
s not set, defaulting to build.sysclasspath=last; set to false for repeatable bu
ilds
[javac] Compiling 1 source file to F:\antExample\Example3\build\classes
jar:
[jar] Building jar: F:\antExample\Example3\build\jar\myJarFiles.jar
targetwebFiles:
[copy] Copying 1 file to F:\antExample\Example3\build\web
dist:
[zip] Building zip: F:\antExample\Example3\dist\app-1.0.zip
build-war:
[war] Building war: F:\antExample\Example3\myfirstAntWebProject.war
BUILD SUCCESSFUL
Total time: 2 seconds
F:\antExample\Example3>
Here i have tried to create both zip as well as war file. you can directly deploy war file or you can extract zip file and put in webapps folder of tomcat server as below.
Output
Recent Comments