YUI Compressor Ant Task

I was working on a project recently that contained a number of JavaScript files. As part of the build process, I wanted to minimise/compress all of the JavaScript files. The first thing that came to mind was to use YUI Compressor.

I was using Ant at the time so I needed to create a target to compress the JavaScript files and then output them to wherever they needed to be outputted to. Problem: By default, YUI Compressor can only handle one file at a time; you can’t pass to it a list of files to process. Damn! Anyway, to cut a long story short, I created a custom Ant task to do that for me. Hoorah!

I’ve set-up a repository on Google Code where you can download the code that generates the custom Ant task. Yes, I know it’s fashionable to create repositories on GitHub but I mostly use Subversion and Mercurial so there!

Alternatively you can check out the project directly by issuing the following command:

hg clone https://yui-compressor-ant-task.googlecode.com/hg/ yui-compressor-ant-task

Technically, it is possible to get the desired behaviour using Ant’s apply task; see the post here and the example under the heading, “Minify your JavaScript and CSS files”. I must admit, I only came across this after I had already developed my version; however, there are still reasons to use it. YUI Compressor supports a number of different options and to include a sub-set of these using apply would start to get ugly and (possibly) cumbersome. My Ant task is a bit neater. Well, I think so anyway.

7 thoughts on “YUI Compressor Ant Task

  1. Thomas

     Hey, thanks for a wonderfully simple implementation 🙂 Implementing Ant tasks doesn’t seem that hard once you get down to it. I’ve even managed to convince the task to use the latest yuicompressor-2.4.8pre.jar (by setting the classpath in taskdef).

    I did run into a some problems, which I decided to try and fix (instead of abandoning ship), so I cloned your project. I’ve addressed the following issues:

     – Setting verbose=”true” would cause a NullPointerExcepction, because the ErrorReporter passed in was null
     – Added failonerror to allow indication of whether or not the build should be considered failed if compressing fails. The default value is set to true, but I’m a little unsure as to what the “standard” default for this option is.
     – And finally; since I foresee that we (in our project) want to minimise our CSS in some future, I’ve added in a ‘type’ attribute that accepts the values ‘js’, ‘css’ or ‘auto’. The first two are the types supported by YUI, the last I’ve added, because I can imagine that some people would maybe like to just gather all .js and .css files and have them minimised.

    Would you be interested in any of these changes? Or, do you think someone elso would?

    Thanks again!

  2. Simon Buckle

    Hi Thomas,

    Thanks for your feedback. 

    I’ve fixed the null pointer exception. Not sure about minimising the CSS; when I created the project originally I only had a need to compress the JavaScript. I’ll have a think about it.

  3. Parambir Singh

    Hi Simon,

    I came across your ant library during one of my recent projects and found it to be almost what I wanted. However, I also required CSS minification and an option to delete original source files after minification (to do away with additional ant delete tasks in build.xml). So, I’ve modified the source of your library to add these features and hosted it on GitHub. I’ve posted the details in a blog post here:


    I’ve taken care about the copyrights/license etc. In case you find something amiss, please let me know so that I can correct it.


  4. Will Darby

    Great utility – definitely helps using yui-compressor with Ant.

    As an FYI – I was able to run it with the yuicompressor jar in the same classpathref, rather than using invoking ant with “-lib”.

Leave a Reply