Apache Ant - SPLessons

ANT Building Project

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ANT Building Project

ANT Building Project

shape Introduction

So far, the basics of ANT are learnt and now, its time to build a project. But before that, the sketch has to be drawn and analyzed. The following tasks are covered in ANT Building Project chapter:

  • Defining Properties
  • Init Task
  • Starting and Stopping Logging
  • Task Preparation
  • Fetching Source code
  • Compiling

shape Description

ANT Building Project process develops by combining various process. Initially, init task has to be defined which loads the properties to the build. Then, the process has to be recorded and the report has to be generated. Next, the build directory has to be build for the created files and report directories.

Source code has to be fetched, compiled and placed in the build directory. Then, the junits test has to be done. If the test is passed, build the war file and deploy it into the FTP Server. If wanted to email the reports, just zip and email.

shape Conceptual
figure

Defining Properties

shape Description

To define properties for the ANT Build file, a text file called build.properties is required. This file will be loaded into the build using property task. It has all the names like project’s name, various directories names, paths of various jar files, server details, compiler names, and email server details.

name=splessons
src.dir=src
build.dir=build
build.dir.classes=${build.dir}/classes
etc.dir=etc
reports.dir=reports
war.name=${name}-${DSTAMP}.war
servlet.jar=C:/java-servlet/servlet-spi.jar
junit.jar=C:/junit3.8.1/junit.jar
build.compiler=modern
cvs.root=:pserver:anonymous:anonymous@PENTIUM4:/AppsData/CVSRepo
cvs.package=com/splessons
server.name=myserver.com
user.id=splessons
password=mysite
remote.dir=oncueproductions
mail.host=smtp.mail.yahoo.com
mail.user=splessons@gmail.com
mail.pass=readfirst
mail.port=485
mail.from.address=itoolsinfo@gmail.com
mail.to.address=itoolsinfo@yahoo.com

Init Task

shape Description

Init task defines tstamp tag, which prints the current date. Then the build.properties will be loaded into the build and classpaths has to be defined. As this is the servlets project, one should add servlet.jar to the classpath.

shape Examples

<?xml version="1.0" encoding="UTF-8"?>
<project name="End to End Build" default="sendmail" basedir=".">
    
    <target name="init">
        <tstamp />
        <property file="build.properties" />
        
        <path id="classpath">
            <pathelement path="${servlet.jar}" />
            <pathelement path="${junit.jar}" />
        </path>
        <property name="classpath" refid="classpath" />
    </target>

Starting and Stop Logging

shape Description

One has to record task to create the log file. However, dstamp variable will be utilized by this log file name and this process goes on every time the build file is run. File name will possess the variable pointing the project name, date and -log text. Parameter action instructs the record task to initialize the recording. If wanted to stop recording, action will be made to stop. Here, append has to be set to false if one wants to run the build twice a day.

shape Examples

Code to start the recorder

    <target name="startrecorder">
        <record name="${name}-${DSTAMP}-log.txt" action="start" append="false" />
    </target>

Code to stop the recorder

    <target name="stoprecorder">
        <record name="${name}-${DSTAMP}-log.txt" action="stop" />
    </target>

Preparing Task

shape Description

Preparing task will make the build directory and classes in the directories use mkdir. A report directory is created to store the reports. Also, clean task will help in cleaning up the build directory and reports directory. However, this action is not done automatically and has to be called whenever required.

shape Examples

Code of prepare task

<target name="prepare" depends="init, startrecorder">
        <mkdir dir="${build.dir.classes}" />
        <mkdir dir="${reports.dir}" />
    </target>

Code for clean task which is given at the last.

    
    <target name="clean" depends="init">
        <delete dir="${build.dir}" />
        <delete dir="${reports.dir}" />
    </target>

Fetching

shape Description

Source code has to be fetched from the CVS repository. Here, cvs root parameter is used for CVS Task. Next, updation has to be done by using the update command.

Then, the fail attribute has to be set to “true” and can be used if CVS throws any error and terminates the build process.

shape Examples

    
    <target name="fetch" depends="init">
        <!-- <cvs dest="${src.dir}" cvsRoot="${cvs.root}" command="update -P -d" failonerror="true" />
        /> -->
    </target>

Compiling ANT Building Project

shape Description

Java C task is needed in compiling process. But before compiling, check whether build directories are created or not, which inturn depends on fetch, prepare and init tasks.

Then, compiler parameter has to be defined, which gives the type of parameter.includeantruntime tells the compiler to include the run time libraries of ant in the class path.


shape Example

    <target name="compile" depends="prepare, fetch">
        <javac srcdir="${src.dir}" destdir="${build.dir.classes}" classpath="${classpath}" compiler="${build.compiler}" debug="on" includeantruntime="false" />
    </target>

Further process of the project i.e. testing with JUnit, creating jar and war files, packing and deploying, and integrating with the eclipse will be shown in the next chapters.

Summary

shape Points

  • build.properties should be loaded to the build file.
  • Init task initiates the project using tstamp tag.
  • Tasks are recorded by record task.
  • Clean task deletes the build directories and reports.
  • Cvs root fetches the code in CVS repository.