Skip to content

Commit d432456

Browse files
committed
#203 New task for exporting modules
1 parent b15fce4 commit d432456

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.marklogic.gradle.task.databases.*
2424
import com.marklogic.gradle.task.datamovement.AddCollectionsTask
2525
import com.marklogic.gradle.task.datamovement.AddPermissionsTask
2626
import com.marklogic.gradle.task.datamovement.DeleteCollectionsTask
27+
import com.marklogic.gradle.task.datamovement.ExportModulesTask
2728
import com.marklogic.gradle.task.datamovement.RemoveCollectionsTask
2829
import com.marklogic.gradle.task.datamovement.RemovePermissionsTask
2930
import com.marklogic.gradle.task.datamovement.SetCollectionsTask
@@ -181,6 +182,9 @@ class MarkLogicPlugin implements Plugin<Project> {
181182
project.task("mlReloadModules", group: modulesGroup, dependsOn: ["mlClearModulesDatabase", "mlLoadModules"], description: "Reloads modules by first clearing the modules database and then loading modules")
182183
project.task("mlWatch", type: WatchTask, group: modulesGroup, description: "Run a loop that checks for new/modified modules every second and loads any that it finds. To ignore files that are already dirty and only process new changes, include -PignoreDirty=true . ")
183184
project.task("mlDeleteModuleTimestampsFile", type: DeleteModuleTimestampsFileTask, group: modulesGroup, description: "Delete the properties file in the build directory that keeps track of when each module was last loaded")
185+
project.task("mlExportModules", type: ExportModulesTask, group: modulesGroup, description: "Export modules matching a URI pattern of ** (can be overridden via -PuriPattern) from the database " +
186+
"defined by mlModulesDatabaseName (can be overridden via -PdatabaseName) to the last path defined by mlModulePaths (can be overridden via -PexportPath). For each module that cannot be exported, " +
187+
"an error will be logged; an error will be thrown instead by setting -PlogErrors to false.")
184188

185189
String qconsoleGroup = "ml-gradle qconsole"
186190
project.task("mlImportWorkspaces", type: ImportWorkspacesTask, group: qconsoleGroup, description: "Import workspaces into qconsole")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.DatabaseClient
4+
import com.marklogic.client.datamovement.ExportListener
5+
import com.marklogic.client.ext.datamovement.QueryBatcherTemplate
6+
import com.marklogic.client.ext.datamovement.consumer.WriteToFileConsumer
7+
import org.gradle.api.tasks.TaskAction
8+
9+
class ExportModulesTask extends DataMovementTask {
10+
11+
@TaskAction
12+
void exportModules() {
13+
String exportPath = ""
14+
String uriPattern = "**"
15+
String databaseName = getAppConfig().getModulesDatabaseName()
16+
boolean logErrors = true
17+
18+
if (project.hasProperty("exportPath")) {
19+
exportPath = project.property("exportPath")
20+
} else {
21+
List<String> modulePaths = getAppConfig().getModulePaths()
22+
if (modulePaths == null || modulePaths.isEmpty()) {
23+
println "Cannot export modules, no module paths are defined; can use -PexportPath= to define a path to export to"
24+
return
25+
}
26+
27+
// Use the last path; if there are multiple, the ones at the beginning may be from dependencies
28+
exportPath = modulePaths.get(modulePaths.size() - 1) + "/root"
29+
}
30+
31+
if (project.hasProperty("uriPattern")) {
32+
uriPattern = project.property("uriPattern")
33+
}
34+
35+
if (project.hasProperty("databaseName")) {
36+
databaseName = project.property("databaseName")
37+
}
38+
39+
if (project.hasProperty("logErrors")) {
40+
logErrors = Boolean.parseBoolean(project.property("logErrors"))
41+
}
42+
43+
File out = new File(exportPath)
44+
println "Will export modules from database '" + databaseName + "' matching pattern '" + uriPattern + "' to path: " + out.getAbsolutePath()
45+
46+
DatabaseClient client = getAppConfig().newAppServicesDatabaseClient(databaseName)
47+
try {
48+
WriteToFileConsumer consumer = new WriteToFileConsumer(out)
49+
consumer.setLogErrors(logErrors)
50+
51+
QueryBatcherTemplate template = newQueryBatcherTemplate(client)
52+
ExportListener listener = new ExportListener()
53+
listener.onDocumentReady(consumer)
54+
template.applyOnUriPattern(listener, "**")
55+
} finally {
56+
client.release()
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)