Adding JS/CSS compression and JS validation using YUI Compressor, JSLint, and jsmin to ant script
updated script – Minify CSS/JS ant revisited using YUI compressor
——-
I have been expanding Alistair Davidson's wonderful General ANT build and release scripts. I added js and css minifying shown in Building Web Applications With Apache Ant along with javascript validation through jslint.
JSLint4Java "is a java wrapper around the fabulous tool by Douglas Crockford, jslint. It provides a simple interface for detecting potential problems in JavaScript code."
JSMin Ant Task -Â "is a filter which removes comments and unnecessary whitespace from javascript files. It typically reduces filesize by half, resulting in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate self-documentation."
Yahoo! UI Library – The YUI Compressor is JavaScript / CSS minifier designed to be 100% safe and yield a higher compression ratio than most other tools.
All downloads / scripts can be found in the links above.
——
Basically I added three files to his root folder (build_release): jslint4java-1.1+rhino.jar, jsmin.0.0.2.jar, and yuicompressor-2.1.1.jar.
Then added this code to where he defines taskdef in his build.xml:
 <taskdef name="jsmin"
       classname="net.matthaynes.jsmin.JSMin_Task"
       classpath="jsmin.0.2.2.jar"/>
 <taskdef name="jslint"
       classname="net.happygiraffe.jslint.ant.JSLintTask"
      classpath="jslint4java-1.1+rhino.jar" />
Then in the target jarupTempDir, I added:
<!— hack till fileset is added to jslint —>
      <delete>
         <fileset dir="${tempDir}/${jarfileTstamp}">
            <include name = "**/*.js" />
             <date datetime="${startDateTime}" when="before" />
         </fileset>
      </delete>    Â
     Â
      <echo>Run JSLintTask</echo>    Â
      <jslint dir="${tempDir}/${jarfileTstamp}">
      <!—
          <fileset dir="${tempDir}/${jarfileTstamp}" includes="**/*.js">
             <date datetime="${startDateTime}" when="after" />
         </fileset>
      —>
      </jslint>
  Â
      <echo>Now minifying js and css files</echo>    Â
      <apply executable="java" parallel="false">
          <fileset dir="${tempDir}/${jarfileTstamp}" includes="**/*.js, **/*.css">
            <date datetime="${startDateTime}" when="after" />
         </fileset>
          <arg line="-jar"/>
          <arg path="yuicompressor-2.1.1.jar"/>
         <!—
          <mapper type="glob" from="*.js" to="*-min.js"/>
         —>
      </apply>
      <!— if want to use jsmin instead of yuicompressor
      <jsmin>
           <fileset dir="${tempDir}/${jarfileTstamp}" includes="**/*.js">
         <date datetime="${startDateTime}" when="after" />
         </fileset>
      </jsmin>
      —>




Posted under: 


FInally getting around to looking at doing this...
One question - how do you manage these files between development and production? For me it seems like you should have uncompressed files (esp. CSS) in development and compress things when pushed to production - but I'm not sure how to handle that in the code?