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?
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
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With