如何通过函数将文件集传递给Gradle Ant任务?

时间:2012-08-03 14:37:43

标签: groovy gradle

在我的Gradle构建中,我想定义一个可重用的函数,用于将文件复制到远程主机。在函数内部我想使用scp Ant任务。以下代码有效:

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/')
}

但是,我不想在remoteCopy函数内硬编码文件集。我希望能够像这样调用函数(如果可以使用这种语法):

remoteCopy('user@host:/home/xxxxxxx/') {
    ant.fileset(dir: 'config') {
       include(name: '**/*.txt')
    }
}

或者可能作为第二个参数:

remoteCopy('user@host:/home/xxxxxxx/',
    ant.fileset(dir: 'config') { include(name: '**/*.txt') } )

知道Groovy和/或Gradle的人可以帮忙吗?


为了完整起见,为了更容易重现,这就是我在Gradle脚本中初始化scp Ant任务的方法:

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)

1 个答案:

答案 0 :(得分:4)

这有用吗?

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' )
  }
}