thecrumb

developer | thinker | tinkerer

Easy Maintenance Mode with Ant


  • Thursday, March 10 2011
  • ant, code

I usually have something in my application where I can flip a bit and turn on "maintenance mode" which displays a friendly message to users and allows me to do updates knowing no one is accessing the application.

Today we'll look at how you can implement this using Ant.

First we'll update our index.cfm file and add some code to check a value and show our friendly message:

<cfset maintenanceMode = false>
<cfif maintenanceMode IS true>
    <h2>Site Maintenance</h2>
    <p>The application is currently unavailable. Please check back in a few minutes (or hours if we really screw something up).</p>
    <p>We apologize for the inconvenience.</p>
    <cfabort>
</cfif>

So now we need to flip the bit on maintenanceMode.

You could access the server, open the file with an editor and hope you don't screw something up while editing the file... or you could do it from the comfort of your IDE with Ant. We will create a macrodef so we can use the same code to turn on/off maintenance mode in all our environments.

The macrodef code:

<macrodef name="maintenance-mode" description="Enable or disable maintenance mode">
    <attribute name="mode" default="false"/>
    <attribute name="dest"/>
    <sequential>
        <echo>
            Setting mainenance mode to: @{mode} on ${path.@{dest}}
        </echo>
        <replaceregexp file="${path.@{dest}}/www/index.cfm" match="maintenanceMode = (\w+)" replace="maintenanceMode = @{mode}"/>
    </sequential>
</macrodef>

First we have defined some attributes we'll pass in:

  • mode = true or false
  • dest = destination - where the file lives we need to modify

We will add an echo in there so we can see what's going on when we run things, and finally we'll use the ReplaceRegexp task to search for our string. We simply need to look for "maintenanceMode = whatever" and that's easy to do with a regular expression. Then we simply update that string with the value we've passed in as 'mode'.

Tip: If you have problems with Regular Expressions check out the RegEx Tester plugin for Eclipse.

Finally we will create two new targets to turn things on and off:

<target name="DEVELOPMENT-MAINTENANCE-ON">
    <maintenance-mode mode="true" dest="development"/>
</target>

<target name="DEVELOPMENT-MAINTENANCE-OFF">
    <maintenance-mode mode="false" dest="development"/>
</target>

Here we define our 'mode' and 'destination' attributes. The 'development' destination is simply a property I have mapped in Ant which points to my web files. Depending on how you access your files you may have to just through more hoops here.

path.development = \\\\serverName\\Inetpub\\wwwroot\\www\\${ant.project.name}

Using a combination of properties and attributes: ${path.@{dest} this will give us the proper path to the file we need to change.

You can easily create additional targets for production, etc.

And finally running this in Eclipse we can see:

Buildfile: D:\workspace\_build\build.xml
DEVELOPMENT-MAINTENANCE-OFF:
     [echo] 
     [echo] Setting mainenance mode to: false on \\serverName\Inetpub\wwwroot\www\myproject
     [echo]             
BUILD SUCCESSFUL
Total time: 1 second

For more on Ant check out my Ant wiki.


ValidateThis – Day 2: Setting Up The Form One Year Ago Today

More like this...