java 如何在gradle任务中通过scp复制目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13183433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:47:48  来源:igfitidea点击:

How to copy directory via scp within gradle task?

javagroovygradlescp

提问by ngeek

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

使用 Gradle 通过 scp 复制一堆文件的干净优雅的方法是什么?

Two ways I currently see are:

我目前看到的两种方式是:

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

有没有更好(更明显)的方法来解决这个问题?

采纳答案by Shorn

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.

从我使用的一个项目到 SCP 文件到 EC2 服务器。jar 文件是我项目的一部分的本地文件,我忘记了我从哪里得到它们。可能有一种更简洁的方式来做这一切,但我喜欢在我的构建脚本中非常明确。

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

回答by Arjan

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

在最初的问题几年后,我喜欢Gradle SSH Plugin。其广泛文档的一小段引述:

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.

我们可以在会话关闭中描述 SSH 操作。

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
}

以下方法在会话关闭中可用。

  • execute- 执行命令。
  • executeBackground- 在后台执行命令。
  • executeSudo- 在 sudo 支持下执行命令。
  • shell- 执行一个shell。
  • put- 将文件或目录放入远程主机。
  • get- 从远程主机获取文件或目录。

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