How to integrate Ant with Tomcat

How to integrate Ant with Tomcat

Objective:  Use Ant script to build and deploy application into an Apache Tomcat container.

Solution:  Apache provides ant “taskdef” for most of tomcat management features such as start, stop, deploy, undeply, and redeploy.

There are few pre-requisites or dependencies before those ant task can be used on a tomcat server. Following are pre-requisites:

 

  1. Must have catalina-ant.jar file in the library path for the tomcat taskdefs. This is part of Tomcat deployment. This jar must match the version of the tomcat that you are targeting to deploy.
  2. The tomcat URL must be either http://localhost:8080/manager or http://localhost:8080/manager/text. The new version uses /text while the older version does not require it.
  3. Add roles and a user in tomcat tomcat-users.xml file. Add following roles to the user
  • manager-gui — Access to the HTML interface.
  • manager-status — Access to the “Server Status” page only.
  • manager-script — Access to the tools-friendly plain text interface that is described in this document, and to the “Server Status” page.

Make sure that you can login in the tomcat admin page with the user.

  1. Make sure that your paths are correct for war file for library.
  2. This will work for local and remote deployment. For remote, you need to change the URL.

<!--   tomcat stuff    -->

<!-- Configure properties to access the Manager applicati -->

<property name="url" value="http://localhost:8080/manager" />

<property name="username" value="admin" />

<property name="password" value="admin" />

<property name="TOMCAT_HOME" value="/tomcat" />

 

<!-- Configure the custom Ant tasks for the Manager application -->

<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="compile.classpath" />

<taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="compile.classpath" />

<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="compile.classpath" />

<!-- taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="compile.classpath" /-->

<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="compile.classpath" />

<taskdef name="start" classname="org.apache.catalina.ant.StartTask" classpathref="compile.classpath" />

<taskdef name="stop" classname="org.apache.catalina.ant.StopTask" classpathref="compile.classpath" />

<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="compile.classpath" />

<taskdef name="remove" classname="org.apache.catalina.ant.RemoveTask" classpathref="compile.classpath" />

 

<target name="deploy" description="Install web application" depends="makeWar">

<deploy url="${url}" username="${username}" password="${password}" path="/${war.name}" war="file:${dist.dir}/${war.name}.war" />

</target>

 

<target name="reload" description="Reload web application" depends="compile">

<reload url="${url}" username="${username}" password="${password}" path="/${war.name}" />

</target>

 

<target name="undeploy" description="Remove web application">

<undeploy url="${url}" username="${username}" password="${password}" path="/${war.name}" />

</target>

 

<target name="start">

<sshexec host="${host}" username="${username}" password="${password}" command=". /etc/profile; ${tomcatHome}/bin/catalina.sh start" trust="true" />

</target>

 

<target name="stop">

<sshexec host="${host}" username="${username}" password="${password}" command=". /etc/profile; ${tomcatHome}/bin/catalina.sh stop" trust="true" />

</target>

 

<target name="remove" description="Remove web application">

<remove url="${url}" username="${username}" password="${password}" path="${{war.name}" />

</target>

 

<target name="tomcat-start">

<java jar="${TOMCAT_HOME}/bin/bootstrap.jar" fork="true">

<jvmarg value="-Dcatalina.home=${TOMCAT_HOME}" />

</java>

</target>

 

<target name="tomcat-stop">

<java jar="${TOMCAT_HOME}/bin/bootstrap.jar" fork="true">

<jvmarg value="-Dcatalina.home=${TOMCAT_HOME}" />

<arg line="stop" />

</java>

</target>