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

Listen Print Discuss 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

  1. Cover of PHP Hacks
    PHP Hacks
    Print: $29.95
    Ebook: $23.99
  2. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95
  3. Cover of Build Your Own Database Driven Website Using PHP and MySQL
    Build Your Own Database Driven Website Using PHP and MySQL
    Print: $39.95
  4. Cover of Learning PHP, MySQL, and JavaScript
    Learning PHP, MySQL, and JavaScript
    Print: $39.99
    Ebook: $31.99

Tagged Articles

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

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com