1
votes

I'm using ml-gradle to run a block of XQuery to update the MarkLogic database. The problem I am running into is I need to wrap all of the code in quotes, but since the code itself has quotes in it I am running into some errors when I try to declare variables i.e. let $config. Does anyone know a way around this? I was thinking I could concatenate all of the code into one big string so it ignores the first and last quotation.

task addCron(type: com.marklogic.gradle.task.ServerEvalTask) {
  xquery = "xquery version \"1.0-ml\";\n" +
          "import module namespace admin = \"http://marklogic.com/xdmp/admin\" at \"/MarkLogic/admin.xqy\";\n" +
          "declare namespace group = \"http://marklogic.com/xdmp/group\";\n" +
          " let $config := admin:get-configuration()\n" +

It bombs out when it is trying to declare $config as a variable. With the error:

> Could not get unknown property 'config' for task ':

Here is an example that works

task setSchemasPermissions(type: com.marklogic.gradle.task.ServerEvalTask) {
  doFirst {
    println "Changing permissions in " + mlAppConfig.schemasDatabaseName + " for:"
  }
  xquery = "xdmp:invoke('/admin/fix-permissions.xqy', (), map:entry('database', xdmp:database('" + mlAppConfig.schemasDatabaseName + "')))"
}

Here is some documentation for ServerEvalTask: https://github.com/marklogic-community/ml-gradle/wiki/Writing-your-own-task

1
In general, embedding other language code inside of another as an escaped string is a little messy and hard to read. For quick little things, maybe not that big of a deal, but you might consider externalizing the XQuery code into separate files that are read from the task rather than embedding. If you are embedding code, don't forget about triple quotes """ and ''' and make sure to understand what a GString is groovy-lang.org/syntax.html#_gstring_and_string_hashcodes If you want it to be a string literal, use single quotes to wrap the code instead of double quotes. - Mads Hansen
Thanks for the input! I'm trying to create a cron in gradle, and even running a simple command I get a failed state. Even doing xquery = "fn:current-datetime();" I get a build failed. - Beez
I was able to fix this issue, by switching to a json style task, and creating a task package in my ml-config - Beez

1 Answers

0
votes

I suspect you are hitting some string template mechanism in Groovy/Gradle. Try escaping the $ sign as well.

Note that you can use both single and double quotes in XQuery code.

HTH!