2017-01-07 8 views
2

<exec>を使用して長いビルド操作を実行するantタスクがあります。 Antは、Windowsのコマンドラインからのバッチファイルによって開始されます。ウィンドウを閉じてantタスクを終了すると、プロセスは<exec>で起動し続けます。 antプロセス自体が終了したときに、生成されたプロセスを終了するにはどのようにすればよいですか?Apache Ant:プロセス開始時に<exec>が終了しました

Antの1.10.0がOracle JDK 8でのWindows 7のx64上で使用されているプロセスを開始するタスクは次のようになります。

<exec executable="${make.executable}" dir="${compile.dir}" failonerror="true"> 
    <arg line="${make.parameters}" /> 
</exec> 

コマンドラインウィンドウを閉じるときjavaプロセス実行中のアリが正常に終了しました。

答えて

1

はここで可能なソリューションです:

  • バッチスクリプトはantPidFile名前付き引数でのAntを起動します。
  • Antスクリプトでは、Java jpsツールを使用して、java.exeプロセスのPIDをAntスクリプトを実行して取得します。
  • 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 
+0

ユーザーがコマンドラインウィンドウを閉じた場合、私はバッチスクリプトの実行が継続しているとは思いません。面白いアプローチだが、ありがとう! – DevCybran

関連する問題