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.)
I think you need a compression library, for example Granule tag.
http://code.google.com/p/granule/
It gzip and combine javascripts wrapped by g:compress tag using different methods, also has Ant task as well
code sample is:
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:
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:
Our project has handled it a number of ways but we have continued to use the YUI Compressor through our different iterations.
We initially had a servlet handle the compression for JavaScript the first time that particular file was accessed; it was then cached. We already had a system in place to handle custom property files so we simply updated our configuration files to support enabling or disabling the compressor depending on the environment we were working in.
Now the development environments never use compressed JavaScript for debugging purposes. Instead we handle the compression in our build process when exporting our application to a WAR file.
Our client has never raised concerns about the compression and the developers don't notice it until they decide to debug JavaScript. So I'd say it's rather transparent with minimal, if any, side affects.
I tried two ways:
Of course the latter solution is better since it does not consume resources at runtime (my webapp is using google app engine) and it doesn't complicate your application code. So assume this latter case in the following answers:
using maven
you activate it only when assemblying the final war; in development mode you see the uncompressed version of your resources
absolutely
YUI compressor
no, it is very complete and easy to use
it is integrated with my favourite tool (maven) and the plugin is in the central repository (a good maven citizen)
I'm writing a framework for managing web assets, called humpty. It aims to be simpler and more modern than jawr or wro4j by using WebJars and ServiceLoaders.
In development, a servlet processes the assets as necessary. The assets would then be pre-compiled before production and placed in a public folder, so that the only part that is used is generating the correct includes in the HTML.
That would be done by switching between development and production modes.
I believe it is transparent, but does strongly favour the use of WebJars.
Whichever one the plugin you put on your classpath uses. Currently looking at writing a plugin for the Google Closure Compiler.
Still pre-release, though I'm using it in production. The maven plugin still needs a lot of work.
The simplicity of just adding a dependency to configure the framework
It's my baby, I love it all ;)
I'm really surprised no one mentioned JAWR - https://j-a-w-r.github.io
It's pretty mature and supports all standard features that are to be expected, and a bit more. Here is how it holds against the OP's excellent criteria.
It originally did the processing/heavy-lifting at application startup and serving was based on a servlet. Starting with 3.x they added support for integrating at build time.
Support for JSP and Facelets is provided through a custom JSP tag library to import processed resources. In addition to that, a JS resources loader is implemented which supports loading the resources from static HTML pages.
A
debug=on
option is available to use before application startup, and a customGET
parameter can be specified at individual requests in production to toggle debug mode selectively at runtime for said request.For JS it supports YUI Compressor and JSMin, for CSS I'm not sure.
SASS
support comes to mind. That said, it does supportLESS
.