In my Gradle build, I want to define a reusable function for copying files to a remote host. Inside the function I want to use the scp Ant task. The code below works:
def remoteCopy(todir) {
ant.scp(
todir: todir,
passphrase: XXXXXXXX,
keyfile: XXXXXXXX) {
fileset(dir: 'config') { // I want this to be passed
include(name: '**/*.txt') // in as a parameter to the
} // function
}
}
task example {
remoteCopy('user@host:/home/xxxxxxx/')
}
However, I don't want to hardcode the fileset(s) inside the remoteCopy function. I want to be able to call the function something like this (if this syntax is possible):
remoteCopy('user@host:/home/xxxxxxx/') {
ant.fileset(dir: 'config') {
include(name: '**/*.txt')
}
}
Or perhaps as a second parameter:
remoteCopy('user@host:/home/xxxxxxx/',
ant.fileset(dir: 'config') { include(name: '**/*.txt') } )
Can someone who knows Groovy and/or Gradle help?
For the sake of completeness, to make it easier to reproduce, this is how I am initializing the scp Ant task within my Gradle script:
configurations { ant_jsch }
repositories { mavenCentral() }
dependencies { ant_jsch 'org.apache.ant:ant-jsch:1.8.1' }
ant.taskdef(name: 'scp',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
classpath: configurations.ant_jsch.asPath)
Does this work?
def remoteCopy( todir, Closure fset ) {
ant.scp( todir: todir, passphrase: XXXXXXXX, keyfile: XXXXXXXX) {
fset()
}
}
remoteCopy( 'user@host:/home/xxxxxxx/' ) {
ant.fileset( dir: 'config' ) {
include( name: '**/*.txt' )
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With