在Gradle中并行化复制任务

时间:2018-03-06 07:25:45

标签: gradle build.gradle

我在Gradle 4版本中完成了以下任务:

task copyWsdlFromArtifacts(type: Copy) {
    includeEmptyDirs = false
    from((configurations.wsdlAndXsdSourcesCompile.files + configurations.wsdlAndXsdSources.files).collect { zipTree(it)})
    into "$buildDir/schema/"
    include '**/*.xsd', '**/*.wsdl'
}

现在这是一个瓶颈。有没有办法以某种方式并行化它?非常感谢你!

2 个答案:

答案 0 :(得分:1)

这个任务在配置阶段做了很多“工作”,所有解压缩和计算每个解压缩文件的文件哈希值。您可以使用拉链作为任务输入来提高性能,这意味着只有在任务不是UP-TO-DATE

时才会进行解压缩

例如:

task copyWsdlFromArtifacts {
    inputs.files configurations.wsdlAndXsdSourcesCompile
    inputs.files configurations.wsdlAndXsdSources
    outputs.dir "$buildDir/schema/"

    doLast {
        copy {
            includeEmptyDirs = false
            from((configurations.wsdlAndXsdSourcesCompile.files + configurations.wsdlAndXsdSources.files).collect { zipTree(it)})
            into "$buildDir/schema/"
            include '**/*.xsd', '**/*.wsdl'            
        }
    }
}

答案 1 :(得分:0)

我最终为每个工件创建了一个任务并且并行运行整个构建:

(configurations.wsdlAndXsdSourcesCompile.files + configurations.wsdlAndXsdSources.files).each { artifact ->
    tasks.create(name: "copyWsdlFromArtifacts$artifact", type: Copy) {
        from zipTree(artifact)
        into "$buildDir/schema/"
        include '**/*.xsd', '**/*.wsdl'
    }

    wsdl2java.dependsOn(tasks.findByName("copyWsdlFromArtifacts$artifact"))
}

希望它有助于某人