Skip to content

Writing a task that talks to a different port

rjrudin edited this page May 4, 2016 · 8 revisions

Writing a task that talks to the Management REST API on port 8002 is straightforward - you can write a custom task that references mlManageClient, which is an instance of ManageClient:

task example {
  doLast {
    mlManageClient.putJson("...")
  }
}

Or you can extend MarkLogicTask and access the ManageClient that way as well:

task example(type: com.marklogic.gradle.task.MarkLogicTask) {
  doLast {
    getManageClient().putJson("...")
  }
}

But sometimes, you need to talk to a port other than the Management one. If you need to talk to the REST API port associated with your application, you can extend MarkLogicTask and get an instance of a MarkLogic DatabaseClient:

task example(type: com.marklogic.gradle.task.MarkLogicTask) {
  doLast {
    def client = newClient()
    // Do something with the client - see http://docs.marklogic.com/javadoc/client/index.html
  }
}

If you need to talk to a different port, or you'd prefer a different API for sending HTTP requests, then give HTTPBuilder a try. You'll need to include it in your buildscript dependencies:

buildscript {
  dependencies {
    classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7'
  }
}

And then you can import the classes you need and write your task. See the HTTPBuilder Wiki for examples of how to use it.

There is also an issue for providing nicer integration with HTTPBuilder.

Clone this wiki locally