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:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="myFirstBuildFile" default="" basedir=".">
        <description>
            A description of what this build file does
        </description>
        <!-- Simply echo's a message to the screen -->
        <echo message="Hello foo, I am the build.xml" />
    </project>

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.

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="myFirstBuildFile" default="" basedir=".">
        <description>
            A description of what this build file does
        </description>    
        <!-- Relative lo    cation of test folder -->
        <property name="test" value="folder_test/" />
        <!-- Relative location of live folder -->
        <property name="live" value="folder_live/" />
    
        <!-- move file -->
        <move file="${test}index.cfm" todir="${live}" />    
        <echo message="The index.cfm file has been moved" />
    </project>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:

    Run As >
> Ant Build
If 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':


<?xml version="1.0" encoding="UTF-8"?>
<project name="myFirstBuildFile" default="" basedir=".">
    <description>
        A description of what this build file does
    </description>    
    <!-- Relative lo    cation of test folder -->
    <property name="test" value="folder_test/" />
    <!-- Relative location of live folder -->
    <property name="live" value="folder_live/" />

    <!--
        move directories and files

        From Ant 1.6.3 you can write the following as:
        <move file="${test}" tofile="${live}" />
    -->

    <move todir="${live}">
        <fileset dir="${test}"/>
    </move>            

    <echo message="The contest of folder_test have been moved" />

    <!--
        You'll notice that when moving the directories Ant deletes the
        original folder. Because of this we are going to use <mkdir>
        to re-create the original folder
    -->
    

<mkdir dir="${test}" />
    <echo message="We have recreated the the folder_test directory" />

</project>    
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 all .cfm files and leave the scribble file in test -->
<move todir="${live}">
<fileset dir="${test}">
<include name="*.cfm"/>
<exclude name="scribble.cfm"/>
</fileset>
</move>

Move a list of files to a new directory


<move todir="${live}">
<filelist dir="${test}">
<file name="index.cfm"/>
<file name="fridays_joke.cfm"/>
</filelist>
</move>

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


<!--- Notice the new parameter here, includeemptydirs -->
<move todir="${live}" includeemptydirs="false">
<fileset dir="${test}">
<exclude name="**/*.bak"/>
</fileset>
<mapper type="glob" from="*" to="*.bak"/>
</move>

More resources:

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
jM's Gravatar Another great article! I've now written my first Ant task. With solid articles like this I might eventually "get" this eclipse/cfeclipse/ant stuff ;-)

Thanks!
# Posted By jM | 9/28/07 5:55 PM
BlogCFC was created by Raymond Camden / Contact Blog Owner / mptooling.com / spicemerchants-portsmouth.co.uk / ipicture.it