Adding JS/CSS compression and JS validation using YUI Compressor, JSLint, and jsmin to ant script

Ant

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>
-->

Mike Henke

Written by Mike Henke

Mike Henke (pronounced "hang-kee") is a full-stack developer with 25+ years of experience, evolving from ColdFusion to modern frontend frameworks, AI, and cloud technologies. Based in Omaha, he enjoys time with his wife and three children when not coding.