Asynchronous gradle task?

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

+4
source share
2 answers
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)
    }
}
+1
source

In some cases, it is very convenient to use the parallel execution function for this. It works only with assemblies of multiprojects (the tasks that you want to perform in 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'
}

Default output:

$ gradle runAll

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

Output from --parallel:

$ gradle runAll --parallel

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

Source: https://habr.com/ru/post/1650963/


All Articles