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.
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
2 <project name="myFirstBuildFile" default="" basedir=".">
3 <description>
4 A description of what this build file does
5 </description>
6 <!-- Simply echo's a message to the screen -->
7 <echo message="Hello foo, I am the build.xml" />
8 </project>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:
In the console window you should see something likeBuildfile: /[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
2 <project name="myFirstBuildFile" default="" basedir=".">
3 <description>
4 A description of what this build file does
5 </description>
6 <!-- Relative lo cation of test folder -->
7 <property name="test" value="folder_test/" />
8 <!-- Relative location of live folder -->
9 <property name="live" value="folder_live/" />
10
11 <!-- move file -->
12 <move file="${test}index.cfm" todir="${live}" />
13 <echo message="The index.cfm file has been moved" />
14 </project>
- build.xml
- [folder_live]
- [folder_test]
- [css]
- [images]
- index.cfm
The plan now is move the complete directory of 'folder_test' to 'folder_live':
2 <project name="myFirstBuildFile" default="" basedir=".">
3 <description>
4 A description of what this build file does
5 </description>
6 <!-- Relative lo cation of test folder -->
7 <property name="test" value="folder_test/" />
8 <!-- Relative location of live folder -->
9 <property name="live" value="folder_live/" />
10
11 <!--
12 move directories and files
13
14 From Ant 1.6.3 you can write the following as:
15 <move file="${test}" tofile="${live}" />
16 -->
17 <move todir="${live}">
18 <fileset dir="${test}"/>
19 </move>
20
21 <echo message="The contest of folder_test have been moved" />
22
23 <!--
24 You'll notice that when moving the directories Ant deletes the
25 original folder. Because of this we are going to use <mkdir>
26 to re-create the original folder
27 -->
28
29 <mkdir dir="${test}" />
30 <echo message="We have recreated the the folder_test directory" />
31
32 </project>
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
2 <move todir="${live}">
3 <fileset dir="${test}">
4 <include name="*.cfm"/>
5 <exclude name="scribble.cfm"/>
6 </fileset>
7 </move>
Move a list of files to a new directory
2 <filelist dir="${test}">
3 <file name="index.cfm"/>
4 <file name="fridays_joke.cfm"/>
5 </filelist>
6 </move>
Append ".bak" to the names of all files in a directory.
2 <move todir="${live}" includeemptydirs="false">
3 <fileset dir="${test}">
4 <exclude name="**/*.bak"/>
5 </fileset>
6 <mapper type="glob" from="*" to="*.bak"/>
7 </move>
More resources:
Thanks!