phing研究

一、介绍。

1、What Phing is?

Phing is a project build system based on Apache ant [ant]. You can do anything withPhing that you could do with a traditional build system like Gnu make[gnumake], and Phing's use ofsimple XML build files and extensible PHP "task" classesmake it an easy-to-use and highly flexible build framework.

Phing源自Binarycloud的一个子项目.Binaryclound是一个高度工程化的框架,为了在企业环境中使用而设计.Binarycloud广泛使用XML来存储关于项目的元数据(配置,节点,窗口小部件,站点结构,等等).因为Binarycloud是为PHP构建的,在每一个页面请求上执行XML处理和转换是不切实际的.Phing用于”编译”XML元数据为可被PHP引擎处理的数组形式.


2、How Phing Works?

% phing -f mybuildfile.xml mytarget
phing默认混寻找build.xml的buildfile。


二、安装phing。

1、系统需求。

To use Phing you must have installed PHP version 5.2 or abovecompiled --with-libxml2, as well as --with-xsl if you want to make useof advanced functionality.

2、支持的软件。

SoftwareRequired forSource
PHP 5.2+Executionhttp://www.php.net
PHPUnit 3.4.0+Optional; enables additional task(s)http://www.phpunit.de
Xdebug 2.0.0+Optional; enables additional task(s)http://www.xdebug.org
SimpleTest 1.0.1 beta+Optional; enables additional task(s)http://simpletest.sourceforge.net
Creole 1.1.0+Optional; enables additional task(s)http://creole.phpdb.org
PhpDocumentor 1.4.0+ (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/PhpDocumentor
VersionControl_SVN (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/VersionControl_SVN
VersionControl_Git (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/VersionControl_Git
PHP_CodeSniffer (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/PHP_CodeSniffer
Archive_Tar (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/Archive_Tar
Services_Amazon_S3 (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/Services_Amazon_S3
HTTP_Request2 (PEAR package)Optional; enables additional task(s)http://pear.php.net/package/HTTP_Request2
PHP DependOptional; enables additional task(s)http://www.pdepend.org
PHP Mess DetectorOptional; enables additional task(s)http://www.phpmd.org
PHP Copy/Paste DetectorOptional; enables additional task(s)http://pear.phpunit.de
DocBlox 0.11.0+Optional; enables additional task(s)http://www.docblox-project.org


3、获取phing。

$> svn checkout http://svn.phing.info/trunk

4、安装phing。

    i、PEAR Install。

$> pear channel-discover pear.phing.info
$> pear install phing/phing
The pear installer will check any dependencies and place thephing script (phing or phing.bat) into your PHP script directoy (i.e.where the "pear" script resides).


    ii、Non-PEAR Install。

       配置步骤:添加完整路径bin/目录到path变量中。

                   设置PHING_HOME环境变量指向phing的安装目录。

                   设置PHP_CMMAND=/usr/local/bin/php

                   设置PHP_CLASSPATH环境变量,指向PHING_HOME/classes。另外,也一并加上phing/classes的目录到PHP include_path ini设置中。

                   检测php.ini文件,确保如下两个参数配置:

                            max_execution_time=0 // 不限制执行时间

                            memory_limit=32M // 需要更多的内存来build files

       Unix样例

       假设phing安装于/opt/phing.需要做下列相关配置。

  export PHP_COMMAND=/usr/bin/php
  export PHING_HOME=/opt/phing
  export PHP_CLASSPATH=${PHING_HOME}/classes
  export PATH=${PATH}:${PHING_HOME}/bin


       Windows样例

  set PHP_COMMAND=c:\opt\php\php.exe
  set PHING_HOME=c:\opt\phing
  set PHP_CLASSPATH=c:\opt\phing\classes
  set PATH=%PATH%;%PHING_HOME%\bin

三、初步了解。

1、例子。完成创建一个新目录,拷贝制定文件到新目录中。并打包。
<?xml version="1.0" encoding="UTF-8"?>

<project name="FooBar" default="dist">

    <!-- ============================================  -->
    <!-- Target: prepare                               -->
    <!-- ============================================  -->
    <target name="prepare">
        <echo msg="Making directory ./build" />
        <mkdir dir="./build" />
    </target>

    <!-- ============================================  -->
    <!-- Target: build                                 -->
    <!-- ============================================  -->
    <target name="build" depends="prepare">
        <echo msg="Copying files to build directory..." />

        <echo msg="Copying ./about.php to ./build directory..." />
        <copy file="./test.php" tofile="./build/test.php" />
    </target>

    <!-- ============================================  -->
    <!-- (DEFAULT)  Target: dist                       -->
    <!-- ============================================  -->
    <target name="dist" depends="build">
        <echo msg="Creating archive..." />

        <tar destfile="./build/build.tar.gz" compression="gzip">
            <fileset dir="./build">
                <include name="*" />
            </fileset>
        </tar>

        <echo msg="Files copied and compressed in build directory OK!" />
    </target>
</project>

根据上边例子,我们需要熟悉一下target的调用机制。即调用顺序。

2、Target Element:A target can depend on other targets. You might have a targetfor installing the files in the build tree。

<target name="A" />
<target name="B" depends="A" />
<target name="C" depends="B" />
<target name="D" depends="C,B,A" />
执行顺序:Suppose we want to execute target D.From its depends attribute, first Ais executed, then B, then C, and finally D.

标签属性:

AttributeMeaningRequired
nameThe name of the targetYes
dependsA comma-seperated list of targets this target depends on.No
ifThe name of the Property that has to be set inorder for this target to be executedNo
unlessThe name of the Property that must not beset in order for this target to be executed. 


3、Task Elements:A task is a piece of PHP code that can be executed. This code implements a particular action to perform (i.e. install a file).Therefore it must be defined in the buildfile so that it is actually invoked by Phing.


4、Property Elemet:Properties are essentially variables that can be used in thebuildfile. These might be set in the buildfile by calling thePropertyTask,or might be set outside Phing on the command line (properties set onthe command line always override the ones in the buildfile). 

                                     Properties may be used in thevalue of task attributes. This is done by placing the property namebetween "${" and "}" in the attribute value. 

5、复杂一点儿的例子。

<?xml version="1.0"  encoding="UTF-8" ?>

<project name="testsite" basedir="." default="main">
    <property file="./build.properties" />

    <property name="package"  value="${phing.project.name}" override="true" />
    <property name="builddir" value="./build/testsite" override="true" />
    <property name="srcdir"   value="${project.basedir}" override="true" />

    <!-- Fileset for all files -->
    <fileset dir="." id="allfiles">
        <include name="**" />
    </fileset>

    <!-- ============================================  -->
    <!-- (DEFAULT) Target: main                        -->
    <!-- ============================================  -->
    <target name="main" description="main target">
        <copy todir="${builddir}">
            <fileset refid="allfiles" />
        </copy>
    </target>

    <!-- ============================================  -->
    <!-- Target: Rebuild                               -->
    <!-- ============================================  -->
    <target name="rebuild" description="rebuilds this package">
        <delete dir="${builddir}" />
        <phingcall target="main" />
    </target>
</project>

四、Project Components

1、Project

In the structure of a Phing buildfile, there must be exactly one Projectdefined;

<?xml version="1.0"?>

<project name="test" description="Simple test build file" default="main" >
  <!-- Everything else here -->
<project>


2、Task

Tasks are responsible for doing the work in Phing. 


3、Basic Types

i、FileSet

FileSets are groups of files. You can include or excludespecific files and patterns to/from a FileSet. The use of patterns isexplained below.

<fileset dir="/tmp" id="fileset1">
  <include name="sometemp/file.txt" />
  <include name="othertemp/**" />
  <exclude name="othertemp/file.txt" />
</fileset>

<fileset dir="/home" id="fileset2">
  <include name="foo/**" />
  <include name="bar/**/*.php" />
  <exclude name="foo/tmp/**" />
</fileset>
If you simply want to match a part of a filename or dirname, you use *. If you want to include multiple directories and/or files, you use **.


ii、FileList

FileLists, like FileSets, are collections of files; however, aFileList is an explicitly defined list of files -- and the files don'tnecessarily have to exist on the filesystem.

<filelist dir="base/" files="file1.txt,file2.txt,file3.txt"/>

<!-- OR: -->
<filelist dir="basedir/" listfile="files_to_process.txt"/>

iii、FilterChains and Filters

FilterChains can be compared to Unix pipes.

<filterchain>
  <replacetokens>
    <token key="BC_PATH" value="${top.builddir}/"/>
    <token key="BC_PATH_USER" value="${top.builddir}/testsite/user/${lang}/"/>
  </replacetokens>

  <filterreader classname="phing.filters.TailFilter">
    <param name="lines" value="10"/>
  </filterreader>
</filterchain>
it will only return the last 10 lines of the files.


iv、File Mappers:

With FilterChains and filters provide a powerful tool for changing contentsof files, Mappers provide a powerful tool for changing thenamesof files.

<mapper type="glob" from="*.bat" to="*.txt"/>
等同于dos下的copy *.bat *.txt


五、文件目录

$PHING_HOME
  |-- bin
  |-- classes
  |    `-- phing
  |         |-- filters
  |         |    `-- util
  |         |-- mappers
  |         |-- parser
  |         |-- tasks
  |         |    |-- ext
  |         |    |-- system
  |         |    |    `-- condition
  |         |    `-- user
  |         `-- types
  |-- docs
  |    `-- phing_guide
  `-- test
       |-- classes
	   `-- etc


六、Task,Types,Mappers可以通过php编写扩展功能。

链接:http://www.phing.info/docs/guide/current/chapters/ExtendingPhing.html#WritingTasks


七、命令行参数。

ParameterMeaning
-h -helpDisplay the help screen
-v -versionPrint version information and exit
-l -listList all available targets in buildfile
-q -quietQuiet operation, no output at all
-verboseVerbose, give some more output
-debugOutput debug information
-logfile <file>Use given file for log
-D<property>=<value>Set the property to the specified value to be used in thebuildfile
-find <file> 
-buildfile <file>Specify an alternate buildfile name. Default is build.xml
-logger path.to.LoggerSpecify an alternate logger. Default is phing.listener.DefaultLogger.Other options includephing.listener.NoBannerLogger,phing.listener.AnsiColorLogger,phing.listener.XmlLogger andphing.listener.HtmlColorLogger.
-propertyfile <file>Load properties from the specified file


八、程序思维导图



错误状态一:

BUILD FAILED
Error reading project file [wrapped: The SVN tasks depend on PEAR VersionControl_SVN package being installed.]
Total time: 0.0923 seconds

解决方法:安装VersionControl_SVN

[root@vm10110027 chenhao5]# /usr/local/bin/pear install -f VersionControl_SVN
WARNING: failed to download pear.php.net/VersionControl_SVN within preferred state "stable", will instead download version 0.3.4, stability "alpha"
WARNING: channel "pear.php.net" has updated its protocols, use "pear channel-update pear.php.net" to update
downloading VersionControl_SVN-0.3.4.tgz ...
Starting to download VersionControl_SVN-0.3.4.tgz (34,119 bytes)
.........done: 34,119 bytes

install ok: channel://pear.php.net/VersionControl_SVN-0.3.4



错误状态二:

BUILD FAILED
/home/chenhao5/phing/chenhao5/build.xml:28:19: Failed to run the 'svn update' command: svn: This client is too old to work with working copy 'trunk'; please get a newer Subversion client

解决办法:

参考页面:http://blog.csdn.net/yihui8/article/details/6154941

执行程序如下:

[root@vm10110027 chenhao5]# /usr/bin/python2.6 ./change-svn-wc-format.py kernel_imx 1.5
Converted WC at 'kernel_imx' into format 9 for Subversion 1.5


错误状态三:

Could not create task/type: 'svnupdate'. Make sure that this class has been declared using taskdef / typedef.

诸如此类的不能使用svn的错误,我跟踪了phing的源代码。大概意思如下,首先大家去phing/tasks/ext/svn/Svn下会发现只有SvnLastRevisionTask.php一个文件。而我们要用的文件时不在这个目录下的。

可以在网上下载如下文件。

-rw-r--r-- 1 root root 8284 Jul 12 14:12 SvnBaseTask.php
-rw-r--r-- 1 root root 2116 Jul 12 14:12 SvnCheckoutTask.php
-rw-r--r-- 1 root root 3767 Jul 12 14:12 SvnCommitTask.php
-rw-r--r-- 1 root root 1897 Jul 12 14:12 SvnCopyTask.php
-rw-r--r-- 1 root root 2132 Jul 12 14:12 SvnExportTask.php
-rw-r--r-- 1 root root 3713 Jul 12 14:12 SvnLastRevisionTask.php
-rw-r--r-- 1 root root 4082 Jul 12 14:12 SvnListTask.php
-rw-r--r-- 1 root root 3430 Jul 12 14:12 SvnLogTask.php
-rw-r--r-- 1 root root 2273 Jul 12 14:12 SvnSwitchTask.php
-rw-r--r-- 1 root root 2099 Jul 12 14:12 SvnUpdateTask.php

这时执行phing还是会继续错误的,需要编辑phing/tasks/defaults.properties,搜到刚才有svnlastrevision的行。添加如下指令。就会发现svn的操作都可以用了。这里困扰了我很久,最后还是跟源代码一步一步跟出来了。。

svncheckout=phing.tasks.ext.svn.SvnCheckoutTask
svncommit=phing.tasks.ext.svn.SvnCommitTask
svncopy=phing.tasks.ext.svn.SvnCopyTask
svnexport=phing.tasks.ext.svn.SvnExportTask
svnlist=phing.tasks.ext.svn.SvnListTask
svnlog=phing.tasks.ext.svn.SvnLogTask
svnswitch=phing.tasks.ext.svn.SvnSwitchTask
svnupdate=phing.tasks.ext.svn.SvnUpdateTask


错误状态四:

PHP Fatal error:  Class 'ZipArchive' not found in /home/chenhao5/phing/classes/phing/tasks/ext/UnzipTask.php on line 67

解决办法:http://apps.hi.baidu.com/share/detail/18549235

安装过程中,如centos系统中如发现没有装phpize等工具,使用yum -y install php*以及yum -y install php-devel。主要是后边这个命令生效。

make时,会报/usr/src/zip-1.10.2/php_zip.c:30:31: error: ext/pcre/php_pcre.h: No such file or directory。需要Edit/usr/src/zip-1.10.2/php_zip.c and replace "ext/pcre/php_pcre.h" with the absolute path of php_pcre.h

结果是,需要php5.2.x才可以,坑爹啊。

php的升级方法:http://hi.baidu.com/suping/blog/item/4c3393cad6a2ea8dc91768c5.html

第二种升级的时候,会遇到如下情况:

[root@vm10110027 usr]# yum update php -y
Setting up Update Process
No Packages marked for Update

此时记住先要输入以下yum clean all



错误状态五:

PHP Fatal error:  require_once(): Failed opening required 'System.php' (include_path='/home/chenhao5/phing/classes:.:/usr/share/pear:/usr/share/php') in /home/chenhao5/phing/classes/phing/tasks/ext/rSTTask.php on line 18


错误状态六:

/home/chenhao5/build.xml:19:7: Failed to run the 'svn export' command: svn: PROPFIND of 'xxxxxxx/miniblog/config': Server certificate verification failed: certificate issued for a different hostname, issuer is not trusted (https://svn1.intra.sina.com.cn)

无法checkout出https,加载ssl协议的路径内容。

参考解决方法:http://hi.baidu.com/mimitd/blog/item/d280b7098b3db1236a60fb61.html

1, 安装openssl

wget http://www.openssl.org/source/openssl-0.9.8.tar.gz
tar zxvf openssl-0.9.8.tar.gz
cd openssl-0.9.8
./config shared
make
sudo make install


2,安装 neon
wget http://www.webdav.org/neon/neon-0.29.0.tar.gz
 ./configure --with-ssl --with-libs=/usr/local/ssl/ --prefix=/usr/local/neon
make
sudo make install

3. 编译subversion时带上 --with-ssl
./configure --with-ssl --with-neon=/usr/local/neon
make
sudo make install




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值