PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 19 ■ AUTOMATED BUILD WITH PHING


Targets


Targets are similar, in some senses, to functions. A target is a set of actions grouped together to achieve
an objective: to copy a directory from one place another, for example, or to generate documentation.
In my previous example, I included a bare-minimum implementation for a target:



As you can see, a target must define at least a name attribute. I have made use of this in the project
element. Because the default element points to the main target, this target will be invoked whenever
Phing is run without command-line arguments. This was confirmed by the output:


megaquiz > main:


Targets can be organized to depend on one another. By setting up a dependency between one target
and another, you tell Phing that the first target should not run before the target it depends on has been
run. Now to add a dependency to my build file:


<?xml version="1.0"?>



<project name="megaquiz"
default="main"








As you can see, I have introduced a new attribute for the target element. depends tells Phing that the
referenced target should be executed before the current one, so I might want a target that copies certain
files to a directory to be invoked before one that runs a transformation on all files in that directory. I
added two new targets in the example: runsecond, on which main depends, and runfirst, on which
runsecond depends. Here's what happens when I run Phing with this build file:


$ phing
Buildfile: /home/bob/working/megaquiz/build.xml


megaquiz > runfirst:


megaquiz > runsecond:


megaquiz > main:


BUILD FINISHED


Total time: 0.3029 seconds


As you can see, the dependencies are honored. Phing encounters the main target, sees its
dependency, and moves back to runsecond. runsecond has its own dependency, and Phing invokes
runfirst. Having satisfied its dependency, Phing can invoke runsecond. Finally, main is invoked. The
depends attribute can reference more than one target at a time. A comma-separated list of dependencies
can be provided, and each will be honored in turn.
Now that I have more than one target to play with, I can override the project element’s default
attribute from the command line:

Free download pdf