Show List
Ant Build To Create a War File and Deploy
Here is a sample Ant build to create War file, deploy to Tomcat server and then to start the Tomcat.
We are going to have a single JSP file and war will be created without xml file.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%><%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>Demo of creating a war file using Apache Ant build and deploy to Tomcat</body></html>
build.xml:
The JSP file is stored in the "src" folder.
In this example we are using "antcall" to call the next target in the sequence. This could also have been achieved using the "depends" attribute.
<?xml version="1.0" ?><project name="WarDemo" basedir="." default="createWar"><!-- Property names going to be used in the tasks below. --><property name="tomcat.dir" value="C:/tomcat"/><property name="tomcat.deploy.dir" value="${tomcat.dir}/webapps"/><property name="tomcat.bin.dir" value="${tomcat.dir}/bin"/><property name="war.filename" value="wardemoapp"></property><property name="src.dir" value="src"/><property name="base" value="."/><target name="createWar"><echo message="Creating war" /><war warfile="${war.filename}.war" needxmlfile="false"><fileset dir="${src.dir}" /></war><antcall target="deploytoTomcat" /></target><target name="deploytoTomcat"><echo message="Deploying the war to Tomcat" /><copy file="${war.filename}.war" todir="${tomcat.deploy.dir}" /><antcall target="startupTomcat" /></target><target name="startupTomcat"><echo message="Starting up Tomcat" /><exec executable="${tomcat.bin.dir}/startup.bat"/></target><target name="shutdownTomcat"><echo message="Starting up Tomcat" /><exec executable="${tomcat.bin.dir}/shutdown.bat"/></target></project>
In order to run the startup and shutdown bat file, we need to set the CATALINA_HOME environment variable:
- Set the CATALINA_HOME environment variable to the location of tomcat folder on the system. E.g. "C:\tomcat"
- Set CLASSPATH to lib folder. E.g. "C:\tomcat\lib"
- Add "%CATALINA_HOME%\bin" to the path.
When the "ant" command is executed, war gets created and deployed to the Tomcat. Then the Tomcat server gets started up:
PS C:\Users\mail2\Downloads\war> ant Buildfile: C:\Users\mail2\Downloads\war\build.xml createWar: [echo] Creating war [war] Building war: C:\Users\mail2\Downloads\war\wardemoapp.war deploytoTomcat: [echo] Deploying the war to Tomcat [copy] Copying 1 file to C:\tomcat\webapps startupTomcat: [echo] Starting up Tomcat [exec] Using CATALINA_BASE: "C:\tomcat" [exec] Using CATALINA_HOME: "C:\tomcat" [exec] Using CATALINA_TMPDIR: "C:\tomcat\temp" [exec] Using JRE_HOME: "C:\Program Files\OpenJDK\openjdk-8u275-b01" [exec] Using CLASSPATH: "C:\tomcat\bin\bootstrap.jar;C:\tomcat\bin\tomcat-juli.jar" [exec] Using CATALINA_OPTS: "" BUILD SUCCESSFUL Total time: 0 seconds
To stop the Tomcat, we can invoke the "shutdownTomcat" target with the "ant" command directly.
PS C:\Users\mail2\Downloads\war> ant shutdownTomcat Buildfile: C:\Users\mail2\Downloads\war\build.xml shutdownTomcat: [echo] Shuting down Tomcat [exec] Using CATALINA_BASE: "C:\tomcat" [exec] Using CATALINA_HOME: "C:\tomcat" [exec] Using CATALINA_TMPDIR: "C:\tomcat\temp" [exec] Using JRE_HOME: "C:\Program Files\OpenJDK\openjdk-8u275-b01" [exec] Using CLASSPATH: "C:\tomcat\bin\bootstrap.jar;C:\tomcat\bin\tomcat-juli.jar" [exec] Using CATALINA_OPTS: "" BUILD SUCCESSFUL Total time: 0 seconds
Source code:
https://github.com/it-code-lab/ant-build-to-create-a-war-file-and-deploy
Leave a Comment