Search code examples
gradlegroovyantscp

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


I use gradle to copy files from remote machines to local machine. Following is the snippet.

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

Above snippet, works fine in a windows shell but fails in a ubuntu shell with the following error.

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)...

A regular scp from terminal works just fine as expected. It is only the ant.scp that fails only in the linux environment. The taskdef declaration is ant.taskdef(name: 'scp', classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp', classpath:somepath). Any pointers/references to cause of the issue will be useful.

While debugging, I just figured out that the variable localFileDirPath has @ symbol. For example, the localDir name is "sample@localDirectory". Now, I guess ant.scp assumes that "sample@localDirectory" is another remote server - due to which the error message completely makes sense. When I test with another localFileDirPath with no @, ant.scp works just fine. Now, in my case, the local directory will have @. So, I am figuring out, how to escape this character.


Solution

  • As per the ant documentation on the scp task, use localToDir instead of toDir when you have @ characters in the path:

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

    with localToDir you don't need to escape the @ character, just send it in as is.