Using Ant As A Build and Deployment Tool (Complete)

Last Updated Thursday January 24, 2002


I used Ant as the build tool to put together the tag library I previously built, see Tag Library Note because the development environment I was using, Oracle JDeveloper was a little clunky in being able to deploy the applications to other servers plus I wanted to get a better understanding of Ant.

I will not claim to be an Ant expert, but I will provide a brief overview of the build.xml file I created so one can us it as a reference for creating their own build file.

Now for the basics:

 

<?xml version="1.0" encoding="iso-8859-1"?>

<project name="TagLibApplication" default="all" basedir=".">

<!-- (1) utilize predefined environment variables -->
<property name="env" environment="env" value="env"/>
<property name="J2EE_HOME" value="${env.J2EE_HOME}"/>
<property name="JAVA_HOME" value="${env.JAVA_HOME}"/>

<!-- (2) Create the class path -->
<path id="common.class.path">
  <pathelement location="."/>
  <pathelement location="${J2EE_HOME}/lib/j2ee.jar"/>
</path>

<!-- create a reference for the class path utilized -->
<property name="common.class.path" refid="common.class.path" />
 

<target name="common">
  <echo message="BuildName: ${ant.project.name}" />
  <echo message="BuildHome: ${basedir}" />
  <echo message="SourceHome: ${basedir}\src" />
  <echo message="BuildFile: ${ant.file}" />
  <echo message="BuildJVM: ${ant.java.version}" />
</target>

<!-- set up basic variables that are needed to execute the build -->
<target name="init" depends="common">
  <property name="app.name" value="TagLibApplication" />
  <property name="ear.name" value="${app.name}" />
  <property name="web.name" value="${ear.name}-web" />
  <property name="bld.dir" value="./build" />
  <property name="etc.dir" value="./etc" />
  <property name="lib.dir" value="./lib" />
  <property name="src.ejb.dir" value="./ejb" />
  <property name="src.web.dir" value="./src" />
  <property name="src.html.dir" value="./public_html" />
  <property name="bld.ear.dir" value="${bld.dir}/${ear.name}" />
  <property name="bld.web.dir" value="${bld.ear.dir}/${web.name}" />
  <property name="bld.compiler" value="classic" />
  <property name="class.files" value="**/*.class" />
  <property name="bak.files" value="**/*~,**/*.bak" />
</target>

<!-- Clean up everthing -->
<target name="clean" depends="init">
  <delete dir="${bld.dir}" />
  <delete dir="${lib.dir}" />
  <delete dir="${src.web.dir}/doc" />
  <delete dir="${basedir}/classes" />
  <delete>
    <fileset dir="." includes="${bak.files}" defaultexcludes="no"/>
  </delete>
</target>

<!-- Make sure the basic directories are in place for the build -->
<target name="setup" depends="init">
  <mkdir dir="${lib.dir}" />
  <mkdir dir="${bld.dir}" />
  <mkdir dir="${etc.dir}" />
  <mkdir dir="${bld.ear.dir}" />
  <mkdir dir="${bld.ear.dir}/META-INF" />
  <mkdir dir="${bld.web.dir}/WEB-INF/classes" />
  <mkdir dir="${src.web.dir}/doc" />
  <mkdir dir="${basedir}/classes" />
</target>

<!-- Compile the java files for the tag library -->
<target name="web-classes" depends="setup">
  <javac srcdir="${src.web.dir}" destdir="${bld.web.dir}/WEB-INF/classes"
          debug="on">
    <classpath>
      <pathelement path="${common.class.path}" />
      <pathelement location="${bld.web.dir}" />
    </classpath>
  </javac>

  <!-- this makes sure that the latest code is in both classes dirs -->
  <copy todir="${basedir}\classes">
    <fileset dir="${bld.web.dir}/WEB-INF/classes" includes="**" />
  </copy>
</target>


