Reputation: 24759
I'm using this Gradle SSH plugin. It has a method put
that will move files from my local machine to the machine the session is connected to.
My app is fully built and exists in build/app
and I'm trying to move it to /opt/nginx/latest/html/
such that the file build/app/index.html
would exist at /opt/nginx/latest/html/index.html
and that any subfolders of build/app
are also copied over.
My 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/'
}
}
}
As I have it above, it's putting all the files into /opt/nginx/latest/html/app
. If I change the from
to use fileTree(dir: 'build/app')
then all the files get copied over but I lose the file structure, i.e. build/app/scripts/main.js
gets copied to /opt/nginx/latest/html/main.js
instead of the expected /opt/nginx/latest/html/scripts/main.js
.
How can I copy the CONTENTS of one directory (not the directory itself) into the target directory while retaining folder structure?
Upvotes: 2
Views: 4150
Reputation: 53
You could create a FileTree
object for your build/app
directory and then ssh your entire tree structure to your remote instance:
FileTree myFileTree = fileTree(dir: 'build/app')
task deploy() << {
ssh.run {
session(remotes.target) {
put from: myFileTree.getDir(), into: '/opt/nginx/latest/html/'
}
}
It should copy your structure and files like:
// 'build/app' -> '/opt/nginx/latest/html/
// 'build/app/scripts' -> '/opt/nginx/latest/html/scripts/'
// 'build/app/*.*' -> 'opt/nginx/latest/html/*.*'
// ...
Upvotes: 2
Reputation: 23785
Looking through the plugin's code, it says:
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'''
You're using option #1 where you are providing a File
(which can also be a directory), while you should be using #2, which would be an iterable list of build/app
's children. So I would try:
put (from: new File('build/app').listFiles(), into: '/opt/nginx/latest/html/')
Edit: Alternatively,
new File('build/app').listFiles().each{put (from:it, into:'/opt/nginx/latest/html/')}
Upvotes: 3
Reputation: 676
You could add a wildcard to copy all files in the folder:
put from: 'build/app/*', into: '/opt/nginx/latest/html/'
Upvotes: 0