Announcing the Gradle REST Plugin

I’ve recently implemented a new, very simple but very useful Gradle plugin.

the -

GRADLE REST PLUGIN

TA DA!!!

Gradle is the build tool of the future, man, and that’s no secret; I build with Gradle when I possibly can.

I faced a situation where I wanted to invoke a few web-hooks during and after my build; the web-hooks I addressed and the information I sent varied according to the state of my build.
At first I thought I should invoke the web-hooks via independent build steps of my CI server, but that also meant:

  1. Breaking the build to a number of separate Gradle processes instead of single process that will execute all the tasks.
  2. Maintaining the web-hook invocation procedures outside of my standard build descriptors.

The downside of #2 being the fact that should I want to run the complete build process (as it runs on my CI server), I would have to recreate my CI server configuration on every premises.

So what do I do? Invoke the web-hooks using Gradle! BOOM!

I can write Groovy code in my Gradle scripts, so why shouldn’t I use Groovy’s HTTP Builder to execute HTTP requests throughout the build process?
Better yet, I can write a Gradle plugin that will hide the implementations details of the HTTP client so that all HTTP requests look like simple Gradle tasks!

Cool, so how do I install this thing?

The plugin source resides in GitHub and is distributed through Bintray, so you can take a look at the project page to find out what’s the latest version.

First, we add the plugin repository and plugin dependency coördinates to the buildscript section of our build script:

buildscript {
    repositories {
        maven { url 'http://dl.bintray.com/content/noamt/gradle-plugins' }
    }

    dependencies {
        classpath 'org._10ne.gradle:rest-gradle-plugin:$LATEST_VERSION'
    }
}

Then, we add the plugin declaration to our build script:

apply plugin: 'rest'

Great, now how do I use it?

The plugin introduces a new task of the type org._10ne.gradle.rest.RestTask; this task is basically a wrapper for an HTTP client, so all we have to do is extend the task, give it the details of the request we want to make and execute!

For example, a GET request looks like:

task getServerVersion(type: org._10ne.gradle.rest.RestTask) {
    httpMethod = 'get'
    uri = 'https://server.com/api/version'
    username = 'user'
    password = 'password'
    contentType = groovyx.net.http.ContentType.JSON
}
Once again, Gradle FTW, man.
About these ads

6 thoughts on “Announcing the Gradle REST Plugin

  1. I’m trying to use your plugin to hit a web app and select a setting in a page. Can the url parameters via command line to the rest plugin? If it is possible how should I go about using it.

  2. Ideally I want to pass the url paramters and append it to URI from command line.
    This is how my tasks looks
    task changeServiceScenario(type: org._10ne.gradle.rest.RestTask) << {
    httpMethod = 'get'
    uri = 'http://127.0.0.1:8080/config/service?name='+$x+'&id='+$y
    contentType = groovyx.net.http.ContentType.JSON
    }

    I invoke the task as gradle changeServiceScenario -Px=… -Py=…
    I get an error that says No value has been specified for property 'uri'.

    • Thanks for taking an interest, Prasannarupan. I’d be glad to help you out; please open an issue on the plugin’s GitHub page and include a gist of the project that reproduces this behavior

  3. Hi Noam, it’d be great if your RestTask returned the HttpResponseDecorator. I need to do a GET on a status server and handle the status. Cheers, Brooke

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s