Introduction To The Phing build.xml File

By default, Phing will look for a file in the current working directory called build.xml when you run it. This document tells Phing what it will be doing during the build. You can change this by using the -f or the -buildfile property of phing and giving the build file as the parameter. The following code makes phing look for a build file called wibble.xml.

phing -f wibble.xml

Assuming that the project is called myProject, then a minimal buildfile would be look like the following.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
  <target name="main">
</target>
</project>

Of course, this doesn't actually do anything at all, so it is utterly useless.

Every build file should consist of at a root project level, with one or more targets. Targets act as functions, they will copy your code to another directory, create a zip file, create documentation or do anything you want the build to do. You can run multiple targets, one after the other, by using the depends attribute. The following build file will run the first target before running the main target.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
  <target name="first">
  <target depends="first" name="main">
</target></target></project>

When run this will output the following:

C:\myProject>phing
Buildfile: C:\myProject\build.xml
 
myProject > first:
 
myProject > main:
 
BUILD FINISHED
 
Total time: 0.2197 seconds

You can call the targets whatever you want, but it is a good idea to call them by their functional name. You can also run single targets by running phing with an argument of the target in question. For example, if you want to run only the first target you would use the following command.

phing first

The target also supports the description attribute, which allows you to add a small description of that the target does. This is useful from a maintenance point of view, but also from a documentation point of view.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
  <target description="The first target." name="first">
  <target depends="first" description="The main and final target." name="main">
</target></target></project>

You can get phing to print out a list of the targets and their description by using the projecthelp flag like this.

phing -projecthelp

This will print out the following.

C:\myProject>phing -projecthelp
Buildfile: C:\myProject\build.xml
Default target:
-------------------------------------------------------------------------------
 main   The main and final target
 
Main targets:
-------------------------------------------------------------------------------
 first  The first target
 main   The main and final target

To actually do anything in a target you need to add in other xml tags. As a quick example you can echo text to the screen by using the echo tag. The following build file will print out a message to the screen when run.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
 <target name="main">
  <echo>Have I built anything yet?</echo>
 </target>
</project>

Automated Build With Phing

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
11 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.