如何使用Gradle将文件复制到多个位置?

时间:2016-05-27 11:58:02

标签: gradle build copy

我想定义一个将文件复制到四个不同目录的Gradle任务。似乎复制任务只允许单个目标位置。

// https://docs.gradle.org/current/userguide/working_with_files.html#sec:copying_files
task copyAssets(type: Copy) {
  from 'src/docs/asciidoc/assets'
  //into ['build/asciidoc/html5/assets', 'build/asciidoc/pdf/assets']
  into 'build/asciidoc/pdf/assets'
}


task gen(dependsOn: ['copyAssets', 'asciidoctor']) << {
    println "Files are generated."
}

如何在不定义四个不同任务的情况下复制文件?

我目前的解决方案是:

// https://docs.gradle.org/current/userguide/working_with_files.html#sec:copying_files
task copyAssetsPDF(type: Copy) {
  from 'src/docs/asciidoc/assets'
  into 'build/asciidoc/pdf/assets'
}
task copyAssetsHTML5(type: Copy) {
  from 'src/docs/asciidoc/assets'
  into 'build/asciidoc/html5/assets'
}
task copyAssetsDB45(type: Copy) {
  from 'src/docs/asciidoc/assets'
  into 'build/asciidoc/docbook45/assets'
}
task copyAssetsDB5(type: Copy) {
  from 'src/docs/asciidoc/assets'
  into 'build/asciidoc/docbook5/assets'
}


task gen(dependsOn: ['copyAssetsPDF', 'copyAssetsHTML5', 'copyAssetsDB45', 'copyAssetsDB5', 'asciidoctor']) << {
    println "Files are generated."
}

1 个答案:

答案 0 :(得分:1)

其中一个解决方案是使用许多复制规范制作单个任务,例如:

task copyAssets << {
    copy {
        from 'src/docs/asciidoc/assets'
        into 'build/asciidoc/pdf/assets'
    }
    copy {
        from 'src/docs/asciidoc/assets'
        into 'build/asciidoc/pdf/assets'
    }
}

或者你可以在循环中完成:

//an array containing destination paths
def copyDestinations = ['build/asciidoc/pdf/assets', 'build/asciidoc/html5/assets']

//custom task to copy into all the target directories
task copyAssets << {
    //iterate over the array with destination paths
    copyDestinations.each { destination ->
        //for every destination define new CopySpec
        copy {
            from 'src/docs/asciidoc/assets'
            into destination
        }
    }
}
相关问题