Gradle SSH插件复制父文件夹而不仅仅是文件

时间:2016-01-12 16:52:54

标签: gradle gradle-ssh-plugin

我正在使用this Gradle SSH plugin。它有一个方法put,它将文件从我的本地机器移动到会话所连接的机器。

我的应用已完全构建并存在于build/app中,我正在尝试将其移至/opt/nginx/latest/html/,以便文件build/app/index.html存在于/opt/nginx/latest/html/index.html且任何build/app的子文件夹也会被复制。

我的build.gradle:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'org.hidetake:gradle-ssh-plugin:1.1.4'
  }
}

apply plugin: 'org.hidetake.ssh'

remotes {
  target {
    host = '<my target vm>'
    user = 'user'
    password = 'pass'
  }
}

...

task deploy() << {
  ssh.run {
    session(remotes.target) {
      put from: 'build/app/', into: '/opt/nginx/latest/html/'
    }
  }
}

如上所述,它将所有文件放入/opt/nginx/latest/html/app。如果我将from更改为使用fileTree(dir: 'build/app'),那么所有文件都会被复制,但我会丢失文件结构,即build/app/scripts/main.js被复制到/opt/nginx/latest/html/main.js而不是预期的/opt/nginx/latest/html/scripts/main.js 1}}。

如何在保留文件夹结构的同时将一个目录(不是目录本身)的CONTENTS复制到目标目录中?

3 个答案:

答案 0 :(得分:2)

查看插件的代码,它说:

output_row.id = input_row.id.replaceAll("[^\\w]","").replaceAll("_", "");;
output_row.mrp = input_row.mrp;

您正在使用选项#1,您正在提供 static usage = '''put() accepts following signatures: put(from: String or File, into: String) // put a file or directory put(from: Iterable<File>, into: String) // put files or directories put(from: InputStream, into: String) // put a stream into the remote file put(text: String, into: String) // put a string into the remote file put(bytes: byte[], into: String) // put a byte array into the remote file''' (也可以是目录),而您应该使用#2,这将是{{1}的可迭代列表孩子们。所以我会尝试:

File

编辑:或者,

build/app

答案 1 :(得分:1)

您可以为FileTree目录创建一个build/app对象,然后将整个树结构ssh到您的远程实例:

FileTree myFileTree = fileTree(dir: 'build/app')

task deploy() << {
  ssh.run {
    session(remotes.target) {
      put from: myFileTree.getDir(), into: '/opt/nginx/latest/html/'
    }
  }

它应该复制你的结构和文件,如:

// 'build/app'         -> '/opt/nginx/latest/html/
// 'build/app/scripts' -> '/opt/nginx/latest/html/scripts/'
// 'build/app/*.*'     -> 'opt/nginx/latest/html/*.*'
// ...

答案 2 :(得分:0)

您可以添加通配符来复制文件夹中的所有文件:

put from: 'build/app/*', into: '/opt/nginx/latest/html/'