如何使用Ant脚本在控制台中执行PHP文件,同时执行文件


How can I execute a PHP file using the Ant script with php output in console, while executing file

我的ant脚本

<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脚本中显示执行完成后的输出(在控制台中)。
我想在控制台显示回声,而文件在处理中…

Exec将每行输出到标准输出。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);
?>