繁体   English   中英

如何启动PHP服务器,运行PHPUnit,并在Ant中停止PHP服务器?

[英]How to start PHP server, run PHPUnit, and stop PHP server in Ant?

我在Ant中有这样的目标

<target name="test">
    <exec executable="php" failonerror="true">
        <arg value="-S"/>
        <arg value="localhost:80"/>
        <arg value="-t"/>
        <arg value="web"/>
    </exec>
    <exec executable="phpunit" failonerror="true">
        <arg value="tests"/>
    </exec>
</target>

问题是当我运行它时,目标将因PHP内置服务器而阻塞。 如何启动PHP服务器,然后运行PHP单元,然后在PHP单元完成(成功或失败)时停止服务器?

如果你想让Ant生成php进程,你可以在任务调用中设置spawn="true"

<exec executable="php" failonerror="true" spawn="true">
    <arg value="-S"/>
    <arg value="localhost:80"/>
    <arg value="-t"/>
    <arg value="web"/>
</exec>

但是从文档中注意它的用法:

如果您生成一个命令,它的输出将不会被ant记录。 在生成进程时,输入,输出,错误和结果属性设置不活动。

我终于有了一些有效的解决方案 要获取PHP服务器PID,我需要执行ps命令然后执行正则表达式以获取PID然后终止服务器。

<project name="myapp" default="test" basedir=".">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="ant-contrib.jar" />
    <target name="start-server">
        <echo>Starting PHP server</echo>
        <exec executable="php" spawn="true">
            <arg value="-S"/>
            <arg value="localhost:8080"/>
            <arg value="-t"/>
            <arg value="${basedir}"/>
        </exec>
        <sleep seconds="1"/>
    </target>
    <target name="stop-server">
        <!-- Getting the PHP server PID -->
        <exec executable="ps">
            <arg value="ax"/>
            <redirector outputproperty="php.ps">
                <outputfilterchain>
                    <linecontains>
                        <contains value="php"/>
                        <contains value="localhost:8080"/>
                    </linecontains>
                </outputfilterchain>
            </redirector>
        </exec>
        <propertyregex property="php.pid" input="${php.ps}" regexp="^\s+(\d+)" select="\1"/>
        <echo>Killing PHP server at ${php.pid}</echo>
        <exec executable="kill">
            <arg value="${php.pid}"/>
        </exec>
    </target>
    <target name="test">
        <antcall target="start-server"></antcall>
        <echo>Starting test</echo>
        <sleep seconds="3"/>
        <echo>Finishing test</echo>
        <antcall target="stop-server"></antcall>
    </target>
</project>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM