如何在 Ant 中启动 PHP 服务器、运行 PHP 单元和停止 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>
文档中关于其用法的注释:
If you spawn a command, its output will not be logged by ant.
The input, output, error, and result property settings are not active when spawning a process.
我终于找到了一些可行的解决方案。要获取 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=""/>
<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>
我在 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>
文档中关于其用法的注释:
If you spawn a command, its output will not be logged by ant. The input, output, error, and result property settings are not active when spawning a process.
我终于找到了一些可行的解决方案。要获取 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=""/>
<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>