簡體   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