How to minify javascript and css files using maven plugin

It is advisable that all the production javascript and css files are minified and most of the javascript libraries do this. This post explains how to minify javascript and css files using maven plugin.I have minified sample javascript and css files using minify-maven-plugin. This sample application uses 1.7.2 version of minify-maven-plugin which requires jdk 7 so it does not work with jdk 6 or prior versions.

In this sample application, I have used embedded jetty web container so minification can be tested from browser as well. Please note that I have tested this sample application on windows with jdk 7 64 bit and maven 3.0.4.

This application contains two sample javascript and two sample css files which will be minified using minify-maven-plugin.


H1 {
	color: white;
	background: teal;
	FONT-FAMILY: arial, helvetica, lucida-sans, sans-serif;
	FONT-SIZE: 18pt;
	FONT-STYLE: normal;
H2 {
	COLOR: #000000;
	FONT-FAMILY: lucida-sans, sans-serif;
	FONT-SIZE: 12pt;
	FONT-STYLE: normal;


body {
	COLOR: #000000;
	FONT-FAMILY: lucida-sans, sans-serif; FONT-SIZE: 18pt;
	FONT-STYLE: normal;
	FONT-VARIANT: normal;

Above two sample css files are placed under the $sample-web-appsrcmainwebappstyles.


function popupFromSample1() {
	alert("Hello World from sample 1");


function popupFromSample2() {
	alert("Hello World from sample 2");

Above two sample javascript files are placed under the $sample-web-app\src\main\webapp\scripts.

I have also created index.jsp page as below to test minified javascripts and css files.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<link rel="stylesheet" type="text/css" href="/styles/minified/sample1.css" />
<link rel="stylesheet" type="text/css" href="/styles/minified/sample2.css" />

<script type="text/javascript" src="/scripts/minified/sample1.js"></script>
<script type="text/javascript" src="/scripts/minified/sample2.js"></script>

		<H1>Header 1!</H1>
		<H2>Header 2!</H2>

		Something in body

		<button type="button" onclick="popupFromSample1()">Try popup from sample1.js</button>
		<button type="button" onclick="popupFromSample2()">Try popup from sample2.js</button>

Refer following pom.xml.

<project xmlns="" xmlns:xsi=""

<name>Sample Maven Web App</name>






In the above pom.xml, minify-maven-plugin is the only plugin required and responsible for minification of javascript and css files. maven-jetty-plugin is used to test this application with jetty web container and maven-war-plugin is used to exclude original javascript and css files from the packaged WAR file.

Take a look at the configuration provided for minify-maven-plugin. I have created minified files in a separate directory (refer <cssTargetDir> and <jsTargetDir> elements) as I don’t want any suffix to be added to minified files. I have specified <nosuffix>true</nosuffix> so name of minified file remains same as source file otherwise it creates *.min.css or *.min.js (i.e. sample1.min.css, sample1.min.css).

I have specified <cssTargetDir> and <jsTargetDir> for resultant minified files otherwise minified files are created in the same directory as source file. Please be informed that <nosuffix> with true value won’t create minified files if target directory is not specified, to avoid file overwriting issue.

In the above sample pom file, I have minified all the javascript and css files available in the directory however you can minify some specific files as described below (replace <cssSourceIncludes> element with the following).



One more thing, note that I have specified true for the <skipMerge>true</skipMerge> which stops the plugin to create one single(one merged file for css and one for javascript) file by combining all the files in one. The default value for skipMerge is false so it creates one style.css for all the css files and one script.js file for all the javascript files. If you do not like these names then you can specify merged file names of your choice using jsFinalFile and cssFinalFile element under plugin configuration.

Now run the mvn clean install –DskipTests from command propmpt, check your generated war file. It should have minified javascript and css files.

Run application in Jetty and test from the browser.

Run mvn jetty:run command from command prompt from project base directory. It deploys war file to embedded web container and which will be accessible at

Now you can check that javascripts and css are minified or not by accessing the following urls. – Returns minified css as below:

H1{color:white;background:teal;FONT-FAMILY:arial,helvetica,lucida-sans,sans-serif;FONT-SIZE:18pt;FONT-STYLE:normal;FONT-VARIANT:normal}H2{COLOR:#000;FONT-FAMILY:lucida-sans,sans-serif;FONT-SIZE:12pt;FONT-STYLE:normal;FONT-VARIANT:normal} – Returns minified css as below:

body{COLOR:#000;FONT-FAMILY:lucida-sans,sans-serif;FONT-SIZE:18pt;FONT-STYLE:normal;FONT-VARIANT:normal;background-color:yellow} – Returns minified javascript as below:

function popupFromSample1(){alert("Hello World from sample 1")}; – Returns minified javascript as below:

function popupFromSample2(){alert("Hello World from sample 2")};

If you have any query on this then feel free to ask me by putting comment below.

Sample application is available on GitHub at