How do you automate Javascript minification for your Java web applications? How do you automate Javascript minification for your Java web applications? javascript javascript

How do you automate Javascript minification for your Java web applications?


We are using Ant task to minify js files with YUICompressor during production build and put result into a separated folder. Then we upload those files to a web server.

Here is an example:

<target name="js.minify" depends="js.preprocess">    <apply executable="java" parallel="false">        <fileset dir="." includes="foo.js, bar.js"/>        <arg line="-jar"/>        <arg path="yuicompressor.jar"/>        <srcfile/>        <arg line="-o"/>        <mapper type="glob" from="*.js" to="*-min.js"/>        <targetfile/>    </apply></target>


I think one of the best and right tool for the job is wro4j Check out https://github.com/wro4j/wro4j

It does everything you need:

  • Keep project web resources (js & css) well organized
  • Merge & minify them at run-time (using a simple filter) or build-time (using maven plugin)
  • Free and open source: Released under an Apache 2.0 license
  • several minification tools supported by wro4j: JsMin, Google Closure compressor, YUI etc
  • Very easy to use. Supports Servlet Filter, Plain Java or Spring Configuration
  • Javascript and CSS Meta Frameworks Support: CoffeeScript, Less, Sass etc
  • Validation: JSLint, CSSLint etc

Can run in debug as well as production modes. Just specify all the files it should handle/pre-process and it does the rest.

You can simply include merged, minified and compressed resource like this:

<script type="text/javascript" src="wro/all.js"></script>