Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / XML
Tip/Trick

Using FTP with Nant

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Sep 2014CPOL2 min read 10.6K   2  
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)


Written By
Technical Lead Gold-Vision CRM
United Kingdom United Kingdom
I am a professional software engineer and technical architect with over twenty years commercial development experience with a strong focus on the design and development of web and mobile applications.

I have experience of architecting scalable, distributed, high volume web applications that are accessible from multiple devices due to their responsive web design, including architecting enterprise service-oriented solutions. I have also developed enterprise mobile applications using Xamarin and Telerik Platform.

I have extensive experience using .NET, ASP.NET, Windows and Web Services, WCF, SQL Server, LINQ and other Microsoft technologies. I am also familiar with HTML, Bootstrap, Javascript (inc. JQuery and Node.js), CSS, XML, JSON, Apache Cordova, KendoUI and many other web and mobile related technologies.

I am enthusiastic about Continuous Integration, Continuous Delivery and Application Life-cycle Management having configured such environments using CruiseControl.NET, TeamCity and Team Foundation Services. I enjoy working in Agile and Test Driven Development (TDD) environments.

Outside of work I have two beautiful daughters. I am also an avid cyclist who enjoys reading, listening to music and travelling.

Comments and Discussions

 
-- There are no messages in this forum --