13

What is a clean and elegant way to copy a bunch of files via scp with Gradle?

Two ways I currently see are:

Are there any better (more obvious) ways to approach this?

Rodrigo
  • 473
  • 8
  • 14
ngeek
  • 7,453
  • 11
  • 35
  • 42

2 Answers2

27

A few years after the original question, I like the Gradle SSH Plugin. A small quote of its extensive documentation:

We can describe SSH operations in the session closure.

session(remotes.web01) {
  // Execute a command
  def result = execute 'uptime'

  // Any Gradle methods or properties are available in a session closure
  copy {
    from "src/main/resources/example"
    into "$buildDir/tmp"
  }

  // Also Groovy methods or properties are available in a session closure
  println result
}

Following methods are available in a session closure.

  • execute - Execute a command.
  • executeBackground - Execute a command in background.
  • executeSudo - Execute a command with sudo support.
  • shell - Execute a shell.
  • put - Put a file or directory into the remote host.
  • get - Get a file or directory from the remote host.

...and allows for, for example:

task deploy(dependsOn: war) << {
  ssh.run {
    session(remotes.staging) {
      put from: war.archivePath.path, into: '/webapps'
      execute 'sudo service tomcat restart'
    }
  }
}
Arjan
  • 21,528
  • 11
  • 59
  • 70
13

From a project of mine that I use to SCP files to an EC2 server. The jar files there are local files that are part of my project, I forget where I got them from. There's probably a more concise way of doing all this, but I like to be very explicit in my build scripts.

configurations {
  sshAntTask
}

dependencies {
  sshAntTask fileTree(dir:'buildSrc/lib', include:'jsch*.jar')
  sshAntTask fileTree(dir:'buildSrc/lib', include:'ant-jsch*.jar')
}

ant.taskdef(
  name: 'scp',
  classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
  classpath: configurations.sshAntTask.asPath)

task uploadDbServer() {
  doLast  {
    ant.scp(
      file: '...',
      todir: '...',
      keyfile: '...' )
  }
}
Shorn
  • 16,252
  • 13
  • 74
  • 140
  • Many Thanks @Shorn for this clean and explicit way (and put it into an own configuration), which I do also anticipate in advantage to allow easier maintenance. – ngeek Nov 03 '12 at 11:29
  • Both of those artifacts are available from Maven central. It makes the config very easy. – Ryan J Apr 11 '13 at 11:47
  • I'm noticing that Gradle comes with jsch... so isn't there a better, more Gradle way, to do this? – Jason Nov 07 '13 at 07:10
  • 2
    your dependencies could read like this, from maven: `sshAntTask 'org.apache.ant:ant-jsch:1.7.1', 'jsch:jsch:0.1.29'` – djKianoosh Apr 14 '14 at 19:41
  • @djKianoosh yes maybe it's better to put that as the default answer.. for gradle noobs like me it adds yet another hurdle. – Felipe Sep 25 '14 at 22:11
  • http://mrhaki.blogspot.com/2009/12/gradle-goodness-using-optional-ant-task.html describes much the same approach. Another good example – Peter Kahn Jul 07 '15 at 18:09