Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass filesets into a Gradle Ant task via a function?

Tags:

gradle

groovy

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)
like image 638
ᴇʟᴇvᴀтᴇ Avatar asked Jan 27 '26 18:01

ᴇʟᴇvᴀтᴇ


1 Answers

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' )
  }
}
like image 148
tim_yates Avatar answered Jan 30 '26 15:01

tim_yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!