一、介绍。
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。
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、支持的软件。
|
3、获取phing。
$> svn checkout http://svn.phing.info/trunk |
4、安装phing。
i、PEAR Install。
$> pear channel-discover pear.phing.info $> pear install phing/phing |
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" /> |
标签属性:
|
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> |
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> |
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"/> |
五、文件目录
$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
七、命令行参数。
|
八、程序思维导图
错误状态一:
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
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