如何在gradle任务中通过scp复制目录?
使用Gradle通过scp复制一堆文件的干净而优雅的方法是什么?
我目前看到的两种方式是:
- 使用Apache Wagon,如下所述: http : //markmail.org/message/2tmtaffayhq25g4s
- 使用Exec任务通过命令行执行scp
有没有更好(更明显)的方法来解决这个问题?
从我使用的项目到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: '...' ) } }
在最初的问题之后几年,我喜欢Gradle SSH插件 。 其广泛文档的一个小引用:
我们可以在会话闭包中描述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
– 从远程主机获取文件或目录。
……并允许,例如:
task deploy(dependsOn: war) << { ssh.run { session(remotes.staging) { put from: war.archivePath.path, into: '/webapps' execute 'sudo service tomcat restart' } } }