Website deployments: Plain old Ant

In the last entry I was mentioning several solutions for managing web site deployments and packaging for php webapps. Since with PHP there is usually no build process already at development time this has often to be established especially for test & production – and yes there needs to be a build process if you do serious php web development.

Despite Sprocket or similar tools I decided to go for the plain old ANT approach. Why? It was the most practical tool, you have full control of what you are doing and the other tools did not quite what I wanted.

One part of this ant solution was to configure production setting in several source files. I rediscovered just how powerful and really simple this great tool is. Once you get used to is configure,combine,pack and deploy your php/js/html/css is a piece of cake:

Here some excerpts
In order to have just one source file I defined some custom preprocessor variables in my various files:

//@@prod@@ $config['base_dir']="";
/*@@dev@@*/ $config['base_dir']="Foo/";
<!--@@js-dev-start@@ -->
<script type="text/javascript" src="assets/js/jquery.plugin1.js"></script>
<script type='text/javascript' src='assets/js/jquery.plugin2.js'></script>
<script type="text/javascript" src="assets/js/jquery.plugin3.js"></script>
<script type="text/javascript" src="assets/js/jquery.plugin4.js"></script>
<script type="text/javascript" src="assets/js/jquery.plugin5.js"></script>
<!-- @@js-dev-end@@ -->
<!-- @@js-prod-start@@ <script type="text/javascript" src="assets/js/jsbuild.js"></script> @@js-prod-end@@ -->

then in ant this looks as follows:

<echo message="Configuring PHP config for production" />
  <replaceregexp byline="true" flags="m">
            <regexp pattern="^.*@@dev@@.*$"/>
            <substitution expression=""/>
            <fileset dir="${build.dir}" >
                <include name="php/config/*.php"/>
            </fileset>
        </replaceregexp> 
        <replaceregexp byline="true" flags="m">
            <regexp pattern="^.*@@prod@@(.*)$"/>
            <substitution expression="\1"/>
            <fileset dir="${build.dir}" >
                <include name="php/config/*.php"/>
            </fileset>
        </replaceregexp>

And for the javascript in my php – view files

<echo message="Configuring referenced Javascript for production" />
<replaceregexp flags="sg">
            <regexp pattern="@@js-dev-start@@[^\@]*@@js-dev-end@@"/>
            <substitution expression=""/>
            <fileset dir="${build.dir}/php/view" >
                <include name="**/*.php"/>
            </fileset>
        </replaceregexp>
        <replaceregexp byline="true"  flags="m">
            <regexp pattern="^.*@@js-prod-start@@(.*)@@js-prod-end@@.*$"/>
            <substitution expression="\1"/>
            <fileset dir="${build.dir}/php/view" >
                <include name="**/*.php"/>
            </fileset>
        </replaceregexp>

Applying the concat task for js and css concatenation and apply java to invoke the yuicompressor and you’re done.

This entry was posted in javascript, php, Web2.0 and tagged , , . Bookmark the permalink. Both comments and trackbacks are currently closed.