November 17, 2009

Gradle Goodness: Get Notified About Finished Build with Snarl

We can add a listener to the Gradle build process to get informed about certain events during the build process. We must implement the BuildListener interface and add it to a Gradle instance. One of the methods is the buildFinished() method. We write an implementation for this method to display the build result as a Snarl notification (Snarl is a Windows equivalent of Growl in OSX). The BuildAdapter class provides an empty implementation for the BuildListener interface and is a good starting point for our implementation.

We can add the code to our build.gradle file for a project. But we want to create the listener and use it for all our Gradle builds. We create the file init.gradle and place it in the ~/.gradle directory. Gradle will invoke this script always when we invoke a Gradle build, so every project we build with Gradle will use this code.

// File: ~/.gradle/init.gradle
class SnarlNotifyListener extends BuildAdapter {
    void buildFinished(BuildResult result) {
        if (result.failure) {
            snarlNotify 'Gradle build failure', result.failure.message, true
        } else {
            snarlNotify 'Gradle build finished', 'Build successful'

    void snarlNotify(title, message = 'No message', sticky = false) {
        def cmd = [
            '<Path to Snarl_CMD.exe>',  // Replace with your local path.
            sticky ? '0' : '10',  // If non-sticky display for 10 seconds.
            '<gradle_home>/docs/groovydoc/groovy.ico'  // Nice little icon, use local path.

def listener = new SnarlNotifyListener()
gradle.addBuildListener listener

Now we can run a Gradle build and we get nice notifications when the build is finished: