Table of contents
1.
Introduction
2.
Interactive Mode
3.
Creating Custom Scripts
4.
Creating Custom Commands
5.
Re-using Grails scripts
5.1.
Invoking Gradle
5.2.
Invoking Ant
6.
Building with Gradle
7.
Frequently Asked Questions
7.1.
What are the 3 phases of the Gradle lifecycle?
7.2.
Does Gradle build run all tasks?
7.3.
What is Grail Java?
8.
Conclusion
Last Updated: Mar 27, 2024

Grails-The Command Line

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The command line interface in Grails 3.0 significantly differs from the command line interface in earlier versions of Grails. It includes APIs for calling Gradle for build-related tasks and for executing code generation.

When you type: grails <<command name>>, Based on the profile of the active application, Grails searches the profile repository. Commands are read from the web profile and the base profile from which it inherits if the profile is for a web application.

Since command behavior profiles are specific, the web profile may provide different behavior for the run-app command than a profile for running batch applications.

Interactive Mode

The Grails command line has an interactive mode option that keeps the JVM running and speeds up command execution. Type "grails" at the command line to start interactive mode, and then use TAB completion to retrieve a list of commands:

interactive output

Image source

If you want to open a file while within interactive mode, you can utilize the open command, which will TAB complete file paths:

interactive open cmd

Image source

Even better, the open command can open the most recent test and dependency reports, respectively, thanks to the logical aliases "test-report" and "dep-report." In other words, type open test-report to open the test report in a browser. You can even open several files simultaneously: Open test-report test/unit/MyTests.groovy will open the MyTests.groovy source file in your text editor and the HTML test report in your browser.

After using the create-* instructions, class names can also be completed using TAB:

interactive complete class

Image source

If you need to run an external process whilst interactive mode is running you can do so by starting the command with a !:

interactive run external

Image source

Note that with ! (bang) commands, you will get file path auto-completion - ideal for external commands that work on the file system such as 'cat', 'ls', 'git', etc.

Enter the exit command to leave the interactive mode. The interactive mode console will close when the Grails application has been launched with run-app typically because the JVM will also close. The program running in forked mode, which means it uses a different JVM, would be an exception to this rule. In that instance, the application will continue to execute even when the interactive mode console shuts down. Use the quit command to end an application currently executing in forked mode and exit interactive mode. The interactive mode will close after the quit command stops the operating application.

Also see, wc command in linux

Creating Custom Scripts

You can make your Command scripts by running the create-script command from the root of your project. For example, the following command will make a script called src/main/scripts/hello-world.groovy:

See down for an example script that prints "Hello World":

description "Example description", "grails hello-world"
println "Hello World"

The output viewed by grails help is defined by the description method, which also helps script users. An even better illustration of giving a description based on the generate-all command is as follows:

description( "Generates a controller that does CRUD operations and the associated views" ) {
  usage "grails generate-all <<DOMAIN CLASS>>"
  flag name:'force', description:"Whether to overwrite existing files"
  argument name:'Domain Class', description:'The name of the domain class'
}

As you can view this description profiles usage instructions, a flag, and an argument. This allows the command to be utilized as follows:

grails generate-all MyClass --force


Also check out - Phases of Compiler

Creating Custom Commands

You can construct your own commands by executing the create command at the project's root. For instance, the command "grails-app/commands/HelloWorldCommand" creates a command with that name:

grails create-command HelloWorld

Re-using Grails scripts

You might find Grails' extensive out-of-the-box command line features helpful in your scripts. Any Grails script you write can call another script by by using a method:

testApp()

The above will invoke the test-app command. You can even pass arguments with the help of method arguments:

testApp('--debug-jvm')

Invoking Gradle

Rather than invoking another Grails CLI command, you can invoke the Gradle directory using the gradle property.

gradle.compileGroovy()

Invoking Ant

You can even invoke Ant tasks from scripts which can help if you need to write code generation and automation tasks:

ant.mkdir(dir:"path")

Building with Gradle

Since Grails 3.1, build-related responsibilities, including compilation, testing, and creating binary distributions of your project are handled by the Gradle built system. Gradle 2.2 or later is advised for use with Grails 3.1. (and higher).

The build specifies the build.gradle file, which includes information on the version of your project, its dependencies, and the repositories where those dependencies may be found (amongst other things).

The Gradle version that comes with Grails 3.1 (now 2.9) is invoked when you use the grails command via the Gradle Tooling API:

# Equivalent to 'gradle classes'
$ grails compile

Although you can run your own local copy of Gradle by using the gradle command, you will require Gradle 2.2 or higher to function with Grails 3.0 (and higher):

$ gradle assemble

Frequently Asked Questions

What are the 3 phases of the Gradle lifecycle?

Every Gradle build proceeds through three lifecycle phases in precisely the same order. These phases are initialization, configuration, and execution. During the initialization phase, Gradle starts up and locates the build files it must process.

Does Gradle build run all tasks?

Multiple tasks can be carried out using a single build file. The build file can be handled by Gradle using the Gradle command. This command will perform each task and its dependencies using a different option while compiling each task in the order it is listed.

What is Grail Java?

Grails is Java and Groovy framework used when developing agile web applications. Grails implements the MVCS (Model, View, and Controller) design pattern. 

Conclusion

This blog will teach us about the basic command line inputs for grails. In learning it we also understood how to create a custom script, custom commands, how can we reuse grails scripts, and what Gradle is. At last, we also studied building CLI with Gradle.

For more content, Refer to our guided paths on Coding Ninjas Studio to upskill yourself.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

thank you

 

Live masterclass