5 The Command Line

Grails 3.0’s command line system differs greatly from previous versions of Grails and features APIs for invoking Gradle for build related tasks, as well as performing code generation.

When you type:

  1. grails <<command name>>

Grails searches the profile repository based on the profile of the current application. If the profile is for a web application then commands are read from the web profile and the base profile which it inherits from.

Since command behavior is profile specific the web profile may provide different behavior for the run-app command then say a profile for running batch applications.

When you type the following command:

  1. grails run-app

It will first search the application, and then the profile for commands:

  • PROJECT_HOME/src/main/scripts/run-app.groovy

  • [profile]/commands/run-app.groovy

  • [profile]/commands/run-app.yml

To get a list of all commands and some help about the available commands type:

  1. grails help

which outputs usage instructions and the list of commands Grails is aware of:

  1. grails <<environment>>* <<target>> <<arguments>>*'
  2. | Examples:
  3. $ grails dev run-app
  4. $ grails create-app books
  5. | Available Commands (type grails help 'command-name' for more info):
  6. | Command Name Command Description
  7. ----------------------------------------------------------------------------------------------------
  8. clean Cleans a Grails application's compiled sources
  9. compile Compiles a Grails application
  10. ...
Refer to the Command Line reference in the Quick Reference menu of the reference guide for more information about individual commands

Arguments

The grails command is a front to a gradle invocation, because of this there can be unexpected side-effects.For example, when executing grails -Dapp.foo=bar run-app the app.foo system property won’t be available to your application. This is because bootRun in your build.gradle configures the system properties.To make this work you can simply append all System.properties to bootRun in build.gradle like:

  1. bootRun{
  2. systemProperties System.properties // Please note not to use '=', because this will override all configured systemProperties. This will append them.
  3. }

Or if you only want to pass through a limited set, you can prefix your system properties using an arbitrary prefix and configure bootRun like:

  1. bootRun{
  2. bootRun {
  3. systemProperties System.properties.inject([:]){acc,item-> item.key.startsWith('boot.')?acc << [(item.key.substring('boot.'.length())):item.value]:acc }
  4. }
  5. }

In this example only system properties starting with boot. are passed through.

Application and JVM arguments should be specified in bootRun as well.

  1. bootRun{
  2. bootRun {
  3. jvmArgs('-Dspring.output.ansi.enabled=always')
  4. args('--app.foo=bar','--app.bar=foo') // Override the `app.foo` and `app.bar` config options (`grailsApplication.config`)
  5. }
  6. }

non-interactive mode

When you run a script manually and it prompts you for information, you can answer the questions and continue running the script. But when you run a script as part of an automated process, for example a continuous integration build server, there’s no way to "answer" the questions. So you can pass the --non-interactive switch to the script command to tell Grails to accept the default answer for any questions, for example whether to install a missing plugin.

For example:

  1. grails war --non-interactive