我是Gradle的新手,我正在使用一个小任务来启动/停止本地tomcat服务器。
我的gradle任务:
task startTomcat(type: Exec){
workingDir ENV['CATALINA_HOME'] + '/bin'
commandLine 'cmd', '/c', 'catalina.bat'
args 'start'
}
当我运行此任务时,tomcat会启动。但是任务没有完成并且一直在等待。 我已经通过了Exec documentation page,但无法得到任何线索。
请帮忙。
答案 0 :(得分:4)
感谢Peter的意见。以下是使用ProcessBuilder
:
/* Configuration for tomcat */
task catalinaConfig {
ext.binDir = ENV['CATALINA_HOME'] + "\\bin"
}
task startTomcat << {
println "Tomcat path: " + catalinaConfig.binDir
ProcessBuilder pb = new ProcessBuilder(["cmd", "/c", "catalina.bat", "start"]);
pb.directory(new File(catalinaConfig.binDir));
println "Starting tomcat now ... "
Process proc = pb.start();
proc.waitFor();
println "Tomcat is coming up now ... " + proc.exitValue()
}
task stopTomcat << {
println "Tomcat path: " + catalinaConfig.binDir
ProcessBuilder pb1 = new ProcessBuilder(["cmd", "/c", "catalina.bat", "stop"]);
pb1.directory(new File(catalinaConfig.binDir));
println "Stopping tomcat now ... "
Process proc1 = pb1.start();
proc1.waitFor();
}
答案 1 :(得分:0)
由于您在Windows上运行,您可能会遇到此错误:Process execution hangs on Windows if child processes are still alive。不幸的是,我没有更简单的解决方法来建议。
答案 2 :(得分:0)
要在外部流程启动后立即退出,我使用了:
task startServer(type:Exec) {
ProcessBuilder pb = new ProcessBuilder("cmd","/c","start.bat")
pb.directory(new File("workingFolder"))
pb.start();
}