FileTree文件的异步操作?
有没有一种方法可以轻松地在 gradle 任务中以智能方式处理 FileTree
文件?我基本上需要等待所有文件的执行,就像使用 GPars 所做的那样,但是如何使用 FileTree 执行此 gradle 操作?
task compressJs(dependsOn: [copyJsToBuild]) << {
println 'Minifying JS'
fileTree {
from 'build/js'
include '**/*.js'
}.visit { element ->
if (element.file.isFile()) {
println "Minifying ${element.relativePath}"
ant.java(jar: "lib/yuicompressor-2.4.6.jar", fork: true) {
arg(value: "build/js/${element.relativePath}")
arg(value: "-o")
arg(value: "build/js/${element.relativePath}")
}
}
}
}
如果我能做类似 .visit{}.async(wait:true)
的事情那就太好了,但我的谷歌搜索什么也没找到。有没有办法可以轻松地使其成为多线程?一个元素的处理不会影响任何其他元素的处理。
Is there a way I can easily make the processing of FileTree
files in a smart way in gradle tasks? I basically need to wait for the execution of all files, much like what you can do with GPars, but how do I do this gradle with FileTree?
task compressJs(dependsOn: [copyJsToBuild]) << {
println 'Minifying JS'
fileTree {
from 'build/js'
include '**/*.js'
}.visit { element ->
if (element.file.isFile()) {
println "Minifying ${element.relativePath}"
ant.java(jar: "lib/yuicompressor-2.4.6.jar", fork: true) {
arg(value: "build/js/${element.relativePath}")
arg(value: "-o")
arg(value: "build/js/${element.relativePath}")
}
}
}
}
It would be lovely if I could do something like .visit{}.async(wait:true)
, but my googling turned up nothing. Is there a way I can easily make this multi-threaded? The processing of one element has no effect on the processing of any other element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在考虑使用多线程之前,我会尝试以下操作:
如果这仍然让您对性能不满意,并且您无法使用性能更高的压缩器,那么您仍然可以尝试使用多线程。 Gradle 还无法在这方面为您提供帮助,但 GPars 或 Java Fork/Join 框架等库可以。
Before thinking about going multi-threaded, I'd try the following:
If this still leaves you unhappy with the performance, and you can't use a more performant minifier, you can still try to go multi-threaded. Gradle won't help you there (yet), but libraries like GPars or the Java Fork/Join framework will.
GPars 解决方案。请注意,可以修改 compress() 函数以正确接受源目录/目标目录/等,但由于我的所有名称都是一致的,所以我现在只使用一个参数。我能够将构建时间从
7.3s
缩短到5.4s
,只缩小了 3 个文件。我见过构建时间急剧失控,所以我总是对这种行为的性能保持警惕。The GPars solution. Note that the
compress()
function could be modified to properly accept source dir/target dir/etc, but since all my names are consistent, I'm just using the one argument for now. I was able to cut my build time from7.3s
to5.4s
with only 3 files being minified. I've seen build times spiral out of control, so I'm always wary of performance with this kind of behavior.