Real solution found by using SimonTheSorcerer’s approach (see http://stackoverflow.com/questions/12387407/use-jsch-to-implement-scp-and-additionally-not-reinvent-the-wheel)
Don’t use ant builder + taskdef, depend on the jars which ant uses and handle things more directly
build.gradle
dependencies { ... compile 'org.apache.ant:ant:1.9.3' compile 'org.apache.ant:ant-jsch:1.9.3' compile 'com.jcraft:jsch:0.1.51' compile 'javamail:javamail:1.3.2' compile gradleApi() }
RemoteCopy.groovy
`class RemoteCopy {
def host
def user
File keyfile
public RemoteCopy(def host, def user, File keyfile) {
this.host = host
this.user = user
this.keyfile = keyfile
}
/**
* Copies fileset to remove destination via scp user@host
* @param from fileset describing what should be copied
* @param remoteDestinationPath
*/
public upload(File fromDir, def copyPattern, def remoteDestinationPath) {
AntBuilder ant = new AntBuilder()
def fileset = ant.fileset(dir: fromDir) {
include(name: copyPattern)
}
SSHExec ssh = new SSHExec(host: host, username: user,
keyfile: keyfile.absolutePath,
passphrase: "",
trust: "yes",
command: "mkdir -p ${remoteDestinationPath}",
verbose: "true")
ssh.execute()
Scp scp = new Scp(keyfile: keyfile.absolutePath, passphrase: "")
scp.setRemoteTodir("${user}@${host}:${remoteDestinationPath}")
scp.addFileset(fileset)
scp.setProject(new Project()); // prevent a NPE (Ant works with projects)
scp.setTrust(true); // workaround for not supplying known hosts file
scp.execute();
}
`