Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Using FTP with Nant

5.00/5 (1 vote)
19 Sep 2014CPOL2 min read 10.8K  
Using FTP with Nant

Introduction

If you have used Nant previously to build and deploy your applications, then you may have needed to copy the build results to a server for testing and / or production. If the server is a remote server then the obvious solution is to use FTP to copy the files to your remote server. This can be easily achieved using Nant and the FTP client WinSCP. WinSCP has a rich client interface and can also be automated from a script by invoking it via the command-line, making it the perfect FTP client to use with your Nant build scripts.

Background

It is assumed that the reader is familiar with Nant syntax and has downloaded and installed the FTP client WinSCP. If you add the path to WinSCP to your Windows PATH environment variable then you do not have to specify the absolute path to WinSCP when referring to it in your Nant scripts.

Copying a single file to a remote server

To copy a single file or a set of files use the syntax below. For multiple files substitute the PUT command with MPUT.

XML
<target name="FTPFileToWebServer"}">
    <echo message="Task execution started at : ${script::format-to-string(datetime::now())}" />

    <property name="ftp.webserver.dir"               value="ftp://username:password@myremoteserver" />
    <property name="built.release.version.dir"       value="\\releases\v1.0\myApplication\" />
    <property name="built.release.version.file"      value="myApplication.dll" />
    <property name="verbose"                         value="true" />

    <exec program="WinSCP.com" failonerror="true" verbose="${verbose}">
      <arg value="/command" />
      <arg value="option batch abort" />
      <arg value="option confirm off" />
      <arg value="option transfer binary" />
      <arg value="open ${ftp.webserver.dir}" />
      <arg value= '"put ""${built.release.version.dir}\${built.release.version.file}"""' />
      <arg value="close" />
      <arg value="exit" />
    </exec>
    
    <echo message="Task execution finished at : ${script::format-to-string(datetime::now())}" />
  </target>

The command option batch abort sets the batch mode to abort so that if any errors are encountered during an FTP transfer then it is immediately aborted.

The command option confirm off prevents the interactive dialog for confirming overwrites from appearing, therefore all files are overwritten by default during the FTP transfer.

Copying an entire folder structure to a remote server

This is where WinSCP really comes into its own. To copy an entire folder structure using FTP commands is possible but far from trivial. You would need to create the folder structure on the remote server using MKDIR commands and then recursively traverse your local folder structure and recreate this on your remote server. This is far from simple. However, with WinSCP, you can achieve this with a single command as in the example below.

XML
<target name="FTPFileToWebServer"}">
    <echo message="Task execution started at : ${script::format-to-string(datetime::now())}" />

    <property name="ftp.webserver.dir"               value="ftp://username:password@myremoteserver" />
    <property name="built.release.version.dir"       value="\\releases\v1.0\myApplication\" />
    <property name="verbose"                         value="true" />
    
    <exec program="WinSCP.com" failonerror="true" verbose="${verbose}">
      <arg value="/command" />
      <arg value="option batch abort" />
      <arg value="option confirm off" />
      <arg value="option transfer binary" />
      <arg value="open ${ftp.webserver.dir}" />
      <arg value= '"synchronize remote ${built.release.version.dir}"' />
      <arg value="close" />
      <arg value="exit" />
    </exec>
    
    <echo message="Task execution finished at : ${script::format-to-string(datetime::now())}" />
  </target>

Summary

That's all there is to it. Using WinSCP to FTP your files to your remote server is easy and straight-forward. Feel free to leave a comment if you would like me to further elaborate on anything within this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)