如何在后台执行gradle外部任务(Exec)? [英] How to execute a gradle external task (Exec) in background?
问题描述
我使用gradle和gulp构建并启动基于JDK6 +的JEE Web应用程序,并且Web容器是Jetty。
Gulp习惯于处理静态资源,例如concat和minify javascript / css文件。另外,我的gulp脚本中有一个 watch
任务,它用于监视静态文件更改并自动重建它们。
$ b
- 使用gulp
[gulp task:build]
。 - 观察静态文件更改
[gulp task:watch]
,我不直接调用这个gulp任务,它是由gradle任务通过 Exec 类[gradle task:watch]
调用的。 > - 构建我的java代码并启动Jetty
[gradle task:jettyRun]
。
因为我添加了gradle任务 watch
作为 jettyRun
任务的依赖关系,所以我尽管我只需要从命令行调用 gradle jettyRun
,我的应用程序将会启动。但结果与我预期的不一样。
以下是我的脚本文件:
build.gradle
apply plugin :'war'
apply plugin:'jetty'
repositories {
jcenter()
}
//在这里省略依赖关系
任务监视(类型:Exec){
workingDir$ {projectDir}
//将构建类型传递给npm和gulp。
commandLinegulp,watch
}
jettyRun.dependsOn watch
gulpfile.js
var gulp = require '吞');
gulp.task('build',function(callback){
//移除代码以使这个问题尽可能简单。
});
$ b $ gulp.task('watch',function(callback){
gulp.watch(['./ src / static / ** / *。js'],['build' ]);
if(callback!= null)callback();
});
结果:
问题:
现在,当执行 gradle watch
任务,对于gradle执行 jettyRun
任务没有任何更改。我知道这个挂起是由gulp启动的监视进程造成的,因为它正在监视文件更改。但我希望Gradle只是启动吞噬监视进程并立即返回以执行下一个 jettyRun
任务!
如何做到这一点?另外,我想从stdout中看到watch任务的输出。
我知道在java中有一个ProcessBuilder,我试过了,但它不起作用。也许我做错了什么。 :(
)您可以使用 ProcessBuilder
在后台启动一个外部进程,让gradle继续执行其他任务。 finalizedBy
可用于停止 runJetty
已完成。
任务手表{
doFirst {
println(Start watching )
ext.process = new ProcessBuilder()
.directory(projectDir)
.command(gulp,watch)
.start()
}
}
任务stopWatching {
doFirst {
println(Stop watch)
if(tasks.watch.process!= null){
tasks.watch.process.destroy()
}
}
}
任务runJetty {
finalizedBy stopWatching
}
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 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.
So to launch the application in Jetty, i need to do following things:
- 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]
.
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.
Following is my script files:
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();
});
The result:
Question:
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.
I know there is a ProcessBuilder in java, i have tried it, but it doesn't work. Maybe I have done something wrong. :(
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屋!