Sunday, June 30, 2013

Ant: copying a directory from one windows system to another

Problem
How to copy a directory from a windows system to a remote windows system using ant
there is one scp command and FTP command, please provide me an example to perform this task using scp and ftp.
Also scp requires SSH which is not common for windows system.
So how to use SCP on windows system using ant
Also if you know any better approach for windows system using ant or java, please share
Also there is one SC command(don't know how to use it)

Solutions
1.
Download:
<scp file="${username}:${password}@${ip}:${path-to-file}" todir="${dir}" trust="true" />
or Upload:
<scp file="${path-to-file}" todir="${username}:${password}@${ip}:${dir}" trust="true" />  
Ant scp task provides attributes such as localFile and remoteFile to replace "file", and localTofile / localTodir and remoteTofile / remoteTodir to replace "dir". Using these attributes can help avoid confusion when you need some scp tasks to get files from the server to local machine, while others to upload files from the local machine to server.
Like this (for uploading):
<scp localFile="${path-to-file}" remoteTodir="${username}:${password}@${ip}:${dir}" trust="true" />
Check the ant manual to see more information: http://ant.apache.org/manual/Tasks/scp.html
Note:
1. Avoid copying multiple files; Copy a zip archive and a ant build file together with it, and unzip on target machine.
2. Using scp, you need to setup ssh server on the target machine; you also need to put jsch.jar in your ANT_HOME/lib. The jsch.jar can be downloaded from

2.
You should go for using FTP to copy files between the windows machines. There are other protocal too, but you should go with FTP.
A sample for using FTP.
<ftp server="${server.location}"
   remotedir="${directory.to.copy}"
   userid="${ftp.username}"
   password="${ftp.password}"
   depends="yes">
   <fileset dir="**Files****"/>
</ftp>
You can further look for details here. There are many options to try there.

3.
To copy a directory from your local host to a remote host using the ant scp, you have to specify the parent of the directory to the fileset and include the directory name eg.
<scp todir="user@hostname:/destination/dir/" keyfile="id_dsa" passphrase="abc" sftp="true" >
    <fileset dir="${parentDir}/"  >
        <include name="**/${dirToCopy}/" />
    </fileset>
</scp> 
sftp="true" helped another user on this subject so I included it. In my case I believe that adding the "/" at the end of the parentDir and dirToCopy, which signifies a directory at the command line, finally let me copy a directory and not just it's contents.

No comments:

Post a Comment