<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Basil Vandegriend: Professional Software Development &#187; FTP</title>
	<atom:link href="http://www.basilv.com/psd/blog/tag/ftp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.basilv.com/psd</link>
	<description></description>
	<lastBuildDate>Wed, 25 Jan 2012 13:23:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Automating FTP with ANT</title>
		<link>http://www.basilv.com/psd/blog/2006/automating-ftp-with-ant</link>
		<comments>http://www.basilv.com/psd/blog/2006/automating-ftp-with-ant#comments</comments>
		<pubDate>Thu, 14 Dec 2006 15:00:48 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[ANT]]></category>
		<category><![CDATA[automated build]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[FTP]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2006/automating-ftp-with-ant</guid>
		<description><![CDATA[This article describes how to automate the transfer of files between servers via FTP using the Java-based ANT build tool. I have been using ANT to do automated FTP for a number of years now, and will share some of the lessons and limitations I have discovered. ANT provides a FTP task. It requires the [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes how to automate the transfer of files between servers via FTP using the Java-based <a href="http://ant.apache.org/">ANT build tool</a>. I have been using ANT to do automated FTP for a number of years now, and will share some of the lessons and limitations I have discovered. </p>
<p>ANT provides a FTP task. It requires the installation of two external libraries into ANT's <code>\lib</code> directory: <a href="http://jakarta.apache.org/oro/">Jakarta ORO</a> and <a href="http://jakarta.apache.org/commons/net/">Jakarta Commons Net</a>. The FTP task connects to a remote server via the FTP protocol and provides all the basic FTP operations: send files, get files, delete files, create directories, delete directories, and change permissions. For the full details on how to use the FTP task, see the ANT manual's <a href="http://ant.apache.org/manual/OptionalTasks/ftp.html">entry on the FTP task</a>. </p>
<p>For my website, I use the FTP task to transfer my server logs to my local workstation for analysis. Here is an example (using ANT version 1.6.5):</p>
<pre class="prettyprint">
&lt;ftp action="recv"
  server="ftp.server"
  remotedir="/logs"
  userid="my-userid"
  password="my-password"
  verbose="yes"
&gt;
  &lt;fileset dir="${log.dir}"&gt;
      &lt;include name="*.log"/&gt;
  &lt;/fileset&gt;
&lt;/ftp&gt;
</pre>
<p>This task transfers the files named <code>*.log</code> from the directory <code>/logs</code> of the server <code>ftp.server</code> to the <code>${log.dir}</code> directory on your local machine.</p>
<p>At work, I usually use the FTP task for <a href="http://www.basilv.com/psd/blog/2006/deploying-application-changes">deploying application changes</a> - sending a set of files built on my local workstation or development server to the test or production server. Here is an example:</p>
<pre class="prettyprint">
&lt;ftp action="send"
  server="ftp.server"
  userid="my-userid"
  password="my-password"
  remotedir="/incoming"
  depends="yes"
  binary="no"
  chmod="755"
&gt;
  &lt;fileset dir="${ftp.source.dir}"&gt;
      &lt;include name="**/*"/&gt;
  &lt;/fileset&gt;
&lt;/ftp&gt;
</pre>
<p>This task transfers all the files within the local directory <code>${ftp.source.dir}</code> to the directory <code>/incoming</code> of the server <code>ftp.server</code>. Files in sub-directories are also transferred into corresponding sub-directories on the remote server, and the sub-directories are created if they do not already exist. Only new or changed files are actually transferred (<code>depends="yes"</code>). The files are transferred in ASCII mode (<code>binary="no"</code>). Assuming the remote server runs UNIX, the permissions of the files are set to 755 (<code>chmod="755"</code>). </p>
<p>Using the FTP task to deploy a set of files has a number of limitations, especially if these files are organized into a hierarchy of directories. While the FTP task will automatically create sub-directories as required, I have not found a way to ensure that the required directory permissions are assigned for new directories (this assumes the remote server runs UNIX). In particular, the <code>chmod</code> attribute is only assigned to new files, not new directories. The only workaround I have found is to execute a script on the server that assigns the necessary directory permissions. Another limitation is that the FTP task will not create empty directories. If you want to create a particular directory structure on the remote server, then each directory you want created must have at least a single file in it. The workaround is to create a dummy file to ensure the directory is created.</p>
<p>Deleting obsolete files and directories on the remote server is also difficult for a couple of reasons. The FTP task does provide all the basic FTP operations, including deleting files and directories. But each is handled as a separate action which requires a separate invocation of the FTP task, instead of performing all the operations within a single FTP session. This is a minor inconvenience. The more significant limitation is in determining how to delete obsolete files and directories. The development build usually assembles a set of files to transfer to the server without caring about the individual files or directories - it just assembles a directory structure. Therefore, if certain files or directories are deleted or renamed, this just results in a new set of files without the build process knowing which are to be deleted. Therefore, automating the deletion of these obsolete files by listing each one is quite difficult. An easier approach would be to simply delete all the files and directories deployed to the server and then re-deploy the new set. This solution also has its problems. First, this requires recreating all the sub-directories, which as I explained above leads to the problem of ensuring their permissions are properly configured. Second, the directory structure into which files are deployed may include files created by server processes which cannot or should not be deleted, such as server logs or application data files. The workaround I use to deal with obsolete files and directories is to simple leave them in place - they typically have no impact on the operation of the system. But I am on the lookout for a better solution to this problem - if you know of any, please let me know via a comment. </p>
<p>Despite the limitations, I have been pleased with my experiences using ANT to automate FTP tasks. I encourage you to give it a try.</p>
<p><strong>Update June, 2009</strong>: I have had questions about what version of the ORO and Commons Net libraries to use, as not all versions are compatible. I have had success with Ant 1.7, Commons Net 1.4.0, and Jakarta ORO 2.0.8.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2006/automating-ftp-with-ant/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

