本次的环境构建基于window系统
一、工具下载
jenkins
官网:http://jenkins-ci.org/
php
官网下载: http://php.net/downloads.php
phar
下载网址:http://yun.baidu.com/share/link?shareid=667059160&uk=3928426386
其中包括:phpunit phpmd phploc phpdox phpcpd pdepend 等phar包。
在此帖发表时,几乎都是最新版本。把所有的phar统一放在一个目录下,方便接下来的操作
jenkins插件
5、Ant
官网下载:http://ant.apache.org/bindownload.cgi
二、配置
1、svn配置
2、ant配置
添加环境变量:ANT_HOME 值:ant安装目录
环境变量path 添加上路径 %ANT_HOME %/bin ,也就是ant.bat 文件所在目录
3、phar配置
添加环境变量:PHAR_HOME 值:上面下载的phar所在目录
然后在path变量添加上路径 %PHAR_HOME %
接着,为每一个phar文件编写一个批处理文件
如:创建一个phpuni.bat,然后写上以下内容,
@php "%~dp0phpunit.phar" %*
其他的类似,不过,我提供的phar下都已经有相应的批处理文件。
可以在命令控制台下 phpunit help 测试是否配置成功。可以调用则表示配置成功。
以上操作都是为实现能在命令控制台下,全局使用相关工具命令,如下图所说
至此,就可以调用相关的命令。
4、xml配置
在jenkins的工程目录下,添加以下xml配置文件
@build.xml (ant构建配置文件)
<?xml version="1.0" encoding="UTF-8"?> <project name="name-of-project" default="build"> <!-- By default, we assume all tools to be on the $PATH --> <property name="toolsdir" value=""/> <!--改成您相应的应用目录和测试目录--> <property name="testdir" value="${basedir}/Test"/> <property name="appdir" value="${basedir}/app"/> <!-- Uncomment the following when the tools are in ${basedir}/vendor/bin --> <!-- <property name="toolsdir" value="${basedir}/vendor/bin/"/> --> <target name="build" depends="prepare,lint,phploc-ci,pdepend,phpmd-ci,phpcpd-ci,phpunit,phpdox" description=""/> <target name="build-parallel" depends="prepare,lint,tools-parallel,phpunit,phpdox" description=""/> <target name="tools-parallel" description="Run tools in parallel"> <parallel threadCount="2"> <sequential> <antcall target="pdepend"/> <antcall target="phpmd-ci"/> </sequential> <antcall target="phpcpd-ci"/> <!--<antcall target="phpcs-ci"/>--> <antcall target="phploc-ci"/> </parallel> </target> <target name="clean" unless="clean.done" description="Cleanup build artifacts"> <delete dir="${basedir}/build/api"/> <delete dir="${basedir}/build/coverage"/> <delete dir="${basedir}/build/logs"/> <delete dir="${basedir}/build/pdepend"/> <delete dir="${basedir}/build/phpdox"/> <property name="clean.done" value="true"/> </target> <target name="prepare" unless="prepare.done" depends="clean" description="Prepare for build"> <mkdir dir="${basedir}/build/api"/> <mkdir dir="${basedir}/build/coverage"/> <mkdir dir="${basedir}/build/logs"/> <mkdir dir="${basedir}/build/pdepend"/> <mkdir dir="${basedir}/build/phpdox"/> <property name="prepare.done" value="true"/> </target> <target name="lint" unless="lint.done" description="Perform syntax check of sourcecode files"> <apply executable="php" failonerror="true" taskname="lint"> <arg value="-l"/> <fileset dir="${appdir}"> <include name="**/*.php"/> <modified/> </fileset> <fileset dir="${testdir}"> <include name="**/*.php"/> <modified/> </fileset> </apply> <property name="lint.done" value="true"/> </target> <target name="phploc" unless="phploc.done" description="Measure project size using PHPLOC and print human readable output. Intended for usage on the command line."> <exec executable="${toolsdir}phploc.bat" taskname="phploc"> <arg value="--count-tests"/> <arg path="${appdir}"/> <arg path="${testdir}"/> </exec> <property name="phploc.done" value="true"/> </target> <target name="phploc-ci" unless="phploc.done" depends="prepare" description="Measure project size using PHPLOC and log result in CSV and XML format. Intended for usage within a continuous integration environment."> <exec executable="${toolsdir}phploc.bat" taskname="phploc"> <arg value="--count-tests"/> <arg value="--log-csv"/> <arg path="${basedir}/build/logs/phploc.csv"/> <arg value="--log-xml"/> <arg path="${basedir}/build/logs/phploc.xml"/> <arg path="${appdir}"/> <arg path="${testdir}"/> </exec> <property name="phploc.done" value="true"/> </target> <target name="pdepend" unless="pdepend.done" depends="prepare" description="Calculate software metrics using PHP_Depend and log result in XML format. Intended for usage within a continuous integration environment."> <exec executable="${toolsdir}pdepend.bat" taskname="pdepend"> <arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml"/> <arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg"/> <arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg"/> <arg path="${appdir}"/> </exec> <property name="pdepend.done" value="true"/> </target> <target name="phpmd" unless="phpmd.done" description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing."> <exec executable="${toolsdir}phpmd.bat" taskname="phpmd"> <arg path="${appdir}"/> <arg value="text"/> <arg path="${basedir}/build/phpmd.xml"/> </exec> <property name="phpmd.done" value="true"/> </target> <target name="phpmd-ci" unless="phpmd.done" depends="prepare" description="Perform project mess detection using PHPMD and log result in XML format. Intended for usage within a continuous integration environment."> <exec executable="${toolsdir}phpmd.bat" taskname="phpmd"> <arg path="${appdir}"/> <arg value="xml"/> <arg path="${basedir}/phpmd.xml"/> <arg value="--reportfile"/> <arg path="${basedir}/build/logs/pmd.xml"/> </exec> <property name="phpmd.done" value="true"/> </target> <target name="phpcpd" unless="phpcpd.done" description="Find duplicate code using PHPCPD and print human readable output. Intended for usage on the command line before committing."> <exec executable="${toolsdir}phpcpd.bat" taskname="phpcpd"> <arg path="${appdir}"/> </exec> <property name="phpcpd.done" value="true"/> </target> <target name="phpcpd-ci" unless="phpcpd.done" depends="prepare" description="Find duplicate code using PHPCPD and log result in XML format. Intended for usage within a continuous integration environment."> <exec executable="${toolsdir}phpcpd.bat" taskname="phpcpd"> <arg value="--log-pmd"/> <arg path="${basedir}/build/logs/pmd-cpd.xml"/> <arg path="${appdir}"/> </exec> <property name="phpcpd.done" value="true"/> </target> <target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit"> <exec executable="${toolsdir}phpunit.bat" failonerror="true" taskname="phpunit"> <arg value="--configuration"/> <arg path="${basedir}/phpunit.xml.dist"/> <arg value="--include-path"/> <arg path="${appdir}"/> </exec> <property name="phpunit.done" value="true"/> </target> <target name="phpdox" unless="phpdox.done" depends="phploc-ci,phpmd-ci" description="Generate project documentation using phpDox"> <exec executable="${toolsdir}phpdox.bat" dir="${basedir}" taskname="phpdox"/> <property name="phpdox.done" value="true"/> </target> </project>
@phpunit.xm.dist(相关信息可以查看官方文档)
<?xml version="1.0" encoding="UTF-8"?> <phpunit colors="true" stopOnFailure="false" backupGlobals="false" backupStaticAttributes="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" addUncoveredFilesFromWhitelist="true" syntaxCheck="false" cacheTokens="true" verbose="false"> <!--bootstrap="bootstrap.php"--> <php> <includePath>app/</includePath> </php> <testsuites> <testsuite name="TEST"> <directory>Test/</directory> <exclude>app/</exclude> </testsuite> </testsuites> <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix="Test.php">app/</directory> </whitelist> </filter> <logging> <log type="coverage-clover" target="build/logs/clover.xml"/> <log type="coverage-html" target="build/coverage" title="BankAccount"/> <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/> </logging> </phpunit>
@phpmd.xml(相关规则信息可以查看官方文档)
<?xml version="1.0" encoding="UTF-8"?> <ruleset name="name-of-your-coding-standard" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> <description>Description of your coding standard</description> <rule ref="rulesets/codesize.xml"/> <rule ref="rulesets/cleancode.xml"/> <rule ref="rulesets/unusedcode.xml"/> <rule ref="rulesets/design.xml"/> <rule ref="rulesets/naming.xml"/> <rule ref="rulesets/controversial.xml/Superglobals"/> <!-- ... --> </ruleset>
@phpdox.xml(相关规则信息可以查看官方文档)
<?xml version="1.0" encoding="UTF-8"?> <phpdox xmlns="http://phpdox.net/config"> <project name="name-of-project" source="src" workdir="build/phpdox"> <collector publiconly="false"> <include mask="*.php"/> </collector> <generator output="build"> <build engine="html" enabled="true" output="api"> <file extension="html"/> </build> </generator> </project> </phpdox>
5、jenkins配置
进入jenkins管理插件页面,下载上面相关插件。
启用ant构建
构建后操作步骤添加
解析各种xml报告文件内容
三,启用
效果图如下:
四、结束语
本人表达能力不给力和知识面有限,如有不对之处,请多多指教。
本文主要阐述一些主要配置内容,其他遗漏地方请见谅。
另外,一些工具本人也是刚接触,不熟悉所以不介绍。可以自行参考官方手册(本人已提供几个)
本文主要参考 jenkins php集成官方指导
相关参考文献:
phpunit中文手册
phpDox手册
phpmd手册
pdepend手册
jenkins php集成官方指导