Minify using an ANT target

Manually minify files is doable when the files are not changed frequently and you do not have many files to minify. If you have JavaScripts that change frequently due to active development or have a large number of files, you might want to consider to automate it. One way to do so is to use ant target for YUI Compressor.

Here is the configuration that I used to setup the minify. You may wish to change directory/file location and file names to match your environment. You can also update the exclude list (i.e. excludes="/lib/**/**/*.js,**/other_special_file.js") for correct files that YUI Compressor can not handle.

    <target name="-pre-dist">
    <echo level="info" message="Compressing JavaScript and CSS files...." />
    <available file="${basedir}/../libs/jars/YUIAnt.jar" property="YUIANT_AVAILABLE" />
    <fail unless="YUIANT_AVAILABLE" message="Run jar target to generate the required task"/>
    <path id="yuicompressor.classpath">
        <fileset dir="${basedir}/../libs/jars">
            <include name="yuicompressor-2.3.6.jar"/>
            <include name="YUIAnt.jar"/>
        </fileset>
    </path>
    <taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
        <classpath>
            <path refid="yuicompressor.classpath"/>
        </classpath>
    </taskdef>
    <yuicompress linebreak="8000" warn="false" munge="no" preserveallsemicolons="true"
         outputfolder="${basedir}/${build.web.dir}" >
         <fileset dir="${basedir}/web" excludes="/lib/**/**/*.js,**/other_special_file.js" >
             <include name="**/*.js" />
             <include name="**/*.css" />
        </fileset>
    </yuicompress>
    <echo level="info" message="Compression Complete" />
    </target>

Leave a comment