Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

If you know anything about JavaScript and haven't been living under a rock, you will know about minification. There are tools like Google's Closure Compiler and Yahoo's YUI Compressor available to make minified versions of your JavaScript and CSS files. Using those tools is easy but what's even easier is having a Maven wrapper around them. This is where the Minify Maven Plugin comes in.

I'm using this plugin for my jQuery Photoboxr plugin and it works a treat! I did find it's documentation/examples a little limited so wanted to share the configuration that I came up with.

Below is the directory and file structure for my project...
 Directory Structure
{$basedir}
|
+--src
| +--css
| +--jquery-ui-photoboxr.css
| +--js
| +--jquery-ui-photoboxr.js
| ...
+--pom.xml


The above structure differs from what the plugin expects. After digging through documentation I found that the default src directory expected is ${basedir}/src/main/webapp which I think is not ideal. Luckily it's easy enough to make the plugin look elsewhere.

I'm going to leave out all the boilerplate POM file content and will just include what's necessary for the Minify plugin. The full pom.xml file is available on Github here.

So lets see the configuration for the plugin...
 Plugin configuration
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<goals>
<goal>minify</goal>
</goals>
<configuration>
<webappSourceDir>${basedir}/src</webappSourceDir>
<cssSourceDir>css</cssSourceDir>
<cssSourceFiles>
<cssSourceFile>jquery-ui-photoboxr.css</cssSourceFile>
</cssSourceFiles>
<cssFinalFile>jquery-ui-photoboxr.css</cssFinalFile>
<jsSourceDir>js</jsSourceDir>
<jsSourceFiles>
<jsSourceFile>jquery-ui-photoboxr.js</jsSourceFile>
</jsSourceFiles>
<jsFinalFile>jquery-ui-photoboxr.js</jsFinalFile>
<cssEngine>YUI</cssEngine>
<jsEngine>CLOSURE</jsEngine>
<closureCompilationLevel>SIMPLE_OPTIMIZATIONS</closureCompilationLevel>
</configuration>
</execution>
</executions>
</plugin>




I bind the plugin to the package phase (line 8). The source directory is set to ${basedir}/src (line 14). On lines 16-20 I specify my source CSS file and the name of minified CSS file - the default is style.css. Similarly on lines 22 to 26 I specify the source and output JavaScript files. Finally the last parts of my configuration specify which minification engines should be used.

I found that Closure Compiler produces smaller JavaScript files than YUI.

To run the plugin, it's as simple as...
 Maven Command
mvn clean package


This will generate minified versions of the CSS and JavaScript files in the 'target' directory.

Enjoy!

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.