Thursday, October 18, 2007

YUI Compressor in ANT

Here is a sample ANT target to batch minify all JavaScript files using YUI Compressor:

<target name="js.minify" depends="init">
<description>minify js files</description>
<apply executable="java" parallel="false" failonerror="true">
<arg value="-jar" />
<arg file="${tools.dir}/${yuicompressor}" />
<arg value="--nomunge" />
<!-- line break set at 20 characters, so that we can get many line breaks, this
is good for debugging in IE-->
<arg value="--line-break" />
<arg value="20" />
<arg value="--preserve-strings" />
<arg value="--preserve-semi" />
<arg value="--charset" />
<arg value="ISO-8859-1" />
<arg value="-o" />
<targetfile />
<srcfile />
<!-- files are processed in alpha-order so that we can know the progress,
and easily identify the file that is failed.-->
<sort xmlns:rcmp="">
<rcmp:name />
<fileset dir="${src.dir}">
<include name="**/*.js" />
<exclude name="**/YUI/**" />
<!-- avoid cycling -->
<exclude name="**/*-min.js" />
<!-- define output file name -->
<mapper type="glob" from="*.js" to="*-min.js" />

About YUI Compressor: it is a tool to minimize JavaScript and CSS files. I have used it for more 6 months on a public web site without any problem.

About ANT:
I have been using APACHE ANT for 7 years, since 2001. To me, an ANT build file structure is more intuitive and scalable than MAKE files.
Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.

This is the post that gets me started on this direction:
Building Web Applications With Apache Ant