Thom Nichols

Thom

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 groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1')
twitter = new RESTClient( 'https://twitter.com/statuses/update.xml' )

twitter.auth.oauth pom.properties.'twitter.oauth.consumerKey',
                pom.properties.'twitter.oauth.consumerSecret',
                pom.properties.'twitter.oauth.accessToken',
                pom.properties.'twitter.oauth.secretToken'

println "Sending tweet..."

msg = "v${pom.version} has been released! (${new Date()}) http://goo.gl/VzuT #groovy"

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

println "Tweet response status: ${resp.statusLine}"
println 'http://twitter.com/#!/httpbuilder/status/${resp.data.id.text()}'
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:
<build>
  <plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>release</id>
        <phase>deploy</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>${pom.basedir}/src/main/script/release_tweet.groovy</source>
        </configuration>
      </execution>
    </executions>
  </plugin>
</build>

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