Asynchronous gradle copy task?

China☆狼群 提交于 2020-01-14 09:36:19

问题


So I create an archive, say a war, and then I want another copy with a different name for convenience. Thing is that I don't want that copy task to slow down the rest of this rather large build. Possible to execute it asynchronously? If so, how?


回答1:


In some cases, it's very handy to use parallel execution feature for this. It works only with multiproject builds (the tasks you want to execute parallel must be in separate projects).

project('first') {
  task copyHugeFile(type: Copy) {
    from "path/to/huge/file"
    destinationDir buildDir
    doLast {
      println 'The file is copied'
    }
  }
}

project('second') {
  task printMessage1 << {
    println 'Message1'
  }

  task printMessage2 << {
    println 'Message2'
  }
}

task runAll {
  dependsOn ':first:copyHugeFile'
  dependsOn ':second:printMessage1'
  dependsOn ':second:printMessage2'
}

The default output:

$ gradle runAll

:first:copyHugeFile
The file is copied
:second:printMessage1
Message1
:second:printMessage2
Message2
:runAll

The output with --parallel:

$ gradle runAll --parallel

Parallel execution is an incubating feature.
:first:copyHugeFile
:second:printMessage1
Message1
:second:printMessage2
Message2
The file is copied
:runAll



回答2:


import java.util.concurrent.*
...
def es = Executors.newSingleThreadExecutor()
...
war {
...
doLast{
        es.submit({
            copy {
                from destinationDir.absolutePath + File.separator + "$archiveName"
                into destinationDir
                rename "${archiveName}", "${baseName}.${extension}"

            }
        } as Callable)
    }
}


来源:https://stackoverflow.com/questions/38905329/asynchronous-gradle-copy-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!