有没有办法合并" gradle任务以避免许多dependsOn声明

时间:2015-03-19 15:15:30

标签: gradle

假设这个gradle脚本:

task copyGroovyScript(dependsOn: prepare, type: Copy) {
    from "${scriptSrcLocation}/${scriptSrcName}"
    into buildFolderZipSource
}

task copyDependenciesForGroovyScript(dependsOn: copyGroovyScript, type: Copy) {
     from configurations.groovyScript.resolve()
     into "${buildFolderZipSource}/groovy-plugin-lib"
}

task copyTestScripts(dependsOn: copyDependenciesForGroovyScript, type: Copy ) {
    from "${scriptSrcLocation}/ReadClient.groovy"
    into "${buildFolderZipSource}/test"
}

task copyTestScriptsBin(dependsOn: copyTestScripts, type: Copy ) {
    from "${scriptSrcLocation}/bin"
    into "${buildFolderZipSource}/test/bin"
}

task copyDependenciesForTestScripts(dependsOn: copyTestScriptsBin, type: Copy) {
    from configurations.testScripts.resolve()
    into "${buildFolderZipSource}/test/lib"
}

task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
    archiveName "output-${buildTime()}.zip"
    excludes ['*.zip']
    destinationDir buildFolder
    from buildFolder
}

我需要不同的Copy任务才能拥有不同的输出文件夹。

有没有办法避免必须拥有所有这些dependsOn语句,并且只是以某种方式在文件中声明的顺序执行gradle?

1 个答案:

答案 0 :(得分:1)

没有办法按照宣布的方式执行。但是你为什么不这样走:

task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
    doFirst {
       copy {
          from "${scriptSrcLocation}/${scriptSrcName}"
          into buildFolderZipSource
       }
    }
    //following doFirst and so on..
    archiveName "output-${buildTime()}.zip"
    excludes ['*.zip']
    destinationDir buildFolder
    from buildFolder
}

修改

在评论中讨论后,结果证明以下代码应该完成工作

task prepare {
   doFirst {
       copy {
          from "${scriptSrcLocation}/${scriptSrcName}"
          into buildFolderZipSource
       }
    }
    //following doFirst and so on..
}

task packageAll(dependsOn: prepare, type:Zip) {
    archiveName "output-${buildTime()}.zip"
    excludes ['*.zip']
    destinationDir buildFolder
    from buildFolder
}