Wednesday, September 19, 2012

How to minify JS and CSS files using Maven

Motivation: Minification of JS and CSS is a common best practice when developing web application. It's done to save bandwidth and keep single unified JS and CSS file, which once loaded will be used for subsequent requests.

Prerequisites: Introductory knowledge of Maven.

Steps:  I will use Samaxes minifier plugin. Read details about this plugin here. I will follow an example. Assuming your directory structure is similar to this
 |- pom.xml
 `- src
      `-- main
            |-- java
            |     `-- [more dirs ommitted]
            |-- resources
            `-- webapp
                  |-- META-INF
                  |-- static
                  |       |-- css
                  |       |     |-- style1
                  |       |     |      |-- one.css
                  |       |     |      `-- two.css
                  |       |     |-- style2
                  |       |            `-- three.css
                  |       `-- js
                  |            `---  js1
                  |                   |-- a.js
                  |                   `-- b.js
                  `-- WEB-INF

In your build tag you need to add, plugin tag under  the build tag. For the above structure it looks like this:


        <!-- more execution statements -->
Here is brief explanation:
  1. Line #10, is relative path from webapp directory
  2. Line #12, 13, 14 are file paths with respect to cssSourceDir
  3. Line #16, is the final directory wrt webapp where the minified file goes.
  4. Line #17, is file name that minified (and concatenated) files will have. Minified file will end with .min.css or .min.js


  1. This comment has been removed by the author.

    1. Great Article IoT Projects for Students

      Deep Learning Projects for Final Year

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. i find a free online service to compress js and minify css, so it will reduce the size of web page.

  3. There is a lot of online tools to minify, but this method it's better because every change you make in your source files, it will be minified automatically by maven, and you don't need to copy and paste in your online service.
    If your source files are finalized, you can use an online service and plublish it, but if you are developing and changing your files many times, use Maven plugin.


  4. I support the opinion of Daniel. While your solution is good but Maven for the job will be more appropriate
    Richard Brown dataroom software

  5. And you still do not know, with a casino you always win gorgeous gamblers bonus so run here as soon as possibl