-
Notifications
You must be signed in to change notification settings - Fork 80
Writing your own task
ml-gradle wraps ml-app-deployer with a number of tasks, but in case you want to do something with the MarkLogic Manage API that isn't yet supported by ml-gradle, you can simply write a new Gradle task and use an instance of ManageClient to easily invoke Management API endpoints. The ManageClient wraps an instance of Spring's RestTemplate and is already configured with the connection properties in your gradle.properties file.
The sample-project Gradle file shows an example of how to do this, and an example is shown below as well. This simple example invokes a database endpoint for merging a database, and it utilizes the "postJson" convenience method on ManageClient along with configuration data found in the AppConfig instance returned by "getAppConfig".
task mergeContentDatabase(type: com.marklogic.gradle.task.MarkLogicTask) {
doLast {
getManageClient().postJson("/manage/v2/databases/" + getAppConfig().getContentDatabaseName(), '{"operation":"merge-database"}')
}
}
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
}
}
You can also write a task with custom XQuery or JavaScript code by extending ServerEvalTask, which uses the DatabaseClient returned by newClient():
task myXQueryTask(type: com.marklogic.gradle.task.ServerEvalTask) {
xquery = "my XQuery code here"
}
task myJavascriptTask(type: com.marklogic.gradle.task.ServerEvalTask) {
javascript = "my JS code here"
}