PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Print Subscribe to PHP Subscribe to Newsletters

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
Ant: The Definitive Guide

Related Reading

Ant: The Definitive Guide
By Steve Holzner

Pages: 1, 2, 3, 4

Next Pagearrow




Recommended for You

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • More O'Reilly Sites
  • igniteshow.com
  • makerfaire.com
  • makezine.com
  • craftzine.com
  • labs.oreilly.com
  • Partner Sites
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com