We can use the Gradle announce
plugin to send announcements from the build process. We can send data to Twitter (I don't know if our followers are waiting for this, but if you want to you can), but also to notification applications on our local computers. For Mac OSX Growl is supported, for Linux notify-send and for Windows Snarl.
The plugin adds an announce
object with the announce()
method. The method accepts two arguments. The first argument is the message and the second argument is either twitter
or local
to indicate where to send the announcement.
apply plugin: 'announce' task info { doLast { announce.announce "Running $it.name", 'local' println gradle.gradleVersion } }
Here we see the announcement as Growl message:
We can also get an announcement object for only sending announcement to the local notification applications. We can use a send()
method that accepts a title for the announcement as first argument and the message as second argument. To get the local announcement object we invoke announce.local
:
apply plugin: 'announce' task info { doLast { // Now we can specify a title and message announce.local.send "Gradle Info Task", 'Running' println gradle.gradleVersion } }
To automatically send out notifications when a task is executed we can implement the TaskExecutionListener
interface. From the implementation we can use the announce.local
object. In the following example build file we create the class TaskAnnouncer
and use the addTaskExecutionListener()
method to add it to the TaskExecutionGraph
available through gradle.taskGraph
:
apply { plugin 'announce' } gradle.taskGraph.addTaskExecutionListener new TaskAnnouncer(localAnnouncer: announce.local) task info { doLast { println gradle.gradleVersion } } class TaskAnnouncer implements TaskExecutionListener { Announcer localAnnouncer @Override void afterExecute(final Task task, final TaskState state) { String message if (state.failure) { message = "Failure: $state.failure.message" } else if (state.executed) { message = 'Done' } else if (state.skipped) { message = "Skipped: $state.skipMessage" } send task, message } @Override void beforeExecute(final Task task) { send task, 'Ready to run' } private void send(final Task task, final String message) { final String title = "Gradle build: $task.project.name:$task.name" localAnnouncer.send title, message } }
Automatically announce build results
To get the build results after running a build we only have to apply the build-announcements
plugin to our Gradle build. This plugin uses the local notification applications to send out a message with a summary of the build. If the build failed we get a message with the task name that failed. For a successful build we can see how many task were executed.
apply { plugin 'announce' plugin 'build-announcements' } task info { doLast { println gradle.gradleVersion } }
The following screenshots show the result of a successful and non-successful build:
Apply for all Gradle builds
To add the plugins to all Gradle builds on our local computer we can create a so-called init script. Init scripts are executed before a project build script. We can place the init scripts at several locations. Let's create a new init script announce.gradle
in our $USER_HOME/.gradle/init.d
directory. If we don't have this directory yet, we can create it ourselves. All files in this directory are treated as init scripts by Gradle and are executed automatically. Here is the contents of the announce.gradle
script:
rootProject { apply { plugin 'announce' plugin 'build-announcements' } }