Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous gradle copy task?

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?

like image 308
user447607 Avatar asked Aug 11 '16 20:08

user447607


2 Answers

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
like image 136
Vyacheslav Shvets Avatar answered Oct 06 '22 10:10

Vyacheslav Shvets


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)
    }
}
like image 41
user447607 Avatar answered Oct 06 '22 12:10

user447607