Monday, March 31, 2014

Minify javascript and css files with maven

Before you deploy to production, you want to minimize js and css files. This is easy to do with maven.
In addition you can also provide each of these minimized files a unique suffix

In your pom.xml, specify your build timestamp format:

In your build section, set resource filtering to true.

Now you can use the yui compressor maven plug in to compress the files you want:


                    <!-- The regex  is to exclude js and css files that have not been minimized-->
                    <!-- add the time stamp to the end of the war -->

Note that in this case I have excluded some 3rd party (datatables, a table plugin for jquery) js and css files. 

In the maven war plug in, you can now exclude the uncompressed files, so only the compressed files are packaged into the war

By adding the timestamp as a suffix, you ensure that the latest js and css are served by the server. The disadvantage of this is you need to specifically add the timestamp value in your JSP files. This can be done like this:

In your resources file (usually add:


Since you have provided resource filtering with the <resources> tag, the timestamp property from your pom is now available in the JSP via the message.props resource file.

<%@ taglib uri="" prefix="spring" %>
<spring:message var="timestamp" code="build.timestamp"/>
<script type="text/javascript" charset="utf-8" src="/resources/script/yourFile-${timestamp}.js"></script> 

No comments:

Post a Comment