如何在后台执行 gradle 外部任务(Exec)? [英] How to execute a gradle external task (Exec) in background?
问题描述
我正在使用 gradle 和 gulp 构建和启动我的基于 JDK6+ 的 JEE Web 应用程序,并且 Web 容器是 Jetty.
I'm using gradle and gulp to build and launch my JEE web application that is based on JDK6+, and the web container is Jetty.
Gulp 用于处理静态资源,例如 concat 和 minify javascript/css 文件.另外,我的 gulp 脚本中有一个 watch
任务,它用于监视静态文件更改并自动重建它们.
Gulp is used to process static resource, such as concat and minify javascript/css files. Also, there is a watch
task in my gulp script, it is used to watch static file changes and rebuild them automatically.
所以要在 Jetty 中启动应用程序,我需要做以下事情:
So to launch the application in Jetty, i need to do following things:
- 使用 gulp 构建静态资源
[gulp task: build]
. - 观察静态文件变化
[gulp task: watch]
,我不直接调用这个gulp任务,是gradle任务通过Exec类调用[gradle task: watch]
. - 构建我的 java 代码并启动 Jetty
[gradle task: jettyRun]
.
- Build static resource using gulp
[gulp task: build]
. - Watch static file changes
[gulp task: watch]
, i don't call this gulp task directly, it is call by a gradle task via Exec class[gradle task: watch]
. - Build my java code and launch Jetty
[gradle task: jettyRun]
.
由于我添加了 gradle 任务 watch
作为 jettyRun
任务的依赖项,所以我只需要从命令调用 gradle jettyRun
行,我的应用程序将启动.但结果和我预想的不一样.
Since i added the gradle task watch
as a dependency of jettyRun
task, so i though i only need to call gradle jettyRun
from command line, my application will be launched. But the result is not same as i expected.
以下是我的脚本文件:
build.gradle
apply plugin: 'war'
apply plugin: 'jetty'
repositories {
jcenter()
}
// omit the dependencies here
task watch(type: Exec){
workingDir "${projectDir}"
// Pass build type to npm and gulp.
commandLine "gulp", "watch"
}
jettyRun.dependsOn watch
gulpfile.js
var gulp = require('gulp');
gulp.task('build', function(callback) {
// removed the code to make this question as simple as possible.
if (callback != null) callback();
});
gulp.task('watch', function(callback) {
gulp.watch(['./src/static/**/*.js'], ['build']);
if (callback != null) callback();
});
<小时>
结果:
问题:
现在,执行gradle watch
任务时进程挂起,gradle执行jettyRun
任务没有变化.我知道这个挂起是由 gulp 启动的监视进程引起的,因为它正在监视文件更改.但我希望 gradle 只启动 gulp watch 进程并立即返回执行下一个 jettyRun
任务!
Now, the process hungs when excutes the gradle watch
task, there is no change to execute jettyRun
task for gradle. I know this hang is caused by the watch process launched by gulp, because it is watching file changes. But i hope that gradle just only launch the gulp watch process and return immediately to execute the next jettyRun
task!
如何做到这一点?另外,我想从标准输出中查看监视任务的输出.
How to do this? Also, i want to see the output of watch task from stdout.
我知道java中有一个ProcessBuilder,我试过了,但它不起作用.也许我做错了什么.:(
I know there is a ProcessBuilder in java, i have tried it, but it doesn't work. Maybe I have done something wrong. :(
推荐答案
你可以使用ProcessBuilder
在后台启动一个外部进程,让gradle继续其他任务.finalizedBy
也可用于在 runJetty
完成时停止观看.
You can use ProcessBuilder
to launch an external process in the background and let gradle continue with other tasks. Also finalizedBy
can be used to stop watching when runJetty
has been completed.
task watch {
doFirst {
println("Start watching")
ext.process = new ProcessBuilder()
.directory(projectDir)
.command("gulp", "watch")
.start()
}
}
task stopWatching {
doFirst {
println("Stop watching")
if (tasks.watch.process != null) {
tasks.watch.process.destroy()
}
}
}
task runJetty {
finalizedBy stopWatching
}
这篇关于如何在后台执行 gradle 外部任务(Exec)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!