Grails - Creating Custom Scripts and Commands



Description

You can create your own custom script by using the create-script as shown below.

 
project_directory> grails create-script my-script
Grails Custom Script

When you run the above command, it will create a script under src\main\scripts\my-script.groovy folder and my-script.groovy file contains below code:

 
description "Example description", "grails example-usage"

println "Welcome to Tutorialspoint!!!"

You can run the script by using the below command:

 
project_directory> grails my-script

It will produce the output as shown below:

Grails Custom Script

You can create your own custom command by using the create-command as shown below.

 
project_directory> grails create-command mycommand
Grails Custom Command

When you run the above command, it will create a command under grails-app/commands/helloworld/MycommandCommand.groovy folder the MycommandCommand.groovy file contains below code:

 
package helloworld

import grails.dev.commands.*

class MycommandCommand implements GrailsApplicationCommand {

    boolean handle() {
        return false
    }
}

The commands which are created, implements the GrailsApplicationCommand trait by default and uses the boolean handle() method to implement the command.

Advertisements