Dynamically setting ml-gradle properties

713 Views Asked by At

I need to set an ml-gradle property (mlModulesDatabaseName) within the build script itself. I was under the impression that the gradle-y way to do that was to use the extra modules extension:

ext {
    mlModulesDatabaseName = 'Modules'
}

This seems to place the value inside the project.properties map as it would if it was read from the gradle.properties, but its doesn't seem to target the correct database when I attempt to run the mlReloadModules task:

$ ./gradlew mlReloadModules -Pdev
:mlDeleteModuleTimestampsFile
:mlClearModulesDatabase
Clearing modules database
Logging HTTP response body to assist with debugging: {"errorResponse":{"statusCode":"404", "status":"Not Found", "messageCode":"XDMP-NOSUCHDB", "message":"XDMP-NOSUCHDB: xdmp:database(\"my-app-modules\") -- No such database my-app-modules"}}
Unable to clear database; cause: 404 Not Found
Finished clearing modules database
:mlPrepareRestApiDependencies
:mlLoadModules
:mlReloadModules

BUILD SUCCESSFUL

This may be ignorance of how gradle scopes its properties on my part, but you would think this would work. Any suggestions on how to pull this off?

3

There are 3 best solutions below

2
On BEST ANSWER

As far as i can remember ml-gradle reads properties immediately after being applied as a plugin. This means all changes to properties after this line

apply plugin: "com.marklogic.ml-gradle"

have no effect. Have you tried setting your ext properties before applying the ml-gradle plugin?

Edit: Another way to set custom properties is to set them like this:

ext {
    mlAppConfig {
        modulesDatabaseName = 'Modules'
    }
}

This also works after the apply plugin line.

1
On

I would recommend using the gradle properties plugin. Put something like this at the top of your build.gradle file if you don't have it already:

plugins {
  id "net.saliman.properties" version "1.4.6"
  id "com.marklogic.ml-gradle" version "3.7.1"
}

Once you have saliman properties plugin in place, you can drop your dev-specific properties in a file called gradle-dev.properties, and run with -PenvironmentName=dev to enable them.

By default it will look for both gradle.properties and gradle-local.properties. It will always read both the gradle.properties as well as the environment-specific properties file (if exists). The latter will override properties from the first.

Depending on the specific tasks, you can also override properties from within build.gradle, but I'd avoid doing that with tasks that come with ml-gradle out of the box.

HTH!

0
On

I would like to share another approach which I use to run ml-gradle tasks within MarkLogic Data Hub Framework (DHF) projects. In DFH development, I sometimes need to run a same task on either the staging DB or the final DB. So I use a GradleBuild task to wrap around the ml-gradle task and set the project properties within the GradleBuild task.

task myFinalDbTask(type: GradleBuild) {    
  tasks = ['myMlGradleTask']
  startParameter.projectProperties = [
    database: mlFinalDbName,
    port: mlFinalPort
  ]
}