Use optional scp ant task within custom gradle plugin

What’s the right way to use an optional ant task within a custom gradle task defined in my plugin?

I have a plugin and am trying to implement an rsync like ability and I’m wrapping ant’s scp task because I don’t want to reimplement it. Is this possible? I’m also concerned that this will work on during plugin development but require all kind of classpath manuvers when I try to use the plugin.

It used to run but now fails thus:

Caused by: jar:file:/builds/tools/Common/gradle/gradle-2.0/lib/ant-1.9.3.jar!/org/apache/tools/ant/antlib.xml:37:     
Problem: failed to create task or type componentdef
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

In plugin’s build.gradle
compile 'jsch:jsch:0.1.29’
compile 'javamail:javamail:1.3.2’
compile gradleApi()

In RemoteCopy.groovy
def ant = new AntBuilder()
ant.project.getBuildListeners().firstElement().setMessageOutputLevel(3)
ant.sshexec(host: host, username: user,
keyfile: keyfile.absolutePath,
passphrase: “”,
trust: “yes”,
command: “mkdir -p ${remoteDestinationPath}”,
verbose: “true”)

    ant.scp(todir: "${user}@${host}:${remoteDestinationPath}",
            keyfile: keyfile.absolutePath,
            passphrase: "",
            trust: "yes",
            verbose: "true",
            failonerror: "true") {
        fileset(dir: fromDir) {
            include(name: copyPattern)
        }
    }

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();

}

`

I’d recommend using the Gradle SSH plugin instead. It has SCP functionality built in.