How to copy directory via scp within gradle task?

*爱你&永不变心* 提交于 2019-11-27 16:11:15

问题


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

Two ways I currently see are:

  • Using Apache Wagon, as described here: http://markmail.org/message/2tmtaffayhq25g4s
  • Executing scp via command line with the Exec task

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


回答1:


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: '...' )
  }
}



回答2:


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'
    }
  }
}


来源:https://stackoverflow.com/questions/13183433/how-to-copy-directory-via-scp-within-gradle-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!