Articles :: CakePHP :: Using ANT with CakePHP

written by Toby Miller on March 29, 2006
March 29, 2006

What's the first thing you should do whenever sitting down to do some Cake coding? Is it get a tall glass of Guinness and scratch your manhood? Absolutely! Though you also always want to download the latest version of Cake from Subversion. Otherwise you're gonna get that same old first reply in IRC ... "Are you running the latest version?".

Now what's something that's still missing from CakePHP? That's right ... a simple way to upgrade your web application to the latest version. Then there's creating backups, creating builds, switching from development to production and the list goes on and on. This can get old and I totally understand how futile an actual upgrade utility would be this early in the game for CakePHP so I decided to see what I could do with ANT.

I've used ANT on a few Java projects and it seemed like a fine tool but I never bothered to dive into the docs and see just how much I could do with it. Well, apparently you can do quite a bit. Here's the scripts that I've been using and they seem to be working really well.

build.properties - This has all of your website settings
   1:##
   2:## build.properties ~ used by build.xml
   3:##
   4:## These build properties are unique to a website project which
   5:## implements the cakephp framework.
   6:##
   7:## Written by: Toby Miller
   8:##
   9:
  10:## project name; usually the website account name
  11:ant.project.name = tobymiller
  12:
  13:## project base directory; usually the website home directory
  14:basedir = .
  15:
  16:## project dir; path to website wwwroot (will be overwritten)
  17:project.wwwroot = htdocs
  18:
  19:## temp dir; path to a temp folder (used for the sole purpose of this build)
  20:project.temp = temp
  21:
  22:## backup dir; location of backups (kept for emergency restorations)
  23:project.backup = backups
  24:
  25:## build dir; location of builds (holding location until pushed into production)
  26:project.build = builds
  27:
  28:## cakephp dir; path to fresh cakephp svn checkout
  29:project.cakephp = /home/tmiller/projects/cakephp/cake

