Copying Files Using Phing

6th January 2009 - 4 minutes read time

One of the main reasons to use Phing is to create a copy of your project in another directory that you can then use as your distribution copy. Your working directory might contain lots of testing code that is not needed in the final build.

To copy a file from one directory to another using Phing you need to use the copy element. Here is a simple example where a single file is copied from one directory to another.

  1. <?xml version="1.0"?>
  2. <!-- build xml -->
  3. <project name="myProject" default="main">
  4. <target name="main">
  5. <copy file="index.php" tofile="myProject_build/index.php" />
  6. </target>
  7. </project>

This isn't entirely useful as you will need to create a rule for every file you want transferred, which might be a few. To copy a directory you need to set up a fileset element. This will contain a list of files and directories that you want to be copied. A fileset element can contain include and exclude rules that govern which files will be used. Here is an example of a fileset, which would normally be on the root of the project element.

  1. <fileset dir="./" id="myProjectFiles">
  2. <exclude name="**/*_test.php" />
  3. <include name="**/*.php" />
  4. </fileset>

In this example we are excluding any file in this and any sub directory that has a filename ending in _test.php as these will be our test files that we don't want copied. We then include any file that contains the .php extension. Because any exclude rules override any include rules this will work as expected and only copy across .php files without a _test at the end of the file name.

You can also specify a directory and everything in it by using the following include rule.

<include name="something/**" />

The previous fileset example can also be written as the following, although I don't recommend this as it can get complicated!

<fileset dir="./" id="myProjectFiles" includes="**/*.php something/**" excludes="**/*_test.php" />

To do something useful with this fileset we can use the copy element again. The main difference here is that we are using the todir attribute of the copy element to designate the destination of the copy.

  1. <?xml version="1.0"?>
  2. <!-- build xml -->
  3. <project name="myProject" default="main">
  5. <fileset dir="./" id="myProjectFiles">
  6. <exclude name="**/*_test.php" />
  7. <include name="**/*.php" />
  8. </fileset>
  10. <target name="main">
  11. <copy todir="./myProject_build">
  12. <fileset refid="myProjectFiles" />
  13. </copy>
  14. </target>
  15. </project>

If you run this it will copy all the files (without the exceptions) and directories from the current directory to a directory called myProject_build. If the destination directory doesn't exist then phing will create it. This works well, but if you run this code a second time it will copy the myProject_build into itself and you will get a recursive directory structure, which is quite messy and probably not what you want. There are two ways to prevent this. The first is to store your project in a subdirectory from your build.xml file and change your fileset rule accordingly. The second way is to add on an exclude rule to your fileset to specifically ignore the build directory and all of its contents.

<exclude name="myProject_build/**" />

Using the fileset and copy elements in this manner allows you to perform operations on the files as you copy them. This might be, for example, adding in a version number to the top of each file using a token replace.

Automated Build With Phing

Add new comment

The content of this field is kept private and will not be shown publicly.