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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

How to make Skype for Business responsive again on macOS

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

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

Blog Activity

Blog Activity