ant - 将文件复制并解压缩到远程计算机 - ant

标签 ant ssh exec scp

我需要从本地计算机复制 zip 文件并粘贴到远程计算机中,然后在远程计算机中解压缩这些文件。

我知道第一部分可以使用 scp 完成(从本地复制 zip 文件并粘贴到远程计算机),但如何使用 ant 完成第二部分?

提前致谢

最佳答案

您可以使用sshexec task在远程计算机上调用命令行 unzip 命令(假设远程计算机已安装 unzip)。

<!-- 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 '" />

unzip 命令可以采用许多其他选项,请参阅其 man page了解完整详情。例如,-j 选项将忽略 zip 文件内的任何目录层次结构,并将所有提取的文件直接放入目标目录中。并且-o将强制覆盖目标目录中的现有文件而不提示。

关于ant - 将文件复制并解压缩到远程计算机 - ant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922157/

相关文章:

maven - 从 pom.xml 文件运行外部 Build.xml 文件

java - 创建 ANT 脚本以在 Tavis-CI 上构建并在提交时运行 JUnit 测试

c++ - 我可以使用 Putty 和 sublime 进行远程编译吗?

java - 使用 `Runtime.getRuntime().exec()` 获取 `top` 的输出

c - execvp() 不处理写入输出 >filename.log

java - 当某些输入文件失败时,如何使 JavaC 保留良好的类文件?

java - 在 ant 脚本中运行 GWTComplier 时出现问题

file - Cygwin ssh - 身份文件无法访问

powershell - 在 PowerShell 中重用正在运行的 PuTTY 代理(选美)

scala - 执行外部命令