From 6fbb40598961f4f52b9c09f05c7d79c864a0f74d Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Mon, 7 Sep 2015 09:47:13 -0400 Subject: [PATCH] #38 WatchTask now reuses the XccAssetLoader in LoadModulesCommand to cut down on duplication --- .../disconnected-project/gradle.properties | 2 +- examples/flexrep-project/gradle.properties | 2 +- sample-project/build.gradle | 7 +-- .../src/{main => test}/resources/logback.xml | 8 +-- .../gradle/task/MarkLogicTask.groovy | 8 +-- .../gradle/task/client/WatchTask.groovy | 55 ++++++++++++------- 6 files changed, 45 insertions(+), 37 deletions(-) rename sample-project/src/{main => test}/resources/logback.xml (71%) diff --git a/examples/disconnected-project/gradle.properties b/examples/disconnected-project/gradle.properties index 50b380552..c3e0e0e8f 100644 --- a/examples/disconnected-project/gradle.properties +++ b/examples/disconnected-project/gradle.properties @@ -2,7 +2,7 @@ mlHost=localhost mlAppName=disconnected-example mlRestPort=8110 -mlGradleDependency=com.rjrudin:ml-gradle:2.0b5 +mlGradleDependency=com.rjrudin:ml-gradle:2.0b7 # Just using admin for everything in this project, as it's a sample of running disconnected, not all of ml-gradle's features mlUsername=admin diff --git a/examples/flexrep-project/gradle.properties b/examples/flexrep-project/gradle.properties index b6cb4b436..ddb594b7b 100644 --- a/examples/flexrep-project/gradle.properties +++ b/examples/flexrep-project/gradle.properties @@ -1,4 +1,4 @@ -mlGradleDependency=com.rjrudin:ml-gradle:2.0b6 +mlGradleDependency=com.rjrudin:ml-gradle:2.0b7 mlHost=localhost mlAppName=flexrep-example diff --git a/sample-project/build.gradle b/sample-project/build.gradle index bef875b98..081aa1ff9 100644 --- a/sample-project/build.gradle +++ b/sample-project/build.gradle @@ -9,7 +9,7 @@ buildscript { maven {url "http://rjrudin.github.io/marklogic-java/releases"} } dependencies { - classpath "com.rjrudin:ml-gradle:2.0b6" + classpath "com.rjrudin:ml-gradle:2.0b7" } } @@ -53,7 +53,7 @@ apply plugin: 'ml-gradle' /* - * The Java plugin is used to compile and run JUnit tests. It also is needed for the "mlWatch" task. + * The Java plugin is used to compile and run JUnit tests. */ apply plugin: 'java' @@ -98,9 +98,6 @@ configurations { * using that library in tests that must invoke MarkLogic HTTP endpoints. */ dependencies { - // This is needed at runtime in order to run gradle mlWatch - runtime "com.rjrudin:ml-javaclient-util:2.2.1" - // Needed to compile and run the JUnit tests testCompile "com.rjrudin:ml-junit:2.2.2" testCompile "com.jayway.restassured:rest-assured:2.4.1" diff --git a/sample-project/src/main/resources/logback.xml b/sample-project/src/test/resources/logback.xml similarity index 71% rename from sample-project/src/main/resources/logback.xml rename to sample-project/src/test/resources/logback.xml index 6d6fd0882..53a3bbcd3 100644 --- a/sample-project/src/main/resources/logback.xml +++ b/sample-project/src/test/resources/logback.xml @@ -1,7 +1,7 @@ - + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n @@ -12,8 +12,8 @@ - - - + + + \ No newline at end of file diff --git a/src/main/groovy/com/rjrudin/marklogic/gradle/task/MarkLogicTask.groovy b/src/main/groovy/com/rjrudin/marklogic/gradle/task/MarkLogicTask.groovy index 01c1b9ed7..224ecab4d 100644 --- a/src/main/groovy/com/rjrudin/marklogic/gradle/task/MarkLogicTask.groovy +++ b/src/main/groovy/com/rjrudin/marklogic/gradle/task/MarkLogicTask.groovy @@ -3,7 +3,6 @@ package com.rjrudin.marklogic.gradle.task import org.gradle.api.DefaultTask import com.marklogic.client.DatabaseClient -import com.marklogic.client.DatabaseClientFactory import com.rjrudin.marklogic.appdeployer.AppConfig import com.rjrudin.marklogic.appdeployer.AppDeployer import com.rjrudin.marklogic.appdeployer.command.Command @@ -36,16 +35,15 @@ class MarkLogicTask extends DefaultTask { AdminManager getAdminManager() { getProject().property("mlAdminManager") } - + DatabaseClient newClient() { - AppConfig config = getAppConfig() - return DatabaseClientFactory.newClient(config.host, config.restPort, config.getRestAdminUsername(), config.getRestAdminPassword(), config.authentication) + getAppConfig().newDatabaseClient() } void deployWithCommandListProperty(String propertyName) { deployWithCommands(getProject().property(propertyName)) } - + void deployWithCommands(List commands) { SimpleAppDeployer deployer = new SimpleAppDeployer(getManageClient(), getAdminManager()) deployer.setCommands(commands) diff --git a/src/main/groovy/com/rjrudin/marklogic/gradle/task/client/WatchTask.groovy b/src/main/groovy/com/rjrudin/marklogic/gradle/task/client/WatchTask.groovy index 8c5373ca3..a464f6787 100644 --- a/src/main/groovy/com/rjrudin/marklogic/gradle/task/client/WatchTask.groovy +++ b/src/main/groovy/com/rjrudin/marklogic/gradle/task/client/WatchTask.groovy @@ -1,33 +1,46 @@ package com.rjrudin.marklogic.gradle.task.client -import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.TaskAction -import com.rjrudin.marklogic.appdeployer.AppConfig +import com.marklogic.client.DatabaseClient +import com.rjrudin.marklogic.appdeployer.AppDeployer +import com.rjrudin.marklogic.appdeployer.command.modules.LoadModulesCommand +import com.rjrudin.marklogic.appdeployer.impl.SimpleAppDeployer +import com.rjrudin.marklogic.gradle.task.MarkLogicTask import com.rjrudin.marklogic.modulesloader.impl.DefaultModulesLoader -class WatchTask extends JavaExec { +/** + * Runs an infinite loop, and each second, it loads any new/modified modules. Often useful to run with the Gradle "-i" flag + * so you can see which modules are loaded. + */ +class WatchTask extends MarkLogicTask { - String modulesLoaderClassName = DefaultModulesLoader.class.getName() + long sleepTime = 1000 @TaskAction - @Override - public void exec() { - setMain("com.marklogic.clientutil.modulesloader.ModulesWatcher") - setClasspath(getProject().sourceSets.main.runtimeClasspath) + public void watchModules() { + println "Getting LoadModulesCommand from mlAppDeployer so it can be reused to load modules" + AppDeployer d = getAppDeployer() + if (d instanceof SimpleAppDeployer) { + LoadModulesCommand command = d.getCommand("LoadModulesCommand") + DefaultModulesLoader loader = new DefaultModulesLoader(command.newXccAssetLoader(getCommandContext())) + List paths = getAppConfig().getModulePaths() + println "Watching modules in paths: " + paths - AppConfig config = getProject().property("mlAppConfig") - def username = config.getRestAdminUsername() ? config.getRestAdminUsername() : getProject().property("mlUsername") - def password = config.getRestAdminPassword() ? config.getRestAdminPassword() : getProject().property("mlPassword") - setArgs([ - config.getModulePaths().join(","), - config.getHost(), - config.getRestPort(), - username, - password, - modulesLoaderClassName - ]) - - super.exec() + DatabaseClient client = newClient() + while (true) { + for (String path : paths) { + loader.loadModules(new File(path), client); + } + try { + Thread.sleep(sleepTime); + } catch (InterruptedException ie) { + // Ignore + } + } + } + else { + throw new RuntimeException("Could not an instance of LoadModulesCommand in mlAppDeployer, thus not able to connect to MarkLogic to load modules") + } } }