Ant: Moving files and directories

First off I wanna say that I'm running all my Ant task 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.

So, what is Ant? Well to steal a quote from Mark Drew, think of Ant as .bat files on steroids. You use XML to describe a set of commands to run a whole range of tasks to do anything from SVN/CVS checkouts, unit tests, FTP, emails, sql, moving/copying folders/files generally just name it!

In this guide I want to cover creating a build.xml file and moving files/folders. 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.

Window >> Show View >> Other >> General >> Console

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 move .cfm pages around. From within the project create a new file called Build.xml right-click on the project folder >> new >> file >> file name (build.xml) In your build.xml add the following and save: 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 Build In the console window you should see something like

Buildfile: /[your eclipse work space root]/Ant/build.xml
[echo] Hello foo, I am the build.xml
BUILD SUCCESSFUL
Total time: 136 milliseconds

You've ran your first build.xml file excellent. Now lets move a file. 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 moved your index.cfm file from test to live. Thats all good and well but I doubt you ever have to move 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 move 'index.cfm' back as well. You new directory structure should look like this:
  • build.xml
    • [folder_live]
    • [folder_test]
      • [css]
      • [images]
      • index.cfm

The plan now is move 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:

Move a set of files to a new directory

Move a list of files to a new directory

Append ".bak" to the names of all files in a directory.

More resources: