Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy and unzip files to remote machine - ant

Tags:

scp

ssh

exec

ant

I need to copy the zip files from local machine and paste in remote machine and unzip those files in remote machine.

I know the first part can be done using the scp (copy zip files from local and paste in remote machine) but how to do the second part using ant?

Thanks in advance

like image 379
coolgokul Avatar asked Oct 07 '22 07:10

coolgokul


1 Answers

You could use the sshexec task to call the command line unzip command on the remote machine (assuming the remote machine has unzip installed).

<!-- local directory containing the files to copy -->
<property name="archives" location="C:\path\to\zipfiles" />
<property name="archives.destination" value="/home/testuser/archives" />
<property name="unzip.destination" value="/home/testuser/unpacked" />

<fileset id="zipfiles.to.copy" dir="${archives}" includes="*.zip" />

<!-- copy the archives to the remote server -->
<scp todir="${user}:${password}@host.example.com:${archives.destination}">
  <fileset refid="zipfiles.to.copy" />
</scp>

<!-- Build the command line for unzip - the idea here is to turn the local
     paths into the corresponding paths on the remote, i.e. to turn
     C:\path\to\zipfiles\file1.zip;C:\path\to\zipfiles\file2.zip... into
     /home/testuser/archives/file1.zip /home/testuser/archives/file2.zip

     For this to work there must be no spaces in any of the zipfile names.
-->
<pathconvert dirsep="/" pathsep=" " property="unzip.files" refid="zipfiles.to.copy">
  <map from="${archives}" to="${archives.destination}" />
</pathconvert>

<!-- execute the command.  Use the "-d" option to unzip so it will work
     whatever the "current" directory on the remote side -->
<sshexec host="host.example.com" username="${user}" password="${password}"
  command="/bin/sh -c '
    for zipfile in ${unzip.files}; do
      /usr/bin/unzip -d ${unzip.destination} $$zipfile ; done '" />

The unzip command can take a number of other options, see its man page for full details. For example the -j option will ignore any directory hierarchy inside the zip files and put all the extracted files directly in the target directory. And -o will force overwrite existing files in the target directory without prompting.

like image 121
Ian Roberts Avatar answered Oct 10 '22 01:10

Ian Roberts