【问题标题】:How to start PHP server, run PHPUnit, and stop PHP server in Ant?如何在 Ant 中启动 PHP 服务器、运行 PHPUnit 和停止 PHP 服务器?
【发布时间】:2015-08-06 17:16:03
【问题描述】:

我在 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 单元完成(成功或失败)时停止服务器?

【问题讨论】:

    标签: php ant phpunit


    【解决方案1】:

    我终于找到了一些可行的解决方案。要获取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>
    

    【讨论】:

      【解决方案2】:

      如果您希望 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 记录。 生成进程时,输入、输出、错误和结果属性设置不活动。

      【讨论】:

      • @PetraBarus 不是 PHP 专家,但 this question 可能很接近。
      • 我们可以获取生成的exec 的 PID 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2016-12-03
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      相关资源
      最近更新 更多