Thoughts on software and people.

Maven and YUICompressor


Here's how I use YUICompressor to minify/obfuscate our Javascript during Maven's compile phase.

First, add YUICompressor to your pom as dependency:

Once YUICompressor is listed as a dependency we can use the ant-run plugin to invoke it during the build:

What we're doing above is hooking the ant-run plugin to the compile phase, creating an ant property that contains the path to yuicompressor.jar, and then invoking a task in an Ant build file called yuicompress.xml. Make sure you specify version 1.3 of the ant-run plugin (or newer) - older versions didn't make the Maven dependency paths available to Ant. The ant file looks like this:

In my case I'm compressing Freemarker templates that contain JS, which is why I specified the type as JS. These are in a subdirectory of my resources directory called 'templates.' I have YUICompressor output the compressed version to the corresponding location in the build directory with a slightly modified name so they get packaged with my jar file and are available (along with the uncompressed versions) at runtime.

One thing to note is that the version of YUICompressor available on the Maven repo is slightly outdated. You may want to install the latest jar in your local repo and use that instead.

Thanks to Julien Lecomte and Yahoo for making YUICompressor freely available.

comments powered by Disqus