14

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
  • 483
  • 8
  • 14
ngeek
  • 7,733
  • 11
  • 36
  • 42

2 Answers2

30

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
  • 22,808
  • 11
  • 61
  • 71
  • this should be the answer, and have way more upvotes – Bender Sep 06 '22 at 16:20
  • @Bender Sure, but It was written almost 3 years after the question been asked. – Kamil Oct 12 '22 at 20:12
  • Indeed, @Kamil Even more, my 7 years old answer may very well be outdated by now. (I've not used Gradle myself for many years now.) – Arjan Oct 14 '22 at 08:52
  • @Arjan sure but the plugin still works fine and is easy to implement into a build + i have yet to find smth similar on the gradle site/community forum and was exactly what i was looking for – Bender Oct 15 '22 at 01:56
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
  • 19,077
  • 15
  • 90
  • 168
  • 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