tag ‍“phing”

how to use phing to build php projects

by Hamid Reza Fahimi Madjd @ Jun 25, 2011

Some days ago I found a solution to build php project that called Phing.
Phing is a build framework based on Apapche Ant. If you are familiar with Ant and its role in Java *E projects, you can learn Phing very fast.
Like Ant, there is a (or more) build file named build.xml (default name) to define operations that you like occur on your project.

Now i'm going to describe it by a practical example:

Consider a php project with following structure:

Project
|_src
|__package1
|___class1.php
|___class2.php
|__package2
|__package3
|_static
|_templates
.htaccess
build.xml
config.php
index.php

Scenario:
I want "build.xml" do following operations:

  1. export project from subversion repository and copy it to a temp directory
  2. make an archive file and name it based on latest version number
  3. copy archive file to a remote server over ssh (scp)

I made build.xml file put following code in it:

<?xml version="1.0" encoding="UTF-8"?>
<!-- define project name and default target -->

<project name="my-project" default="dist">

<!-- define some properties (variables) -->
  <property name="path.dive.root" value="/home/hamid/temp" />
  <property name="path.dive" value="${path.dive}/${phing.project.name}" />

<!-- empty related temp directory (delete old one and create new one) -->
  <target name="prepare">
    <echo msg="delete and create ${path.dive} directory" />
    <delete dir="${path.dive}" includeemptydirs="true" verbose="true" failonerror="true" />
    <mkdir dir="${path.dive}" />
  </target>

  <target name="build" depends="prepare">

<!-- export project from reposiroey and copy to temp directory -->
    <svnexport
    svnpath="/usr/bin/svn"
    username="username" password="password"
    force="true" nocache="true"
    repositoryurl="svn://localhost/${phing.project.name}/"
    todir="${path.dive}"/>

<!-- get latest version number and save in svn.lastrevision property -->
    <svnlastrevision
    svnpath="/usr/bin/svn"
    workingcopy="${path.source}"
    repositoryurl="svn://localhost/${phing.project.name}"
    propertyname="svn.lastrevision"/>

<!-- generate archive file name -->
   <property name="archive.filename" value="${phing.project.name}-v${svn.lastrevision}.tar.gz" />
  </target>

  <target name="dist" depends="build">

<!-- archive directory and save it -->
    <tar destfile="${path.dive.root}/${archive.filename}" basedir="${path.dive}" compression="gzip"/>

<!-- copy archive file and copy to sources foler on remote server -->
    <scp username="username" password="password"
    host="host"
    todir="sources/"
    file="${path.dive.root}/${archive.filename}" />
  </target>
</project>

after that go to path that build file is there and type:

$ phing

cause build file name is build.xml, it's not necessary to specify it.
Also if you use PhpStorm, you can add build.xml to Phing Build tool window and deal with that inside the IDE.
Now, I did build process just in 10 Seconds instead of 5 Minutes.

I hope this example encourage you to use Phing.
You can get more detail information from its website.

phing, php comments no comment

last tweet