Ant Task to compress JavaScript and CSS using YUICompressor

In the Ant task for combining multiple JavaScript files into one and minifying them using Dojo Shrinksafe entry i built a sample ant task that can be used to combine and minify JavaScript using Dojo ShrinkSafe.

But what if you want to use the YUI Compressor, or you want to combine and minify css, so i built this task for that


<target name="minify" >
<antcall target="minifyjs" />
<antcall target="minifycss" />
</target>
<target name="minifyjs" >
<mkdir dir="build/WebContent/js" />
<concat destfile="build/WebContent/js/combined.js">
<fileset dir="WebContent/js" includes="*.js" />
</concat>
<java fork="true" jar="${yuicompressor.lib.dir}" dir="build/WebContent/js"
output="build/WebContent/js/combined.min.js">
<arg value="combined.js" />
</java>
</target>

<target name="minifycss" >
<mkdir dir="build/WebContent/css" />
<concat destfile="build/WebContent/css/combined.css">
<fileset dir="WebContent/css" includes="*.css" />
</concat>
<java fork="true" jar="${yuicompressor.lib.dir}" dir="build/WebContent/css"
output="build/WebContent/css/combined.min.css">
<arg value="combined.css" />
</java>
</target>



The minify task is broken into two parts one is minifyjs which combines and minfies all the JavaScript files in /js folder and the minifycss which combines and minifies all the css files in /css folder.

2 comments:

DomFilk said...

I found a free online service using the YUICompressor to compress the javascript, if someone don't want to code yourself, you can have a try.

Abhi said...

Thanks for info
Web Design Company in Bangalore
Website development in Bangalore