Thom Nichols


Technology is evolution outside the gene pool

Tweet your Project status with Maven and Groovy

I saw a tweet from Tim_Yates a while ago about keeping track of Groovy plugin releases.  Well, what if each plugin project had its own Twitter feed?

That's essentially what I've done for the HTTPBuilder project, my solution for making the complexities of HTTP both simple and accessible from Groovy.  HTTPBuilder automatically 'tweets' every time I publish a release as part of the build process. 

I've been meaning to write this for a while and now that I've been beaten to the punch by an Ant/Java solution, it's motivated me to finally share this.

Thank you GMaven!

Maven and the GMaven plugin take care of the bulk of the work here.  GMaven already has the capability to run a Groovy script when a certain build phase is executed in Maven.  So, add a simple script that uses the Twitter REST API, et voila!  You automatically tweet to your users every time a release (or snapshot) is deployed.  How cool is that?

The Script

The script itself is fairly simple -- at least, HTTPBuilder makes is easy -- thanks to OAuth integration and simple POST support.  This takes care of the twitter authentication and message encoding:
// Send a Twitter update when a release is made.  Cool!

import static*

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1')
twitter = new RESTClient( '' )


println "Sending tweet..."

msg = "v${pom.version} has been released! (${new Date()}) #groovy"

resp = body : [ status:msg, source:'httpbuilder' ],
        requestContentType : URLENC, contentType:TEXT )

println "Tweet response status: ${resp.statusLine}"
println '!/httpbuilder/status/${}'
assert resp.statusLine.statusCode == 200
Thanks to GMaven, you can get handy bits of build information (like the project version) from the 'pom' object that is automatically added to the script scope. 

Maven Configuration

The Maven piece is equally simple:

That's It!

I wish it was more complicated, but it's really not.  Execute a groovy script when you test, build or release an artifact in Maven.  And if a lot projects did this, it would be very easy to keep up-to-date on your latest releases. 

Category: Maven Groovy REST