2016-08-11 127 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