Igor's Blog

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

, ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

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

Recent Blog Posts

Showing whitespace characters in vi

Using Jackson to automatically convert Java POJOs to JSON as output of a Jersey service

Enable quick access to the keyboard and emoji viewer in macOS

How to tell if your site is served via CloudFlare

Googe Cloud Platform doesn't support CloudFlare's free edge certificates

Fixing the invalid private key error in the Google Cloud Platform App Engine app Settings page

Cleaning the sticky PlayStation controller analogue thumb sticks

Invoking interactive shell scripts non-interactively

Using variables as column numbers with Bash and Awk when processing text files

Debugging Google DFP ad delivery via the publisher console

Recent Galleries

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

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again