在gradle复制任务中使用通配符

时间:2016-12-05 22:38:59

标签: gradle

我想使用通配符复制目录,但Gradle from任务的copy方法不接受通配符。

// this doesn't work
task copyDirectory(type: Copy) {
        from "/path/to/folder-*/"
        into "/target"
}
// this does
task copyDirectory(type: Copy) {
        from "/path/to/folder-1.0/"
        into "/target"
}

1 个答案:

答案 0 :(得分:7)

只需使用'include'任务属性来指定您需要复制的目录的确切文件,如下所示:

task copyDirectory(type: Copy) {
    from "/path/to/"
    include 'test-*/'
    into "/target"
}

更新:如果您只想复制目录内容,那么您必须单独处理每个文件,如下所示:

task copyDirectory(type: Copy) {
    from "/path/to/"
    include 'test-*/'
    into "/target"
    eachFile {
        def segments = it.getRelativePath().getSegments() as List
        it.setPath(segments.tail().join("/"))
        return it
    }
    includeEmptyDirs = false
}