一尘不染

Gradle Exec任务未退出

tomcat

我是Gradle的新手,并且正在使用一个小任务来启动/停止本地tomcat服务器。

我的gradle任务:

task startTomcat(type: Exec){
    workingDir ENV['CATALINA_HOME'] + '/bin'
    commandLine 'cmd', '/c', 'catalina.bat'
    args 'start'
}

当我运行此任务时,tomcat会启动。但是任务尚未完成,并且一直在等待。我已经浏览了Exec文档页面,但没有任何线索。

请帮忙。


阅读 290

收藏
2020-06-16

共1个答案

一尘不染

感谢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();
}
2020-06-16