(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 grails.dev.commands.*

class MyExampleCommand implements ApplicationCommand {

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

The command can then be run with:

grails my-example

Or as a Gradle task:

gradle myExample

Note that the plugin should be on both the build classpath and the runtime classpath in build.gradle:

buildscript {
  ...
  dependencies {
    classpath "org.grails.plugins:myplugin:0.1-SNAPSHOT"
  }
  ...
  dependencies {
    runtime "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 the database etc.