Gradle Tasks for JavaScript and CSS Minification and Combination using ThreadPoolExecutor for Parallel Execution

This is my first post for this blog. And, I am going to start with a posting a task I created for Gradle, for minification and combination of JS and CSS, that too using a ThreadPoolExecutor for faster minification and combination
. Following is an explanation of each section, and finally there will the complete code that you would have to add to your Gradle Build Script. So, lets go!

First, we include the important stuff to start :-

Now, we will write a snippet, which will represent all JS files in a code base :-

This will find all JavaScript files, recursively in all underlying folders. Let’s write something similar for CSS, shall we ? :-

Easy ? Good!

The idea for minification and combination, is that, we need to create a task for each file for minification, and each section for combination. That is, we create tasks dynamically, as follows :-

All tasks here will be created with a name jsMinifyTask_{index}. Again, one task per file for minification.

Something similar for CSS :-

This code will create tasks for minification of CSS files, one at a time.

For combination, I wrote a small JSON structure to make it easy for maintenance. The JSON is :-

This is pretty self explanatory, I guess ? Add up more to make more combined files.

The code which uses this to create tasks for combining files is :-

So now, we are done with creating tasks, and it is time to make them run! I created a wrapper task for this. This will minify all files parallely, and then combine them, parallely. Makes it faster 😉

Runtime.runtime.availableProcessors() will find all available processors and use them.

All that is required now is, writing a simple ‘dependsOn minify’ in the tasks you already have in your build scripts. Voila!

For reference, the complete code is :-

Let me know in comments in case you have problems 🙂

15 thoughts on “Gradle Tasks for JavaScript and CSS Minification and Combination using ThreadPoolExecutor for Parallel Execution”

  1. Hi there! This post could not be written much better! Going through this post reminds me of my previous roommate!
    He constantly kept talking about this. I’ll send this
    information to him. Fairly certain he will have a great read.
    Many thanks for sharing!

  2. Hi, this weekend is pleasant for me, because this point in time i am reading
    this wonderful informative article here at my residence.

  3. Hello there! This is my 1st comment here so I just wanted to give
    a quick shout out and say I genuinely enjoy reading through your
    blog posts. Can you recommend any other blogs/websites/forums
    that go over the same subjects? Thanks a lot!

  4. I am really impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent quality writing, it is rare to see
    a great blog like this one today.

  5. Hi there Dear, are you truly visiting this site regularly, if so then you will
    definitely get pleasant knowledge.

  6. I think this is among the most vital information for me. And i’m glad reading your article.
    But wanna remark on few general things, The website style is ideal, the articles is really excellent :
    D. Good job, cheers

  7. After looking over a number of the blog articles on your blog, I honestly like your technique of writing a blog.

    I book marked it to my bookmark site list and will be checking back soon. Take a look at my website as well and tell me what you
    think.

  8. Your style is really unique in comparison to other folks I have read stuff from.
    Thank you for posting when you have the opportunity, Guess I’ll just book mark this site.

  9. Ⅿy brother recommended I might like thіs website.

    He waѕ totaally right. This post actuaⅼly mаde my day.
    You cann’t imagine simply how much time I had spent
    for this informаtion! Thanks!

  10. These are in fact great ideas in about blogging.

    You have touched some good factors here. Any way keep up writing.

Leave a Reply

Your email address will not be published.