An Ant build script for web applications

Ant web build script FAQ: Can you share an example Ant build script for a web application?

Sure, here's a sample Ant build script for a Java web application. I've included other example Ant scripts for Java Swing/JFC/GUI applications on this website, but this one demonstrates how to use Ant to build a web app instead.

My Ant build script for web applications

<project name="MY_APP" default="cleandist" basedir=".">

  <property environment="currentenv" />
  <property file="build.${currentenv.ANT_HOST_NAME}" />
  <property name="build"   value="build" />
  <property name="rootdir" value="../" />


  <!-- properties for 'jsp-only' deployment on tool -->
  <property name="cvspages"        value="../pages" />
  <property name="jspTargetDir"    value="${appDeployDir}" />
  <property name="imagesTargetDir" value="${jspTargetDir}/images" />


  <path id="class.path">
    <pathelement path="${currentenv.classpath}"/>
    <fileset dir="../lib">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset>
    <fileset dir="${tomcat_home}/lib">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset>
  </path>


  <!-- CLEAN -->
  <target name="clean">
    <echo message="Removing old directory structure..." />
    <delete dir="${appDeployDir}" >
    </delete>
  </target>


  <!-- MK DEPLOY DIR -->
  <target name="mkDeployDir">
    <mkdir dir="${appDeployDir}/WEB-INF/classes" />
    <mkdir dir="${dg_props_dir}" />
  </target>


  <!-- DEPLOY -->
  <target name="deploy">
      <copy todir="${appDeployDir}">
          <fileset dir="${rootdir}/pages"/>
      </copy>
      <replace file="${appDeployDir}/global/properties.jsp" token="@web_site_context@" value="${web_site_context}" /> 
      <copy todir="${appDeployDir}/WEB-INF/lib">
          <fileset dir="${rootdir}/lib" />
      </copy>

      <copy todir="${appDeployDir}/WEB-INF/classes">
          <fileset dir="${rootdir}/classes" />
      </copy>

      <!-- STRUTS STUFF -->
      <copy todir="${appDeployDir}/WEB-INF">
          <fileset dir="${rootdir}/resources">
            <include name="*.tld"/>
          </fileset>
      </copy>
      <copy file="${rootdir}/resources/struts-config.xml" toDir="${appDeployDir}/WEB-INF/" />


      <!-- WEB.XML -->
      <copy file="${rootdir}/resources/web.xml" toDir="${appDeployDir}/WEB-INF/" />
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@dg_props_dir@" value="${dg_props_dir}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@deployDir@"    value="${appDeployDir}" /> 

      <echo message="CHANGING SETTINGS FOR DD CONN BROKER" />
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@databasehost@"  value="${databasehost}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_name@"       value="${db_name}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_username@"   value="${db_username}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_password@"   value="${db_password}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_log_file@"   value="${db_log_file}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_driver@"     value="${db_driver}" /> 
      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@db_url@"        value="${db_url}" /> 

      <replace file="${appDeployDir}/WEB-INF/web.xml" token="@file_upload_dir@"   value="${file_upload_dir}" /> 


      <replace file="${appDeployDir}/fileUpload.jsp" token="@web_site_context@"   value="${web_site_context}" /> 

      <!-- <copy file="${rootdir}/resources/log.conf" toDir="${appDeployDir}/WEB-INF/" /> -->

      <!-- DATA/CONFIG FILE LOC -->
      <copy file="${rootdir}/resources/dg.props" toDir="${dg_props_dir}" />

  </target>

  
  <target name="compile">
    <echo message="Compiling MY_APP ..." />
    <javac classpathref="class.path" srcdir="${rootdir}/classes" destdir="${appDeployDir}/WEB-INF/classes" />
  </target>


  <!-- JSP DEPLOY -->
  <target name="jspDeploy">
    <echo message="Deleting JSP's and HTML files in web folder ..." />
    <delete>
      <fileset dir="${jspTargetDir}" includes="*.jsp"/>
      <fileset dir="${jspTargetDir}" includes="*.html"/>
    </delete>
    <echo message="Deleting images in web folder ..." />
    <delete>
      <fileset dir="${imagesTargetDir}" includes="*.gif"/>
      <fileset dir="${imagesTargetDir}" includes="*.jpg"/>
    </delete>

    <echo message="Creating necessary folders (if needed) ..." />
    <mkdir dir="${jspTargetDir}"/>
    <mkdir dir="${imagesTargetDir}"/>

    <echo message="Copying JSP's and HTML files to web folder ..." />
    <copy todir="${jspTargetDir}">
      <fileset dir="${cvspages}">
        <include name="*.*"/>
      </fileset>
    </copy>

    <echo message="Copying images to web folder ..." />
    <copy todir="${imagesTargetDir}">
      <fileset dir="${cvspages}/images">
        <include name="*.*"/>
      </fileset>
    </copy>

    <echo message="Changing file permissions on the site ..." />
    <chmod file="${jspTargetDir}\*" perm="ug+rw" />
    <chmod file="${imagesTargetDir}\*" perm="ug+rw" />


  </target>


  <!-- CLEANDIST -->
  <target name="cleandist" depends="clean, mkDeployDir, compile, deploy">
  </target>

</project>

Ant web build script - Discussion

In this sample Ant web build script I also do a fair amount of variable substitution during the build process. I do this by referencing a property file in this line of the Ant script:

  <property file="build.${currentenv.ANT_HOST_NAME}" />

This lets me change Ant build properties depending on which server (or workstation) I am running the build process on. The ANT_HOST_NAME is a variable that I set in the environment of each machine. Here are the contents of one of these property file that I reference on my laptop:

tomcat_home  = c:\\tomcat-3.3.2
appDeployDir = c:\\tomcat-3.3.2\\webapps\\appname
db_username = username
db_password  = password
dg_props_dir = c:/tomcat-3.3.2/webapps/appname/data
db_log_file  = c:/tomcat-3.3.2/logs/ddConnectionBroker.log
web_site_context = /appname
file_upload_dir = c:/tomcat-3.3.2/logs
db_driver = org.postgresql.Driver
db_url = jdbc:postgresql://192.168.1.10/appname

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.