我有一个ant任务,它使用< exec>执行冗长的构建操作. Ant由
Windows命令行中的批处理文件启动.如果我通过关闭窗口来终止ant任务,则该过程由< exec>启动.继续跑步.当ant进程本身终止时,如何终止生成的进程?
在带有Oracle JDK 8的Windows 7 x64上使用Ant 1.10.0.启动该过程的任务类似于:
<exec executable="${make.executable}" dir="${compile.dir}" failonerror="true">
<arg line="${make.parameters}" />
</exec>
关闭命令行窗口时,正确终止运行ant的java进程.
最佳答案 这是一个可能的解决方案:
>批处理脚本使用名为antPidFile的参数启动Ant.
> Ant脚本使用Java jps工具获取运行Ant脚本的java.exe进程的PID.
> Ant脚本将PID写入antPidFile.
> Ant脚本生成子进程.
> Ant退出并控制返回批处理脚本.
>批处理脚本将以前的Ant脚本的PID加载到变量中.
>批处理脚本使用内置的wmic工具来识别Ant生成的进程.
>批处理脚本使用内置的taskkill工具来终止Ant生成的所有子进程(和孙子进程).
build.xml文件
<project name="ant-kill-child-processes" default="run" basedir=".">
<target name="run">
<fail unless="antPidFile"/>
<exec executable="jps">
<!-- Output the arguments passed to each process's main method. -->
<arg value="-m"/>
<redirector output="${antPidFile}">
<outputfilterchain>
<linecontains>
<!-- Match the arguments provided to this Ant script. -->
<contains value="Launcher -DantPidFile=${antPidFile}"/>
</linecontains>
<tokenfilter>
<!-- The output of the jps command follows the following pattern: -->
<!-- lvmid [ [ classname | JARfilename | "Unknown"] [ arg* ] [ jvmarg* ] ] -->
<!-- We want the "lvmid" at the beginning of the line. -->
<replaceregex pattern="^(\d+).*$" replace="\1"/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
<!-- As a test, spawn notepad. It will persist after this Ant script exits. -->
<exec executable="notepad" spawn="true"/>
</target>
</project>
批处理脚本
setlocal
set DeadAntProcessIdFile=ant-pid.txt
call ant "-DantPidFile=%DeadAntProcessIdFile%"
rem The Ant script should have written its PID to DeadAntProcessIdFile.
set /p DeadAntProcessId=< %DeadAntProcessIdFile%
rem Kill any lingering processes created by the Ant script.
for /f "skip=1 usebackq" %%h in (
`wmic process where "ParentProcessId=%DeadAntProcessId%" get ProcessId ^| findstr .`
) do taskkill /F /T /PID %%h