2013-04-24 71 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