<!-- Copies all of the .jsp and .htm* files for the web application -->
<target name="web-jsp" depends="setup">
  <copy todir="${bld.web.dir}">
    <fileset dir="${src.html.dir}" includes="index.html, *.jsp, *.htm*" />
  </copy>
</target>

<!-- Copies descriptor files for the deployment (.war) -->
<target name="web-descriptor" depends="setup">
  <copy todir="${bld.web.dir}/WEB-INF">
    <fileset dir="${etc.dir}" includes="web.xml,*.tld"/>
    <fileset dir="${src.html.dir}/WEB-INF" includes="web.xml,*.tld"/>
  </copy>
</target>

<!-- Creates the war file for the application -->
<target name="web-war" depends="web-classes,web-jsp,web-descriptor">
  <jar jarfile="${lib.dir}/${web.name}.war">
    <fileset dir="${bld.web.dir}" includes="**" />
  </jar>
</target>


<!-- Generates Standard Javadoc for the TagLibrary. Only create doc
when specifically requested or when the .war file is generated.
-->

<target name="doc" depends="web-classes">
  <javadoc sourcepath="${src.web.dir}" destdir="${src.web.dir}/doc"
  packagenames="mypackage"
  classpath="${common.class.path}"/>
</target>


<!-- Copies descriptor files for the enterprise deployment (.ear) -->
<target name="j2ee-descriptor" depends="setup">
  <copy todir="${bld.ear.dir}/META-INF">
    <fileset dir="${etc.dir}" includes="application.xml"/>
  </copy>
</target>

<!-- Creates the .ear file for deployment on the j2ee server -->
<target name="j2ee-ear" depends="web-war,j2ee-descriptor">
  <jar jarfile="${lib.dir}/${ear.name}.ear">
    <fileset dir="${lib.dir}" includes="${web.name}.war" />
    <fileset dir="${bld.ear.dir}" includes="META-INF/*.xml" />
  </jar>
  <echo message="FINISHED:use ${lib.dir}/${ear.name}.ear with deployment tool.." />
</target>


<!-- Creates the .jar file for EVERYTHING on the j2ee server -->
<target name="backup" depends="web-war,j2ee-descriptor">
  <jar jarfile="${ear.name}_backup.zip">
    <fileset dir="${lib.dir}" includes="${web.name}.war" />
    <fileset dir="${bld.ear.dir}" includes="META-INF/*.xml" />
    <fileset dir="${basedir}" excludes="${ear.name}_backup.zip"/>
  </jar>
</target>



<!--
  to test on the reference implementation without deploying, you
  need to copy the web application directory to the "public_html"
  directory in j2EE_HOME, and then copy the library files to the
  lib directory in J2EE_HOME
-->

<target name="test-deploy" depends="init,j2ee-ear">
  <copy todir="${J2EE_HOME}/public_html" overwrite="true">
    <fileset dir="${bld.ear.dir}" excludes="META-INF/" />
  </copy>
  <copy todir="${J2EE_HOME}/lib/classes">
    <fileset dir="${bld.web.dir}/WEB-INF/classes" includes="**" />
  </copy>
</target>


<!--

   this will automatically deploy your application to the J2EE RI

   the trick here is the use of the <srcfile/> tag. what it will do

   is upon evaluation, the value of the file set will be replaced

   to provide the desired results.

-->
<target name="deploy" depends="j2ee-ear">
    <apply executable="${J2EE_HOME}/bin/deploytool" >
        <arg value="-deploy"/>
        <srcfile/>
        <arg value="localhost"/>

        <fileset dir="${lib.dir}" includes ="${ear.name}.ear"/>
    </apply>
</target>

<target name="all" depends="j2ee-ear" />

</project>
 

Example usage:

  • Ant : this will build the whole project and create the .ear file for deployment
  • Ant test-deploy : will deploy the jsp and the tag library to a j2ee reference implementation for testing
  • Ant doc : generate the java documentation
  • Ant clean : deletes all backup files and class file and documentation

[ Cover Letter]  [ Resume ]  [ Home Page ] [ mailto:AaronKSaunders@Hotmail.com ]