English 中文(简体)
ANT - Deploying Applications
  • 时间:2024-12-22

Ant - Deploying Apppcations


Previous Page Next Page  

In the previous chapter, we have learnt how to package an apppcation and deploy it to a folder.

In this chapter, we are going to deploy the web apppcation directly to the apppcation server deploy folder and then, we are going to add a few Ant targets to start and stop the services.

Let us continue with the Hello World fax web apppcation. This is a continuation of the previous chapter; the new components are highpghted in bold.

build.properties

The file for build.properties is given below −


# Ant properties for building the springapp
appserver.home=c:\install\apache-tomcat-7.0.19

# for Tomcat 5 use $appserver.home}/server/pb
# for Tomcat 6 use $appserver.home}/pb
appserver.pb=${appserver.home}/pb

deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://www.tutorialspoint.com:8080/manager
tomcat.manager.username=tutorialspoint
tomcat.manager.password=secret

build.xml

The file for build.xml is as follows −


<?xml version="1.0"?>

<project name="fax" basedir="." default="usage">
   <property file="build.properties"/>
   <property name="src.dir" value="src"/>
   <property name="web.dir" value="war"/>
   <property name="javadoc.dir" value="doc"/>
   <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
   <property name="name" value="fax"/>
   
   <path id="master-classpath">
      <fileset dir="${web.dir}/WEB-INF/pb">
         <include name="*.jar"/>
      </fileset>
   <pathelement path="${build.dir}"/>
   </path>

   <target name="javadoc">
      <javadoc packagenames="faxapp.*" sourcepath="${src.dir}"
         destdir="doc" version="true" windowtitle="Fax Apppcation">
         <doctitle><![CDATA[<h1>= Fax Apppcation=</h1>]]></doctitle>
         <bottom><![CDATA[Copyright © 2011. All Rights Reserved.]]></bottom>
         <group title="util packages" packages="faxapp.util.*"/>
         <group title="web packages" packages="faxapp.web.*"/>
         <group title="data packages" packages="faxapp.entity.*:faxapp.dao.*"/>
      </javadoc>
   </target>

   <target name="usage">
      <echo message=""/>
      <echo message="${name} build file"/>
      <echo message="-----------------------------------"/>
      <echo message=""/>
      <echo message="Available targets are:"/>
      <echo message=""/>
      <echo message="deploy --> Deploy apppcation as directory"/>
      <echo message="deploywar --> Deploy apppcation as a WAR file"/>
      <echo message=""/>
   </target>


   <target name="build" description="Compile main source tree java files">
      <mkdir dir="${build.dir}"/>
      <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
         deprecation="false" optimize="false" failonerror="true">
         <src path="${src.dir}"/>
         <classpath refid="master-classpath"/>
      </javac>
   </target>

   <target name="deploy" depends="build" description="Deploy apppcation">
      <copy todir="${deploy.path}/${name}" preservelastmodified="true">
         <fileset dir="${web.dir}">
            <include name="**/*.*"/>
         </fileset>
      </copy>
   </target>

   <target name="deploywar" depends="build" description="Deploy apppcation as a WAR file">
      <war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
         <fileset dir="${web.dir}">
            <include name="**/*.*"/>
         </fileset>
      </war>
      <copy todir="${deploy.path}" preservelastmodified="true">
         <fileset dir=".">
            <include name="*.war"/>
         </fileset>
      </copy>
   </target>
      
   <target name="clean" description="Clean output directories">
      <delete>
         <fileset dir="${build.dir}">
            <include name="**/*.class"/>
         </fileset>
      </delete>
   </target>
   <!-- ============================================================ -->
   <!-- Tomcat tasks -->
   <!-- ============================================================ -->

   <path id="catapna-ant-classpath">
   <!-- We need the Catapna jars for Tomcat -->
   <!-- * for other app servers - check the docs -->
      <fileset dir="${appserver.pb}">
         <include name="catapna-ant.jar"/>
      </fileset>
   </path>

   <taskdef name="install" classname="org.apache.catapna.ant.InstallTask">
      <classpath refid="catapna-ant-classpath"/>
   </taskdef>
   <taskdef name="reload" classname="org.apache.catapna.ant.ReloadTask">
      <classpath refid="catapna-ant-classpath"/>
   </taskdef>
   <taskdef name="pst" classname="org.apache.catapna.ant.ListTask">
      <classpath refid="catapna-ant-classpath"/>
   </taskdef>
   <taskdef name="start" classname="org.apache.catapna.ant.StartTask">
      <classpath refid="catapna-ant-classpath"/>
   </taskdef>
   <taskdef name="stop" classname="org.apache.catapna.ant.StopTask">
      <classpath refid="catapna-ant-classpath"/>

   </taskdef>

   <target name="reload" description="Reload apppcation in Tomcat">
      
      <reload url="${tomcat.manager.url}"username="${tomcat.manager.username}"
      password="${tomcat.manager.password}" path="/${name}"/>
   </target>
</project>

In this example, we have used Tomcat as our apppcation server.

First, in the build properties file, we have defined some additional properties which are explained below −

    The appserver.home points to the installation path to the Tomcat apppcation server.

    The appserver.pb points to the pbrary files in the Tomcat installation folder.

    The deploy.path variable now points to the webapp folder in Tomcat.

Apppcations in Tomcat can be stopped and started using the Tomcat manager apppcation. The URL for the manager apppcation, username and password are also specified in the build.properties file.

Next, we declare a new CLASSPATH that contains the catapna-ant.jar. This jar file is required to execute Tomcat tasks through Apache Ant.

Tasks

The catapna-ant.jar provides the following tasks −

Sr.No Properties & Description
1

InstallTask

Installs a web apppcation. Class Name: org.apache.catapna.ant.InstallTask

2

ReloadTask

Reload a web apppcation. Class Name: org.apache.catapna.ant.ReloadTask

3

ListTask

Lists all web apppcations. Class Name: org.apache.catapna.ant.ListTask

4

StartTask1

Starts a web apppcation. Class Name: org.apache.catapna.ant.StartTask

5

StopTask

Stops a web apppcation. Class Name: org.apache.catapna.ant.StopTask

6

ReloadTask

Reloads a web apppcation without stopping. Class Name: org.apache.catapna.ant.ReloadTask

The reload task requires the additional parameters which are as follows −

    URL to the manager apppcation.

    Username to restart the web apppcation.

    Password to restart the web apppcation.

    Name of the web apppcation to be restarted.

Let us issue the deploy-war command to copy the webapp to the Tomcat webapps folder and then, let us reload the Fax Web apppcation. The following outcome is the result of running the Ant file −


C:>ant deploy-war
Buildfile: C:uild.xml

BUILD SUCCESSFUL
Total time: 6.3 seconds

C:>ant reload
Buildfile: C:uild.xml

BUILD SUCCESSFUL
Total time: 3.1 seconds

Once the above task is run, the web apppcation is deployed and the web apppcation is reloaded.

Advertisements