Skip to content

Writing a task that talks to a different port

Rob Rudin edited this page Dec 15, 2023 · 8 revisions

This page is largely obsolete and the information in Writing-your-own-task should be of more assistance in creating custom Gradle tasks.

Interacting with the Management REST API

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("...")
  }
}

Interacting with a Client REST API

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
  }
}

As of version 3.0.0, the underlying "mlAppConfig" (an instanceof AppConfig in ml-app-deployer) object that ml-gradle creates also has some methods for creating a DatabaseClient for specific databases, e.g.

def modulesClient = mlAppConfig.newModulesDatabaseClient()
def schemasClient = mlAppConfig.newSchemasDatabaseClient()
def anyDbClient = mlAppConfig.newAppServicesDatabaseClient("any-database-name")

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.

Also see the RESTClient API in HTTPBuilder - you may find this to be a better fit than HTTPBuilder.

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