Igor's Blog
Programming, DIY, Games, Hacks, and Tech

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

comments powered by Disqus
Other posts you may like...

Recent Blog Posts

A hack to create an uber jar with the Maven Shade Plugin using local jar files

Is it worth it? Apple USB-C Digital Multi AV adapter vs a cheap eBay clone

Running X11 graphical applications after changing to another user using 'sudo su'

How to stop Facebook using ad images as post sharing thumbnails

Picking lists for Atari Lynx capacitor replacement kits

Performance - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Look and feel - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Using math to work out the diameter of hard to reach water cooling pipes

How to get the unreachable shard in the Hinterlands in DragonAge Inquisition

Measy RC12 wireless 2.4Ghz keyboard touchpad review

Recent Galleries

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Don't forget to
me for more great articles!
Don't show this again