如何使用gradle中的"复制"任务复制目录

Boh*_*ian 12 directory copy gradle

我想将一系列文件和整个目录复制到单个Copy任务中的另一个目录中.我可以复制单个文件和目录的内容,但是如何复制目录本身?

这是我的任务:

task myTask(type: Copy) {
    from 'path/to/file'
    from 'path/to/dir'
    into 'path/to/target'
}
Run Code Online (Sandbox Code Playgroud)

它复制文件OK,但只复制dir中的文件.我想最终得到dir的内容path/to/target/dir(不是在path/to/target).

我找到了一个使用的工作:

task myTask(type: Copy) {
    from 'path/to/file'
    from 'path/to'
    into 'path/to/target'
    include 'dir'
}
Run Code Online (Sandbox Code Playgroud)

但这容易发生名称冲突.我实际上有很多文件和dirs要复制,我想把它作为一项任务.

小智 9

有一本书是可供下载的摇篮网站上点击这里 或点击"获取免费电子书" 在这里:"摇篮超越基础"提供了一个直接的答案第3页(你的问题转化目录结构).

应用于您的示例,解决方案如下:

    task myTask(type: Copy) {
        from 'path/to/file'
        from 'path/to/dir' {
            into 'dir'
        }
        into 'path/to/target'
    }
Run Code Online (Sandbox Code Playgroud)

限制是它不会从源动态确定第二级目标目录(dir)的名称,但它是一种干净的方法.


Ada*_*ker 8

我知道的唯一解决方案:

task myTask(type: Copy) {
    into 'path/to/target'
    from 'path/to/file'

    into ('dir') {
       from 'path/to/dir'        
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住,into: ('dir')建筑工程相对于path/to/target/位置而言