(Quick Reference)

create-command

Purpose

The create-command command creates a new Grails Gradle task and shell command that can be run with the grails command from a terminal window.

Examples

The command:

grails create-command MyExample

Creates a class called grails-app/commands/PACKAGE_PATH/MyExampleCommand.groovy such as:

import org.apache.grails.core.cli.*

class MyExampleCommand implements ApplicationCommand {

  boolean handle(ExecutionContext ctx) {
      def dataSource = applicationContext.getBean(DataSource)
      return true
  }
}

Commands can be executed with the runCommand command.

grails run-command my-example

Or as a Gradle task:

gradle runCommand -Pargs="myExample"

If the command you are executing is defined in a plugin that you have declared a dependency on, then you can execute the command in short form like so:

grails my-example

Or as a Gradle task:

gradle myExample

Declaring the plugin as a regular dependency is all that is required for the short form to work — the plugin’s companion -cli artifact (which carries its commands) is discovered from the dependency graph automatically and both the command and its Gradle task are registered:

dependencies {
    implementation "org.grails.plugins:myplugin:0.1-SNAPSHOT"
}

Description

In order to separate the code generation and build layer, in Grails 3.x scripts created with create-script do not have access to the running application instance.

Instead, Grails 3.x features a new concept called an ApplicationCommand that is invoked via Gradle to perform tasks such as interact with classes in the runtime.