2013-04-24 51 views
0

我的ant腳本我如何執行使用與控制檯輸出PHP Ant腳本的PHP文件,在執行文件

<target name="testRunPHP"> 
    <exec executable="${php}" failonerror="true" dir="${source}"> 
      <arg line="${local.deploydir}/application/controllers/sleepTest.php"/> 
      <arg line="-output" /> 
    </exec> 
</target> 

我sleepTest.php

header('Content-type: text/html; charset=utf-8'); 
header("Cache-Control: no-cache, must-revalidate"); 
header ("Pragma: no-cache"); 
set_time_limit(0); 
apache_setenv('no-gzip', 1); 
ini_set('zlib.output_compression', 0); 
ini_set('implicit_flush', 1); 
for ($i = 0; $i < 10; $i++) { 
    $randSlp=rand(1,3); 
    echo "Sleeping for ".$randSlp." second. ";; 
    sleep($randSlp); 
    if(ob_get_level()>0) 
     ob_end_flush(); 
} 
ob_implicit_flush(1); 

如果文件運行在瀏覽器中比在執行文件
時顯示輸出,但是在執行完成後在ant腳本顯示輸出(在控制檯中)時顯示輸出。
我想在控制檯中顯示回聲,同時文件正在處理...

回答

3

Exec將每行輸出到stdout。你的PHP腳本中的問題是你輸出到一行。所有你需要做的是在echo "Sleeping for ".$randSlp." second. \n";的末尾添加'\ n'。我已經與這些腳本測試這一點:

的build.xml

<project name="exectest" default="test"> 

    <dirname property="build.dir" file="${ant.file}" /> 

    <target name="test"> 

      <exec executable="cmd"> 
       <arg value="/c" /> 
       <arg value="C:\wamp\bin\php\php5.3.13\php.exe" /> 
       <arg value="${build.dir}\test.php" /> 
      </exec> 

    </target> 

</project> 

test.php的

<?php 

header('Content-type: text/html; charset=utf-8'); 
header("Cache-Control: no-cache, must-revalidate"); 
header ("Pragma: no-cache"); 
set_time_limit(0); 
apache_setenv('no-gzip', 1); 
ini_set('zlib.output_compression', 0); 
ini_set('implicit_flush', 1); 
for ($i = 0; $i < 10; $i++) { 
    $randSlp=rand(1,3); 
    echo "Sleeping for ".$randSlp." second. \n"; 
    sleep($randSlp); 
    if(ob_get_level()>0) 
     ob_end_flush(); 
} 
ob_implicit_flush(1); 

?> 
+0

謝謝。 @PatrykRoszczyniała。成功運行。你能幫我運行'C:\ xampp \ htdocs \ MyCodeIgniterApp \ index.php my_controller/index' 它在命令提示符下運行成功,但在螞蟻顯示錯誤'[exec]無法打開輸入文件:C:\ xampp \ htdocs \ MyCodeIgniterApp \ index.php my_controller/index''[exec]結果:1' – 2013-04-24 11:05:54

+0

這不是文件'C:\ xampp \ htdocs \ MyCodeIgniterApp \ index.php my_controller/index'。這是什麼'my_controller/index'?它是一個PHP網站的參數?最好的辦法是編輯問題並粘貼你想運行的ant腳本。 BTW。如果答案是正確的,你應該檢查它是正確的/有幫助的。 – pepuch 2013-04-24 11:10:30

+0

感謝您的幫助。 my_controller是codeigniter控制器的php文件。索引是該控制器的行爲。傳遞控制器,而不是在參數和它與cmd的工作,但不能在螞蟻腳本中工作 – 2013-04-24 11:16:03