ANT Builds and FTP

The more I play with ANT the more I kick myself for not looking into it earlier. This past weekend I was playing with some different ways to run builds and one of the things I ran into is having ANT ftp files to your different servers as you deploy your application from dev to qa, staging and production.

One of the first things you need to do is add two jar files to your ANT installation's lib folder from Jakarta Commons Net and Jakarta-ORO. They are commons-net.jar and jakarta-oro-2.0.8.jar.

After you have these two files, create a target like the one below named "ftp.deploy" in your build.xml file. See sample below:

<project name="alogix" default="ftp.about" basedir=".">
   
      <!-- set global properties for this build -->
         <!-- hd path to application -->
            <property name="application.dir" value="D:\inetpub\alogix\" />
         <!-- ftp server to move files to -->
            <property name="ftp.server" value="www.alogix.net" />
         <!-- directory in server to move files to (root relative) -->
            <property name="ftp.remotedir" value="/" />
      
      <!-- filesets -->
         <fileset id="fileset.exclude" dir=".">
            <exclude name="*build.xml" />
            <!-- use this if you need to exclude a folder (uncomment line and change folderName) -->
               <!-- <exclude name="**/[folderName]/**" /> -->
            <!-- use this if you need to exclude a file (uncomment line and change filename & extension -->
               <!-- <exclude name="*[filename].[extension]" /> -->
         </fileset>   
         
      <!-- targets -->
         <target name="ftp.about">
            <echo message="Using FTP in ANT example." />
            <echo message="By: Phill Nacelli" />
            <echo message="www.phillnacelli.net" />
         </target>
      
         <target name="ftp.help">
            <echo message="usage: ftp.deploy" />
         </target>   
      
         <target name="ftp.deploy">
            <input message="Please enter ftp server username:" addproperty="ftp.username" />
            <input message="Please enter fpt server password:" addproperty="ftp.password" />
            <ftp action="send" server="${ftp.server}" userid="${ftp.username}" password="${ftp.password}" remotedir="${ftp.remotedir}">
               <fileset refid="fileset.exclude" />
            </ftp>
         </target>
   </project>

Notice that nowhere in the build.xml file you see any username and password written. Instead of storing it on the file as a property we actually prompt the user for it when running the ftp.deploy target. This ensures that you can distribute your build file without compromising security.

If you use any versioning systems, notice that in the fileset.exclude we did not enter anything to stop the system files that they embed in your project (ie. ".svn" folder for Subversion, "vssver.scc" for VSS), that's because they are already excluded by default (see Default Excludes section on this page).

Related Blog Entries

Comments
If you were using ANT from within Eclipse, where would you put the jar files?
# Posted By Scott Stroz | 7/19/06 5:55 PM
By default, Eclipse's ANT home directory should be pointing to {Eclipse_Install_Directory}\Eclipse\plugins\org.apache.ant_1.6.5 (or whatever ANT version "org.apache.ant_x.x.x" you currently have). In there you'll have a directory named lib. Place the two jar files in that directory and restart Eclipse.
# Posted By phill.nacelli | 7/19/06 6:43 PM
I am using ANT in Eclipse, and have the two jars in the right place. However, when I run the FTP script, I get the following error:

java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClient

I am using Eclipse 3.2. Any ideas on how to solve this?

Hope you can help,

Erik-Jan
# Posted By Erik-Jan Jaquet | 2/11/07 10:42 AM
Hi Erik,

Try the following:

1. In Eclipse go to Windows > Preferences...
2. In the left tree navigation control, expand Ant and click on Runtime
3. In the right pane select the Classpath tab and Click on Ant Home Entries, this will show all the jar files used by ANT in Eclipse.
4. Check if the two jar files show up in there, if not click on Add External JARs...
5. Select the two jar files and see if they show up in the list.
6. Click on Apply button and just to be sure restart your Eclipse.

Give it a shot.. let me know how this works for you. I'll be happy to help you troubleshoot this further.

Cheers..
# Posted By phill.nacelli | 2/11/07 9:26 PM
Phill

Just to confirm that modifying the ANT Classpath under Preferences does work just fine.
# Posted By Don | 3/3/07 5:36 AM
Hi

Thanks for the ftp task explain.

From Spain, Javi.

Bye!
# Posted By Javi | 4/24/08 3:39 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.003. Powered by ColdFusion Server v8,0,1,195765.