CHAPTER 19 ■ AUTOMATED BUILD WITH PHING
target that copies the assembled (and transformed) files to their output locations. Breaking the
installation into separate build and install phases is not absolutely necessary, but it does mean that you
can check the results of the initial build before committing to overwriting production code. You can also
change a property and install again to a different location without the need to run a potentially
expensive copy/replace phase again.
At its simplest, the Copy task allows you to specify a source file and a destination directory or file:
As you can see, I specify the source file using the file attribute. You may be familiar already with the
todir attribute, which is used to specify the target directory. If the target directory does not exist, Phing
will create it for you.
If you need to specify a target file, rather than a containing directory, you can use the tofile
attribute instead of todir.
Once again, the build/conf directory is created if necessary, but this time, Config.php is renamed to
myConfig.php.
As you have seen, to copy more than one file at a time, you need to add a fileset element to copy:
The source files are defined by the srclib fileset element, so all you have to set in copy is the todir
attribute.
Phing is smart enough to test whether or not your source file has been changed since the target file
was created. If no change has been made, then Phing will not copy. This means that you can build many
times and only the files that have changed in the meantime will be installed. This is fine, as long as other
things are not likely to change. If a file is transformed according to the configuration of a replacetokens
element, for example, you may want to ensure that the file is transformed every time that the Copy task
is invoked. You can do this by setting an overwrite attribute:
Now whenever copy is run, the files matched by the fileset element are replaced whether or not
the source has been recently updated.
You can see the copy element summarized in Table 19–5.