Building frontend code with Ant

Recently I’ve been checking various ways for optimising frontend code. In Professional JavaScript for Web Developers (2nd Edition), Nicholas C. Zakas speaks about best practices for deploying websites in production (among other good things). Having a solid build process for deployment saves both time and bandwidth, and helps automating repetitive tasks. There are plenty of online resources about the subject, but Julien Lecomte’s Building Web Applications With Apache Ant explains really well what possibilities Ant offers for frontend code build process (see also Setting up a JavaScript Build Process). For more details, see Apache Ant Manual.

Based on the advice from those articles, I’ve put together a basic Ant script which concatenates and minifies both JS and CSS files during the build process. This could be used as a base template for frontend production code.

On this site, I use Minify PHP5 app on the server side to automatically combine and minify multiple CSS and Javascript files every time they change. However, I wanted to define a structured build process that I can customise and reuse for any website I work on.

Here’s what the script does (for details, see comments in the code):

  1. Delete the directory created by earlier build
  2. Recreate the directory structure to be used in new build, create timestamp
  3. Concatenate specified files using Ant concat task
  4. Minify the file using YUI Compressor (called from the Ant script using apply task) and add ‘-min’ to the file name.

You need JDK, Apache Ant and YUI Compressor to run this. Now, when I set this up on Windows, I got the following error:

Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib\tools.jar

If you get that too, just copy tools.jar from earlier Java version and you’re done. When everything is set up, go to Ant dir (where the build.xml file lives):

cd C:\Ant

Then build the project (by default, it uses build.xml):


Here’s the build.xml I’ve created:

<?xml version="1.0" encoding="utf-8"?>
<project name="myProject" default="prod" basedir=".">
    <description>JS/CSS production build for myProject</description>
    <target name=""
        description="Sets global properties for this build">
        <!--YUI Compressor location-->
        <property name="yui.dir" value="C:/yui/build/yuicompressor-2.4.2.jar"/>
        <!--Source JS dir-->
        <property name="src.js.dir" value="C:/dev/src/myProject/js"/>
        <!--Source CSS dir-->
        <property name="src.css.dir" value="C:/dev/src/myProject/css"/>
        <!--Output dir-->
        <property name="build.dir" value="build"/>

    <!--Create build dirs-->
    <target name="-init" depends=""
        description="Creates build directory structure">
        <!--Create the time stamp-->
            <format property="TODAY" pattern="EEE, d MMM yyyy HH:mm:ss Z"/>
        <!--Delete previous build files-->
        <delete dir="${build.dir}"/>
        <!--Recreate build dirs-->
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.dir}/js"/>
        <mkdir dir="${build.dir}/css"/>
        <!--Write build time stamp into a file-->
        <echo file="${build.dir}/js/tstamp.txt" append="false">Build Date: ${TODAY}</echo>
        <echo file="${build.dir}/css/tstamp.txt" append="false">Build Date: ${TODAY}</echo>

    <!--Concatenate JS files-->
    <target name="-js.concatenate" depends="-init" 
        description="Concatenates specified JavaScript files">
        <concat destfile="${build.dir}/js/concat.js">
            <!--List files in correct order in the concatenated file-->
                files="first.js, second.js"/>

    <!--Concatenate CSS files-->
    <target name="-css.concatenate" depends="-init" 
        description="Concatenates specified CSS files">
        <concat destfile="${build.dir}/css/concat.css">
            <!--List files in correct order in the concatenated file-->
                files="first.css, second.css"/>

    <!--Minify JS files-->
    <target name="-js.minify" depends="-js.concatenate" 
        description="Minifies JavaScript files">
        <apply executable="java" parallel="false">
            <arg line="-jar"/>
            <arg path="${yui.dir}"/>
            <arg line="-o"/>
            <mapper type="glob" from="*.js" to="${build.dir}/js/*-min.js"/>

    <!--Minify CSS files-->
    <target name="-css.minify" depends="-css.concatenate" 
        description="Minifies CSS files">
        <apply executable="java" parallel="false">
            <arg line="-jar"/>
            <arg path="${yui.dir}"/>
            <arg line="-o"/>
            <mapper type="glob" from="*.css" to="${build.dir}/css/*-min.css"/>

    <target name="prod" 
        description="Builds files for production"

Obviously, there are lots of other things you can do with Ant. There’s really nothing new in this script, but it combines ideas from several articles mentioned above so I thought I’d share it here (download build.xml from GitHub). I hope it can be useful for you – I know for me it will! ;)

Update 28 March, 2011: uploaded this build.xml file to GitHub.