Backing up your files with Ant and Eclipse

Tagged Under : ,

I have heard a lot about Ant for years now but being a CF developer, never really had the need to use it, or I must say, never bothered to use it. However, recently, I read some blog posts about this and I was very impressed by its power and the opportunities it opens. So I decided to give it a try. Because I use Eclipse for development, it made my life easier as it comes with the Eclipse installation. All I had to do was to create a new Eclipse/CFEclipse project and throw in a build.xml file and then rock n roll.

As my first attempt with Ant, I decided to work on a build file that would take all my source files from a folder, zip them up and save the zip file into a backups folder giving the zip file a timestamp based name. Sounds very easy to do with a CF script but this time around I was gonna try to do this with a Ant build file.

Here is what I did :-

  1. Create a new Eclipse Project(I called it AntProject)
  2. Create a folder(srcFiles) and put some files in it.
  3. Create another folder (backups).
  4. Create a blank build.xml in the root of the Project.
  5. Copy the following code to the build.xml file.

<?xml version=”1.0″ encoding=”UTF-8″?>
<project name=”makebackups” default=”" basedir=”.”>
<!– project description–>
<description>
This file creates backups when its run
</description>

<!– set properties for the folder names we are going to use,
srcDir is where files will be coped from and destDir is where backup will be saved–>
<property name=”srcDir” value=”srcFiles”/>
<property name=”destDir” value=”backups”/>

<!– create a timestamp for the naming the Zip file–>
<tstamp>
<format property=”NOW_UK” pattern=”yyyymmddhhmm” locale=”en,UK”/>
</tstamp>

<!– name of the zip file – based on current timestamp–>
<property name=”zipFilename” value=”bkp${NOW_UK}.zip” />

<!– zip up the files and save–>
<zip destfile=”${destDir}/${zipFilename}”
basedir=”${srcDir}”update=”true”/>

<!– the code wont run without the below command,
ha ha , just kidding, this is just a info message–>
<echo level=”info” message=”File ${zipFilename} saved” />

</project>

Thats it! You can now run this project by right clicking on build.xml and then “Run As – Ant Build”
That should open up your Console window when you run this but to open the console window manually, you can do this.
Window -> Show View -> Other -> General -> Console.

If everything goes fine, you should see a zip file in the backups Folder. Really Cool! I am impressed. Now only if I can figure out how to do that each time I close my Eclipse without having to run it manually.

Comments:

5 Responses to “Backing up your files with Ant and Eclipse”


  1. You have a small issue with this line:
    “”

    The needs a / in it at the end according to my console error that I got from eclipse. Thanks for writing this up!


  2. Sorry, your zip line needs to be:
    <zip destfile=”${destDir}/${zipFilename}” basedir=”${srcDir}” update=”true” />


  3. @Todd,

    Oh yeah, I missed the closing line while posting this. Thanks for pointing it out.


  4. OMG, that is so quick and it allows a mapped drive for the destination (unlike SVN).


  5. Thanks for the help… this ant its great.

Leave a Reply