how I deploy rest service extension using gradle? - gradle

Rest service : http://host:8000/v1/config/resources/removeCollection?put:database=string&put:uris=string*
I want to deploy this REST service extension in MarkLogic using gradle. How can I deploy this?

If you're already using ml-gradle, you can add your implemented interface to marklogic\src\main\ml-modules\services and deploy using the mlLoadModules task. The mlCreateResource task as part of the scaffolding would also add metadata in marklogic\src\main\ml-modules\services\metadata.

I recommend looking at ml-gradle. You can easily hook it up in gradle by adding a few lines, most importantly being:
plugins { id "com.marklogic.ml-gradle" version "4.0.4" }
As described in the readme, you can optionally follow that with invoking the mlNewProject task, which will provide you with a useful scaffold structure for a typical ml-gradle project.
ML-gradle gives you access to all kinds of tasks, including one called mlLoadModules to deploy source, and rest extensions. There is also a built-in task for removing collections in any database, called mlDeleteCollections. You can look at the Task-reference to get a glimpse of all the tasks, or just run gradle tasks.
HTH!

Two easy ways of plugin gradle to invoke REST API:
Method One:
If you don’t have any Project just yet,
create a gradle.properties file in which you define four parameters: host, mlUsername, mlPassword, RestPort
create a build.gradle file in the same folder:
plugins {
id "com.marklogic.ml-gradle" version "4.0.4"
}
task FCdeleteCollections(type: com.marklogic.gradle.task.datamovement.DeleteCollectionsTask) {
………………..
collections = ["{collection-name}"]
}
Invoke the gradle task:
[root# ~] # gradle FCdeleteCollections
Method Two:
If you already scaffolded the Project, in my opinion, it is safer to invoke one-time deletion task like this:
[root# ~] # gradle -Pdatabase={db-name} mlDeleteCollections -Pcollections={collection-name}
My preference is to invoke such task through Java API | DMSDK.

Related

Is there a simple way to access project resource files from a gradle task?

I am working on a Gradle task for a java project. The task needs to read files from a subfolder of the project’s resources directory. I was expecting to find a standard way for Gradle to access project resources but have been unable to find one. Does Gradle provide simple way to find and import resource files? Maybe through a plugin?
If you mean the src/main/resources folder, then no. That is by convention used by the Java plugin to hold resources for the actual module and not the build classpath. But if you just want to read a file in it from a task, just use normal Java, Groovy or Kotlin APIs. You don't need a plugin for that.
Here is one for Groovy:
task printMyResource {
doLast {
logger.quiet(file("src/main/resources/subfolder/my_file.txt").text)
}
}
(The file method resolves a path to a File object relative to the project folder.)

Is there a way to list task properties in Gradle?

There is a command that lists all tasks (gradle tasks) accessible at project level which I am using very often when while working with Gradle. Is there a way to see properties available inside each of those tasks? I want to be able to do something like that:
gradle properties compileJava #this should list all properties available in **compileJava** task
One thing you can do is gradle help --task compileJava.
This will indicate which projects do define the compileJava task and also tell you what is the type of the task.
With this type, you can consult the DSL documentation to get information about the available properties.
There is no tool part of the Gradle CLI (at this time - Gradle 6.0.1) that will give you this in your terminal.
Many properties are not exposed via gradle help --task XXX. Use gradle model to get the list of all available tasks: it prints the class baking the task. Get the source code for the class.
Alternative way is via logging (run gradle bump):
tasks.register("dump") {
doLast{
logger.lifecycle("type: {}", bootJar.properties);
}
}

Executing integration tests in gradle

Me and my team are working on a project with a lot of modules. We are using gradle for the project and everyone is new to gradle. We have a Main parent project i.e, parent build with the details of project dependencies. We want to add the integration_test task configuration to all the modules so that we can call the command gradle integration_test. So is there any way or concept of writing the configuration in the main module and make the child projects import the same configuration.
FYI: I tried it by directly adding it to the main project but got an error saying the classpath for the files which I specified does not exists. Any help or thought would be appreciated. Thanks in advance.
Is there a particular reason to split "integration tests" from the standard test task?
If so, you can run the same script for all subprojects from the main project's build file: https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:subproject_configuration
For instance:
subprojects {
task integrationTest {
// whatever you need
}
}
I am note sure if this is what you talk about in the last paragraph. If so, please attache the error message you get.
It is also possible to "import" some configuration by subprojects, but the above definition is better in most scenarios.

Where can I put my custom task class in Gradle

It's easy to write a custom plugin and include it in the build script
apply from: "utilities.gradle"
For test purpose this file is in the same directory as the build.gradle
Calling a task defined in utilities.gradle from build.gradle works without any hassle.
In utilities gradle is also a plugin defined - configuring it from build.gradle just works.
But if I define a custom task in utilities.gradle calling it is no problem but if I want to use that custom taks in build.gradle it says
> Could not find property 'GreetingTask' on root project 'TestGradle'.
utilities.gradle:
task hello(type: GreetingTask)
class GreetingTask extends DefaultTask {
#TaskAction
def greet() {
println 'hello from GreetingTask'
}
}
build.gradle
task hellox(type: GreetingTask)
Ok... I read the documentation here: http://www.gradle.org/docs/current/userguide/custom_tasks.html
It says the custom task is not visible outside...
But then... how to share custom tasks with the team without making a Jar for everything.
What I want is to place the utilities.gradle on a network drive and share it with the other.
pls help
There is a special $rootDir/buildSrc directory which is its own build. All classes that this build produces are available to all build scripts in the "main" build. The buildSrc build has a default build.gradle, but you can add your own. By default, Java classes are expected under src/main/java, and Groovy classes under src/main/groovy. You can read more about buildSrc in the Gradle User Guide.
To share classes across multiple builds, a separate plugin project that publishes a Jar is the way to go.

Gradle - Add additional task to existing task

I'm working on a project that uses EJB2s. The created EJB Jars require additional processing by the application server before they're bundled in the war/ear and deployed.
I have created a custom task that works to do the additional processing if I invoke it explicitly (gradle ejbDeploy), but am having trouble fitting it into the gradle multi-project lifecyle. I need to somehow add it to the build graph to execute automatically after the jar task.
My first attempt was to add it to jar with
jar.doLast{
ejbDeploy.execute()
}
which seems to work for arbitrary code blocks, but not for tasks
What's the recommended solution for this? I see three approaches:
Hook into the build graph and add it explicitly after the jar
task.
Set it up somehow in jar.doLast{}
Set it up as a prerequisite for the WAR task execution
Is there a recommended approach?
Thanks!
I would go for approach #3 and set it up as a dependency of the war task, e.g.:
war {
it.dependsOn ejbDeploy
...
}
I'm new to Gradle, but I would say the answer really depends on what you're trying to accomplish.
If you want to task to execute when someone runs the command gradle jar, then approach #3 won't be sufficient.
Here's what I did for something similar
classes {
doLast {
buildValdrConstraints.execute()
}
}
task buildValdrConstraints(type: JavaExec) {
main = 'com.github.valdr.cli.ValdrBeanValidation'
classpath = sourceSets.main.runtimeClasspath
args '-cf',valdrResourcePath + '/valdr-bean-validation.json'
}
Add the following, and then ejbDeploy will be executed right after jar, but before war
jar.finalizedBy ejbDeploy
See Gradle Docs. 18.11. Finalizer tasks

Resources