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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
Window >> Show View >> Other >> General >> Console
1Window >> 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
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
right-click on the project folder >> new >> file >> file name (build.xml)
1right-click on the project folder >> new >> file >> file name (build.xml)
In your build.xml add the following and save:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<?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>
1<?xml version="1.0" encoding="UTF-8"?>
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
Run As >> Ant Build
1Run 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]
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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<?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>
1<?xml version="1.0" encoding="UTF-8"?>
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>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
Run As >> Ant Build
1Run 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]
The plan now is move the complete directory of 'folder_test' to 'folder_live':
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<?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>
1<?xml version="1.0" encoding="UTF-8"?>
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>
Again while in the editor right-click anywhere on your build.xml file and choose the following:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
Run As >> Ant Build
1Run 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
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<!-- 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>
1<!-- move all .cfm files and leave the scribble file in test -->
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
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<move todir="${live}">
<filelist dir="${test}">
<file name="index.cfm"/>
<file name="fridays_joke.cfm"/>
</filelist>
</move>
1<move todir="${live}">
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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<!--- 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>
1<!--- Notice the new parameter here, includeemptydirs -->
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!