2016-08-11 123 views
4

因此,我創建了一個檔案,說一場戰爭,然後爲了方便起見,我想另一個名稱不同的副本。事情是我不希望這個複製任務減慢這個相當大的構建的其餘部分。可以異步執行它?如果是這樣,怎麼樣?異步gradle複製任務?

+1

不知道,如果這個實際工作,但你可以試試看:HTTP:/ /stackoverflow.com/a/38528039/745574 – RaGe

+0

真的很好@RaGe,感謝您找到這個! – Opal

+0

我想我有這個工作。多謝你們。 :-)你想作爲答案發布嗎? – user447607

回答

1
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) 
    } 
} 
+0

這樣做嗎?有用。該文件被複制,但IDK如何測試以查看它是否實際上是一個異步副本。 – user447607

+0

在發生其他事情之前,doLast可以防止它被解僱。就像創造需要複製的戰爭一樣。 – user447607

+0

閱讀以上內容,我剛剛回答了我自己的問題。 – user447607

1

在某些情況下,爲此使用parallel execution feature非常方便。它僅適用於多項目構建(您要並行執行的任務必須位於不同的項目中)。

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' 
} 

默認輸出:

$ gradle runAll 

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

輸出與--parallel

$ gradle runAll --parallel 

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

由於dbUnit存在一些技術債務,我們無法使用它。問題是,真的沒有理由不能並行複製文件,所以我寧願寫它,以便它始終如此。 – user447607