Build process

Contents

  • 1 Introduction To Ant
  • 2 How to Automate the Build process using Ant
  • 3 Complete Build script for this process is below
  • 4 Executing the Ant Build script
  • 5 Enter reader Comments about this article here

Introduction To Ant

Ant is a Java based build tool from the Apache Jakarta Foundation that is used for building Java projects.Ant build files are written in XML.

Requirements : JAVA and ANT[Ant can be downloaded from [[1]]]

How to Automate the Build process using Ant

  • Lets us see a basic automation process to
 1. Checkout the java files from cvs
 2. Creating and organizing files and directories
 3. Compiling the java source code
 4. Creating the java archive(jar) file
 5. Running the jar file to print the output
  • Ant Build file structure is divided into following -Information about the projects being enclosed in <project> to  </project> tags which defines three attributes about the project  name,default target to be performed and the basedir -A project  can have a set of properties and it has a name and a value. Here we can set the global properties for the build, Example- <property name = “src.dir” value = “${basedir}/src”/>-Within each project, you will have a series of actions  -compile,make directories,change permissions,create a jar etc…  and each set of actions is known as a target enclosed in  <target> to </target> tags-Within a target, we specify a number of tasks to be performed. A task is a piece of code that can be executed and the task can have multiple attributes

1. Checkout the java files from cvs  – Syntax <cvs cvsRoot=”/pathto/CVSROOT” package=”projectname” dest=”${src.dir}”/>    This will checkout the project “projectname” from the CVSROOT location “/pathto/CVSROOT” and put in the destination directory ${src.dir}

2. Creating and organizing files and directories  – <mkdir dir = “${src.dir}”/> Creates the Directory “src”  – <delete dir = “${build.dir}”/> Deletes the Directory “build”

3. Compiling the java source code  – <javac srcdir=”${src.dir}” destdir=”${classes.dir}”>    Compiles all the java source files in the “src” directory and put the class files in the “classes” directory

4. Creating the java archive(jar) file  – <jar destfile=”${jar.dir}/project.jar” basedir=”${classes.dir}”>    All the class files are archived and a file “project.jar” is created inside the “jar” directory

5. Running the jar file to print the output  – <java jar=”${jar.dir}/project.jar” />    Runs the jar file “project.jar” and displays the output of the program

Complete Build script for this process is below

<?xml version = "1.0"?><project name = "AntSample" basedir = "." default="run">

<property name = "src.dir" value = "${basedir}/src"/>

<property name = "build.dir" value = "${basedir}/build"/>

<property name = "classes.dir" value = "${build.dir}/classes"/>

<property name = "jar.dir" value = "${build.dir}/jar"/>
<property name="main-class"  value="theclassfile"/>

<target name="checkout">
<echo message="Checking out from the CVS repository"/>
<cvs cvsRoot="/pathto/CVSROOT" package="projectname" dest="${src.dir}"/>
</target>

<target name = "clean" depends = "checkout">
<echo message="deleting the existing build directory"/>
<delete dir = "${build.dir}"/>
</target>

<target name = "mkdir" depends = "clean">
<echo message="creating directories"/>
<mkdir dir = "${build.dir}"/>
<mkdir dir = "${classes.dir}"/>
<mkdir dir = "${build.dir}"/>
<mkdir dir = "${jar.dir}"/>
</target>

<target name = "compile" depends="mkdir,clean" >
<echo message="Compile the java source files"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}">
</javac>
</target>

<target name="jar" depends="compile">
<echo message="Create the jar file"/>
<jar destfile="${jar.dir}/project.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar">
<java jar="${jar.dir}/project.jar" fork="true"/>
</target>
</project>

Executing the Ant Build script

  • Save the above script as build.xml and from the command line type “ant -f build.xml” to execute the Ant script
 The output will be the details about the build as SUCCESSFUL or FAILED and the time taken for the build will be displayed as
     BUILD SUCCESSFUL     Total time: 3 seconds

Leave a comment