PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Improve Your Build Process with Ant

by Michael Kimsal
12/21/2005

Web applications today are much more complex beasts than they were even just a few years ago. The largest sites may constitute thousands of files with complex directory structures, and migrating those between development, staging, and production environments can be difficult to say the least. My own experience with web applications dates back to 1996. While I've seen a lot of changes over the years, keeping a large project in check never seems to get much easier, despite advances in CPU speed, RAM prices, broadband, and communication tools.

A few years ago I started using Ant to help bring some structure to my project's packaging and maintenance. As always, I kept everything in CVS, but I'd grown tired of making sure that every step I'd outlined happened in the correct order to create a working product. Between copying files, setting permissions, and editing configuration files, I found many times that one simple omission would cost hours of lost time. Using Ant to automate those tasks was the first step in regaining control of the build process.

Ant Is Java

Yes, Ant is a tool written in Java. Many PHP advocates like to defend PHP against other technologies in the web space by claiming, "Use the best tool for the job!" PHP on the Web is a shining example of this. However, I've yet to find any tools written in PHP that can do everything Ant can do. The closest I've found is a project called Phing, but even this tool lacks some advanced aspects of Ant. (Maybe they'll catch up!)

How Does It Work?

This article isn't a complete Ant tutorial--the Ant home page has much more information than I can provide here. Additionally, this article assumes you have Java and Ant installed already.

When invoked, the Ant program looks for an XML configuration file (build.xml by default), which contains the instructions about what tasks to perform. Each set of tasks is a target and has a name in the XML file.

Example Build File

Here's an example build.xml file:

<?xml version="1.0"?>
<project name="Sample Project" default="init" basedir=".">

        <description>Example project</description>

        <target name="init">
                <property name="sample" value="Hello world!"/>
        </target>
</project>

Assuming the file is in the current directory, type ant at a command line:

Buildfile: build.xml

init:

BUILD SUCCESSFUL
Total time: 0 seconds

Ant processed the build file, ran the init target (because it is the project's default target), and then exited successfully. Now change the file a little more and add a bit of output:

<?xml version="1.0"?>
<project name="Sample Project" default="init" basedir=".">

        <description>Example project</description>

        <target name="init">
                <property name="sample" value="Hello world!"/>
                <echo message="${sample}"/>
        </target>
</project>

The output this time is:

Buildfile: build.xml

init:
     [echo] Hello world!

BUILD SUCCESSFUL
Total time: 0 seconds

Pages: 1, 2, 3, 4

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: