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

I'm interested in hearing how you prefer to automate Javascript minification for your Java web apps. Here are a few aspects I'm particularly interested in:

  • How does it integrate? Is it part of your build tool, a servlet filter, a standalone program post-processing the WAR file, or something else?
  • Is it easy to enable and disable ? It's very unfunny to try and debug a minified script, but it's also useful for a developer to be able to test that the minification doesn't break anything.
  • Does it work transparently , or does it have any side effects (apart from the ones inherent in minification) that I have to consider in my day-to-day work?
  • Which minifier does it use?
  • Does it lack any features that you can think of?
  • What do you like about it?
  • What don't you like about it?
  • This will mostly serve as a reference for my future projects (and hopefully other SOer's will find it informative, too), so all kinds of tools are interesting.

    (Note that this is not a question about which minifier is best . We have plenty of those around already.)


    Round-up post

    If you post something new in this thread, edit this post to link to yours.

  • Ant apply task (using YUI Compressor)
  • Custom YUI Compressor Ant task
  • Maven YUI Compressor plugin
  • Granule (for JSP, JSF, Grails, Ant)
  • Ant macros for Google Closure compiler
  • wro4j (Maven, servlet filters, plain Java, etc)
  • ant-yui-compressor (ant task for compressing JS+CSS)
  • JAWR
  • Minify Maven Plugin
  • humpty

  • 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. You can find some good examples for YUI+Ant integration in this blog.

    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>
    
    链接地址: http://www.djcxy.com/p/45700.html

    上一篇: 使用Express从NodeJS Server下载文件

    下一篇: 你如何自动化你的Java Web应用程序的JavaScript缩小?