build.xml - This is the guts of any ant build
   1:<?xml version="1.0"?>
   2:<project default="init">
   3:    <!--
   4:        get properties:
   5:        get necessary properties from the "build.properties" file which should
   6:        be located in the same directory as this script
   7:    -->
   8:    <property file="build.properties"/>
   9:
  10:    <!--
  11:        init:
  12:        all actions rely on this action, it creates the timestamp which is used
  13:        to name the temporary working directory and backup files generated by
  14:        this script
  15:    -->
  16:    <target name="init">
  17:        <tstamp>
  18:            <format property="TIMESTAMP" pattern="yyyyMMdd-hhmm"/>
  19:        </tstamp>
  20:    </target>
  21:
  22:    <!--
  23:        update:
  24:        update this website (local version) with the latest build of cakephp
  25:        (this requires you to checkout the latest version of cakephp from svn)
  26:    -->
  27:    <target name="update" depends="init">
  28:        <sequential>
  29:            <antcall target="create-temp"/>
  30:            <antcall target="get-cakephp"/>
  31:            <antcall target="get-webapp"/>
  32:            <antcall target="backup-website"/>
  33:            <antcall target="sync-website"/>
  34:            <antcall target="cleanup-temp"/>
  35:        </sequential>
  36:    </target>
  37:
  38:    <!--
  39:        build:
  40:        build the latest version of this website
  41:    -->
  42:    <target name="build" depends="init">
  43:        <sequential>
  44:            <antcall target="create-temp"/>
  45:            <antcall target="get-website"/>
  46:            <antcall target="clean-build"/>
  47:            <antcall target="create-build"/>
  48:            <antcall target="cleanup-temp"/>
  49:        </sequential>
  50:    </target>
  51:
  52:    <!--
  53:        backup:
  54:        backup the current local website
  55:    -->
  56:    <target name="backup" depends="init">
  57:        <antcall target="backup-website"/>
  58:    </target>
  59:
  60:    <!--
  61:        create temp:
  62:        create a temporary working directory to be used by this script
  63:    -->
  64:    <target name="create-temp">
  65:        <mkdir dir="${basedir}/${project.temp}/${TIMESTAMP}"/>
  66:    </target>
  67:
  68:    <!--
  69:        get cakephp:
  70:        prepare the temporary working directory with the latest build of cakephp
  71:        (this requires you to checkout the latest version of cakephp from svn)
  72:    -->
  73:    <target name="get-cakephp">
  74:        <copy todir="${basedir}/${project.temp}/${TIMESTAMP}">
  75:            <fileset dir="${project.cakephp}">
  76:                <exclude name="**/.svn/**"/>
  77:                <exclude name="**/.svn/**"/>
  78:            </fileset>
  79:        </copy>
  80:    </target>
  81:
  82:    <!--
  83:        get website:
  84:        prepare the temporary working directory with the latest copy of this
  85:        website so that this script can manipulate it without affecting this
  86:        website, this is used for purposes like creating production builds
  87:    -->
  88:    <target name="get-website">
  89:        <copy todir="${basedir}/${project.temp}/${TIMESTAMP}">
  90:            <fileset dir="${project.wwwroot}">
  91:                <exclude name="**/.svn/**"/>
  92:                <exclude name="**/.svn/**"/>
  93:            </fileset>
  94:        </copy>
  95:    </target>
  96:
  97:    <!--
  98:        clean build:
  99:        clean the temporary working directory for a production launch by
 100:        updating development settings, removing wip files, removing temp/cache
 101:        files, and so on ... keep builds as clean as possible
 102:    -->
 103:    <target name="clean-build">
 104:        <!-- delete temp/cache files -->
 105:        <delete includeemptydirs="false">
 106:            <fileset dir="${basedir}/${project.temp}/${TIMESTAMP}/app/tmp" includes="**/*"/>
 107:        </delete>
 108:
 109:        <!-- switch to the production database -->
 110:        <replace file="${basedir}/${project.temp}/${TIMESTAMP}/app/app_model.php" token="var $useDbConfig = 'development';" value="var $useDbConfig = 'production';"/>
 111:
 112:        <!-- turn debugging off -->
 113:        <replaceregexp file="${basedir}/${project.temp}/${TIMESTAMP}/app/config/core.php" match="define('DEBUG', [0-9]+);" replace="define('DEBUG', 0);"/>
 114:    </target>
 115:
 116:    <!--
 117:        create build:
 118:        create the build file (tgz) and store it in the builds directory
 119:    -->
 120:    <target name="create-build">
 121:        <tar destfile="${basedir}/${project.build}/${ant.project.name}_build_${TIMESTAMP}.tgz" basedir="${basedir}/${project.temp}/${TIMESTAMP}" compression="gzip"/>
 122:    </target>
 123:
 124:    <!--
 125:        get webapp:
 126:        after the latest cakephp build has been downloaded into the temporary
 127:        working directory overwrite the generic webapp provided by cakephp with
 128:        the custom webapp from this website
 129:        (also don't forget your custom .htaccess files)
 130:    -->
 131:    <target name="get-webapp">
 132:        <copy todir="${basedir}/${project.temp}/${TIMESTAMP}/app" overwrite="true">
 133:            <fileset dir="${basedir}/${project.wwwroot}/app">
 134:                <exclude name="index.php"/>
 135:                <exclude name="webroot/.htaccess"/>
 136:                <exclude name="webroot/css.php"/>
 137:                <exclude name="webroot/index.php"/>
 138:                <exclude name="webroot/js/vendors.php"/>
 139:            </fileset>
 140:        </copy>
 141:        <copy todir="${basedir}/${project.temp}/${TIMESTAMP}" overwrite="true">
 142:            <fileset dir="${basedir}/${project.wwwroot}">
 143:                <include name=".htaccess"/>
 144:            </fileset>
 145:        </copy>
 146:    </target>
 147:
 148:    <!--
 149:        backup website:
 150:        create a full backup copy of this website elsewhere on the hard drive
 151:        just in case an error occurs and this scripts actions need to be undone
 152:    -->
 153:    <target name="backup-website">
 154:        <tar destfile="${basedir}/${project.backup}/${ant.project.name}_backup_${TIMESTAMP}.tgz" basedir="${basedir}/${project.wwwroot}" compression="gzip"/>
 155:    </target>
 156:
 157:    <!--
 158:        sync website:
 159:        synchronize updated files from the cakephp build to the website
 160:    -->
 161:    <target name="sync-website">
 162:        <sync todir="${basedir}/${project.wwwroot}" overwrite="yes" includeEmptyDirs="true">
 163:            <fileset dir="${basedir}/${project.temp}/${TIMESTAMP}"/>
 164:        </sync>
 165:    </target>
 166:
 167:    <!--
 168:        cleanup temp:
 169:        delete temporary working files and folders that were used by this script
 170:    -->
 171:    <target name="cleanup-temp">
 172:        <delete dir="${basedir}/${project.temp}/${TIMESTAMP}"/>
 173:    </target>
 174:</project>

The commands this build script promote are:

ant update (updates local website with latest version of cakephp)
ant backup (creates a backup of your local website)
ant build (creates a production ready build from your local website)

I hope some of you find this useful.

permalink                                                                                                                                                                          
   Natural Living (5)
      Heating & Cooling (1)
      Herbal Remedies (1)
   Personal (0)
      Family (1)
      Humor (11)
      Miscellaneous (1)
      Politics (5)
   Technology (2)
      System Administration (4)
            Linux (1)
            Solaris (0)
      Web Development (2)
            CSS (3)
            Design (1)
            Flash (1)
            JavaScript (11)
            PHP (1)
                        CakePHP (1)
            Web Browsers (2)
                        Firefox (1)
                        Internet Exploder (0)
                        Netscape (1)
printed @ tobymiller.com
(currently rendering CSS for Internet Explorer)(currently rendering CSS for non-Internet Explorer browsers)