upload file with FTP using nant

2019-03-15 14:01发布

问题:

I have an NAnt script that I use to build my .NET project, and I'm looking to see if there is a way to upload the resulted assemblies to some remote folder using an FTP task.

I couldn't find any good example online, and I'm wondering if anyone knows how to do it, if it's doable at all.

FYI: I'm running it on a windows machine, if it makes any difference.

回答1:

You could use WinSCP as console application in a NAnt <exec> task. Using WinSCP will give You access to extra goodies like synchronization.

That's what we are doing and it works like a charm.



回答2:

Working WinSCP example here:

    <exec
        verbose="true"
        program="WinSCP.exe" 
        basedir="${WinSCP.Folder.Install}">
        <arg value="/command" />
        <arg value="/log=D:\Logs\WinSCP\winscp.log" />
        <arg value="/script=backup.winscp" />
        <arg line="/parameter ${YOUR_FILE}" />
    </exec>

where backup.winscp in above exec is a file with the following content

option batch abort
option confirm off 
open ftp://user:password@ftp.yourhost.com
put "%1%"
exit


回答3:

Having the same need myself, I developed a basic FTP upload NAnt task. You can find it here: https://sourceforge.net/projects/w3c-nant/

Example usage (copy pasted from the site's API docs):

<ftpUpload host="ftp.myserver.com" username="root" password="r00t" todir="/">
    <fileset basedir="dist">
        <include name="**/*" />
        <exclude name="**/*.config" />
    </fileset>
</ftpUpload>

I already use it in my local build scripts to upload my site to its live server.



回答4:

You may use the WinSCP (free ftp client for windows) and integrate it through nant "exec" tag. The setup is fairly straight forward and once it's done it works like a charm.



回答5:

We using something like this (NAnt-0.86-beta1):

<!-- Copies files from artifacts folder to destination folder -->
<target name="deploy-configuration">
  <!-- Generate temporary folder for the processed web.config -->
  <property name="generated-config-file-path" value="${path::get-temp-path()}${common::GenerateGUID()}" />
  <mkdir dir="${generated-config-file-path}" />
  <!-- Copy -->
  <copy file="${artifacts.dir}/web.config" tofile="${generated-config-file-path}/web.config" />
  <!-- Update web.config with values for our destination environment before we deploy. -->
  <update-configuration-path file="${generated-config-file-path}\web.config" />
  <!-- Deploy using FTP -->
  <connection id="ftp-transfer-connection"
    server="${project.deployment.ftp.server}"
    username="${project.deployment.ftp.user}"
    password="${project.deployment.ftp.password}"
    />

  <ftp connection="ftp-transfer-connection"
       showdironconnect="false"
       createdirs="true"
       verbose="true"
       exec="true"
       logfiles="false"
       >

    <put type="bin"
         localdir="${generated-config-file-path}"
         remotedir="${project.deployment.path.remote}"
         flatten="false"
         >
      <include name="**\web.config" />
    </put>
  </ftp>
  <delete dir="${generated-config-file-path}" />
</target>


<target name="deploy">

  <connection id="ftp-transfer-connection"
    server="${project.deployment.ftp.server}"
    username="${project.deployment.ftp.user}"
    password="${project.deployment.ftp.password}"
    />

  <ftp connection="ftp-transfer-connection"
       showdironconnect="false"
       createdirs="true"
       verbose="true"
       exec="true"
       logfiles="false"
       >

    <put type="bin"
         localdir="${artifacts.dir}"
         remotedir="${project.deployment.path.remote}"
         flatten="false"
         >
      <include name="**\bin\**" />
      <include name=".\*.svc" />
      <include name=".\web.config" />
    </put>
  </ftp>
  <!-- Deploy configuration -->
  <call target="deploy-configuration" />
</target>


回答6:

I would like to share with you my task. I just use arg values. It works ok. Here's my script:

<property name="path.to.root" value="${project::get-base-directory()}\"/>
<property name = "deploy.folder" value = "${path.to.root}Deploy" />

<!-- FTP -->
<property name = "ftp.host" value = "127.0.0.1"/>
<property name = "ftp.port" value="21"/>
<property name = "ftp.user" value = "username"/>
<property name = "ftp.password" value="mypass"/>        
<property name = "ftp.local.dir" value = "${deploy.folder}" />

<!-- WinSCP-->
<property name = "winscp.run" value ="${path.to.root}\tools\WinSCP\WinSCP.com"/>

<target description="Copy to the FTP server" name="ftp.copy">
    <exec program="${winscp.run}">
        <arg value="/command" />
        <arg value="option batch abort" />
        <arg value="option confirm off" />
        <arg value="option transfer binary" />
        <arg value="open ftp://${ftp.user}:${ftp.password}@${ftp.host}:${ftp.port}" />
        <arg value= '"put ""${ftp.local.dir}"""' />         
        <arg value="close" />
        <arg value="exit" />
    </exec>     
</target>

Have fun!