ant.scp 失败,本地目标不支持从远程服务器复制到远程服务器

ant.scp fails with Copying from a remote server to a remote server is not supported for local destination

我使用 gradle 将文件从远程机器复制到本地机器。以下是片段。

    ant.scp(file: "${userName}:${pwd}@${hostName}:${remoteFileAbsPath}", todir: localFileDirPath, trust: "true")

上面的代码片段,在 windows shell 中运行良好,但在 ubuntu shell 中运行失败,并出现以下错误。

Caused by: : Copying from a remote server to a remote server is not supported.
at org.apache.tools.ant.taskdefs.optional.ssh.Scp.execute(Scp.java:229)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(BasicAntBuilder.java:77)...

来自终端的常规 scp 工作正常。只有 ant.scp 仅在 linux 环境中失败。 taskdef 声明是 ant.taskdef(name: 'scp', classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp', classpath:somepath)。任何导致问题的 pointers/references 都会有用。

调试的时候才发现变量localFileDirPath有@符号。例如,localDir 名称是 "sample@localDirectory"。现在,我猜 ant.scp 假设 "sample@localDirectory" 是另一个远程服务器 - 因此错误消息完全有意义。当我用另一个没有 @ 的 localFileDirPath 进行测试时,ant.scp 工作得很好。现在,在我的例子中,本地目录将有@。所以,我想知道如何转义这个角色。

根据 ant documentation on the scp task,当路径中有 @ 个字符时,使用 localToDir 而不是 toDir

  ant.scp(file: "${userName}:${pwd}@${hostName}:${remoteFileAbsPath}", localtodir: localFileDirPath, trust: "true")

localToDir 你不需要转义 @ 字符,只需按原样发送即可。