Ant: Copying files and directories

I know that I've done a post on copying files with Ant, but I really want to take this further and into more detail. To save my fingers a lot of the text below is taken from my first post as moving and copying directories/files is a similar command in principle.

I also want to mention that I am running all my Ant tasks via Eclipse and not the command line. If there is a demand to know how to install Ant and run it from the command line I'll do that post separately. At this point I'm assuming you've got Eclipse and you know you way around it enough? Before we do begin you will need to ensure that you can see the Console 'View', you can get to this by going to:

\n\n Window >> Show View >> Other >> General >> Console

Before we jump in, if you are on Unix then know that the file permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is down to the current Java runtimes and the inability to set the permissions. You can get around this by using ANT to call Copy Path view the terminal i.e. instead. If you are on Windows and you copy a file to a directory where that file already exists, but with different casing, the copied file takes on the case of the original. The workaround is to delete the file in the destination directory before you copy it.

Lets begin!

Create a new project in your Eclipse workspace. The type of project doesn't matter, though I'm creating a CFEclipse one as I want to cpoy .cfm pages. From within the project create a new file called Build.xml. Create two more folders in your Ant project called 'folder_test' and 'folder_live' and in the first folder put a file called 'index.cfm'. Your project should now look like this

  • build.xml
    • [folder_live]
    • [folder_test]
      • index.cfm
The idea of the next task to copy index.cfm from the test folder to the live one, i'm also going to introduce properties, think of them as variables. A description of what this build file does Now while still in the editor right-click anywhere on your build.xml file and choose the following: Run As >> Ant BuildIf all has gone well you should of just copied your index.cfm file from test to live. Thats all good and well but I doubt you ever have to copy just one file my guess is that you have a host of folders and files. In the directory 'folder_test' add two other directories called 'images' and 'css' and copy 'index.cfm' back as well. You new directory structure should look like this: (feel free to stick some more files into the newly created folders)
  • build.xml
    • [folder_live]
    • [folder_test]
      • [css]
      • [images]
      • index.cfm

The plan now is copy the complete directory of 'folder_test' to 'folder_live': A description of what this build file does Again while in the editor right-click anywhere on your build.xml file and choose the following: Run As >> Ant Build

Of course there is more you can do, rather than going through full set examples here or some of the other features:

Copy a set of files to a directory

Copy a set of files to a directory, appending .bak to the file name on the fly

Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.

Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.

Copies some resources to a given directory.

Copies the two newest resources into a destination directory.

More resources:




comments powered by Disqus