CodeVisor's Blog

Visual Code Metrics & Analysis

Introduction to Ant and Build Systems

leave a comment »

What is a build system?

A build system is a piece of software that helps you build your project. Build doesn’t necessarily mean compile, there are many tasks associated with certain project, you may want to build a distributable package for example, package the current source code in a compressed archive, or you may want to build a PDF manual from documentation. The tasks you may need depends on the project and your requirements, but every task usually has some kind of a process that is repeatable every time you want to do that task.

Why use a build system?

A build system allows you to define steps needed to do what you want, which makes processing those repeatable tasks easy, less error-prune, time efficient, and more importantly unifies the process across different developers. A build system allows you to specify your dependencies clearly (i.e. To do C, you must do A and D first), and saves time by building files that was changed from the last build only.

Introduction to Ant

Ant is a cross-platform build system designed for Java software projects, although it can be used in other contexts, it’s widely used in Java projects. With Ant, you write an XML file, and then invoke ant with that file and other parameters to process the build.

For a software project, you will define targets. Targets are collections of tasks you want to do, common targets you will probably define are:

  • build to compile the source code using the javac compiler.
  • dist to build a distributable JAR file of the project, and maybe source code too if you want.
  • clean to delete compiled files and JAR files.

Those are just suggestions and to give you an idea of what targets are, but you can define the target to do whatever you want.

Steps within targets are executed by calling Ant Tasks. Removing a directory is a task for example that takes one parameter which is the directory to be removed. Ant provides a collection of predefined tasks, and you can write your own custom task in Java if those tasks don’t cover your needs, or isn’t available elsewhere.

Ant Example

This is an example build.xml file to show how Ant configurations look like: [1]

<?xml version="1.0" encoding="UTF-8"?>
<project name="ExampleProject">
<property name="src" location="src" />
<property name="build" location="build" />
<!--Defining a target named "build"-->
 <target name="build">
   <!--Create the build directory using the predefined "mkdir" Ant Task-->
   <mkdir dir="${build}"/>
   <!--Invoke the javac compiler to compile source code to the build directory-->
   <javac srcdir="${src}" destdir="${build}"/>
 </target>
 <!--A "clean" target to delete compiled files-->
 <target name="clean">
   <delete dir="${build}">
 </target>
</project>

To execute the build target in that example, you need to go to the command line, and switch to the directory where the XML file exists, then execute ant followed by the target name, in this case: ant build [2].

Ant assumes the file name is build.xml, if that’s not the case, you will have to pass the configuration file name as a parameter to ant (i.e. ant -f otherfile.xml).

Fortunately for those who don’t like the command line, as Ant is widely used, it’s integrated in many Integrated Development Environments like Eclipse, NetBeans and others.

Further Reading

Other Build Systems


Footnotes

[1] I’ve skipped introducing ant properties to keep it a short introduction, shouldn’t be hard to get though.
[2] This requires that you’ve already installed Ant of course.

Written by Hassan Ibraheem

25 February, 2010 at 11:52 am

Posted in Software

Tagged with ,

Leave a comment