Table of contents
1.
Introduction
2.
Getting Started with Grails
2.1.
Installation Requirements
2.2.
Downloading and Installing the Grails
2.2.1.
Install with SDKMAN
2.2.2.
Install Manually
2.3.
Creating an Application using Grails
2.4.
A Hello World Example
2.5.
Using the Interactive Mode
2.6.
Getting the setup in an IDE
2.6.1.
IntelliJ IDEA
2.6.2.
Text Editors
2.7.
Convention over Configuration
2.8.
Run and Debug an Application
2.9.
Testing an Application
2.10.
Deploying an Application
2.11.
Supported Java EE Containers
2.12.
Creating Artefacts in Grails
2.13.
Generating an Application in Grails
3.
Frequently Asked Questions
3.1.
What is the use of Grails?
3.2.
Is Grails a good framework?
3.3.
Does Grails use Maven?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Getting Started with Grails

Author Sanjana Yadav
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello, Readers!! 

We are already familiar with web frameworks and their applications. Some common frameworks are React.jsDjango, and AngularJs. Let us explore another web application framework called Grails. In this article, we will see an introduction to Grails, including its installation, setup, and examples.

Getting Started with Grails

So, let’s get started!!

Getting Started with Grails

Grails is a free, open-source online application framework using Apache Groovy programming language. It is meant to be a high-productivity software. It follows the “code by convention” paradigm by offering a standalone development environment and keeping much of the configuration information hidden from the developer.

Let us now install and set up grails.

Installation Requirements

Before installing Grails 5.2.4, we need:

  1. A Java Development tool Kit (JDK) installed version 1.8 or above.
     
  2. After downloading the required JDK for your operating system, you need to run the installer and then, pointing to the location of this installation, set up the environment variable called JAVA_HOME.
     
  3. Java installation can be automatically detected on some platforms like macOS. However, in most cases, you will have to configure the java location manually.

Downloading and Installing the Grails

There are several ways to install grails. Let’s look at each individually:

Install with SDKMAN

Follow the steps below to install grails using SDKMAN:

  1. Run the following command on your terminal to install the latest version.
sdk install grails


2. Or you can run the command with the version being specified.

sdk install grails 5.2.4

Install Manually

Follow the steps below for the manual installation of grails:

  1. Download and extract the zipped file containing the binary distribution of Grails to your preferred location.
  2. Set the GRAILS_HOME environment variable to the same location where the file is extracted.


The further procedure will be different for Unix/Linux and windows. Let us see them individually.

Unix/Linux

  • Add export GRAILS_HOME=/path/to/grails to your profile.
  • This can be achieved by adding export PATH="$PATH:$GRAILS_HOME/bin" to your profile.


Windows

  • Go to the folder where you have extracted the Grails folder, and then copy the path to bin directory. For example,
    C:/path_to_grails/bin
  • Go to environment variables and edit the path variable in User variables or System variables.
  • Now, create a new path variable and paste the copied path in the path variable.
  • To check if the Grail is properly installed, type grails -version in the command prompt. You should get the output as
    Grails version: 5.2.4

Creating an Application using Grails

We first need to be comfortable with the grails command to create a Grails application.
Syntax

grails <<command name>>


The create-app command is used to create an application in Grails.

  • Run the create-app command:
    grails create-app helloworld
  • The above command will create a new directory inside the existing one. This new directory, helloworld in this case, contains the project. Go to this directory.
    cd helloworld

Let us understand this with a classic Hello World example.

A Hello World Example

Let us take the newly created project and see a “Hello World” example.

Firstly, change your directory to helloworld and then start the grails interactive console. Below are the commands for this.

$ cd helloworld
$ grails


You should be able to see a window similar to the below image.

A Hello World Example

In this application, we just want a page that prints the “Hello World” message to the browser. You have to create a new controller action for every new page in Grails. So, our task now is to create a new controller using the create-controller command.

grails> create-controller Hello
A Hello world examplr

Grails provides auto-completion on command names. So you can just type cre and hit tab to see a list of all create-* commands.

 create-* commands

The create-controller command above will create a new controller in the grails-app/controllers/helloworld HelloController.groovy directory.

After creating a controller, we now need to add an action to build a “Hello World!” page.

Edit the HelloController.groovy file by adding a render line. The code will look like this:

package helloworld

class HelloController {

    def index() {
        render "Hello World!"
    }
}


The above action is just a method. Here, it calls a special Grails’ method to render the page.

Our task is done!

To see the application, we need to start the server using a command named run-app.

grails> run-app.


This will launch an embedded server on port 8080, which will host our application. We should now be able to visit your app at http://localhost:8080/ - test it!

To see the context path through command line, use commands:

grails> run-app -Dgrails.server.servlet.context-path=/helloworld


The result will look like this:

Grails Home Page

Using the Interactive Mode

Grails has had an interactive mode since 3.0, which makes command execution quicker because the JVM does not have to be restarted for each command. To activate the interactive mode, type 'grails' from any project's root and use TAB completion to retrieve a list of possible commands. As an example, consider the screenshot below:

Using the Interactive Mode

Getting the setup in an IDE

IntelliJ IDEA

The IDE that Grails recommend is IntelliJ IDEA. Grails 5.0 development works quite well with IntelliJ IDEA.

To begin using Intellij IDEA and Grails 5.0, select File / Open and navigate to your build.gradle file to import and set up the project.

Text Editors

There are several compatible test editors available that work great with Groovy and Grails. Let us have a look at these:

  1. TextMate: It contains a bundle to support Groovy/Grails.
  2. Sublime Text Editor: We can install a plugin using Sublime Package Control for Sublime Text Editor.
  3. Atom editor: It provides a package for Groovy/Grails.
  4. Visual Studio Code: It provides an extension for Groovy/Grails.

Convention over Configuration

When configuring itself, Grails favors “convention over configuration”. This implies that file names and locations are used rather than explicit configuration, so you should become familiar with the directory structure that Grails provides.

Run and Debug an Application

Run

As we saw previously in the example, the run-app command is used to run the application. This command, by default, loads a server to the 8080 port.

grails run-app


You can also use the port argument and specify a different port:

grails run-app -port=8090


Debug

You can debug a Grails application with the following steps:

  1. Right-click on the Application.groovy class in your IDE.
  2. Now, select the appropriate action based on the results.

Or you can also run your application using the following command, then attach a remote debugger.

grails run-app --debug-jvm

Testing an Application

Grails' create-* commands generate unit or integration tests for you in the src/test/groovy directory. 

To conduct tests, use the test-app command as follows:

grails test-app

Deploying an Application

There are several ways to deploy Grails’ application.
If you are deploying to a typical container (Tomcat, Jetty, etc.), you may generate a Web Application Archive (WAR file), and Grails contains the war command for this purpose:

grails war


This will generate a WAR file in the build/libs directory, which can then be deployed according to the instructions for your container.

It should be noted that Grails will include an embeddable version of Tomcat within the WAR file by default, which may cause issues if you deploy to a different version of Tomcat. If you do not want to use the embedded container, modify the scope of the Tomcat dependencies to testImplementation before deploying to your production container in build.gradle. The command that can be used is

testImplementation "org.springframework.boot:spring-boot-starter-tomcat"


If you are creating a WAR file to deploy on Tomcat 7, you must also modify the target Tomcat version in the build. By default, Grails is built against Tomcat 8 APIs. Insert the following line above the dependencies section to target a Tomcat 7 container:

ext['tomcat.version'] = '7.0.59'


Unlike most scripts, which use the development environment by default until changed, the war command uses the production environment by default. Like any other script, you may override this by supplying the environment name, for example,

grails dev war


If you do not want to use a special Servlet container, simply execute the Grails WAR file as a standard Java program.

Supported Java EE Containers

Grails operates on any container that supports Servlet 3.0 or above, and it has been tested on the following container products:

  1. Tomcat version 7
  2. Resin version 4 or above
  3. IBM WebSphere version 8.0 or above
  4. GlassFish version 3 or above
  5. Oracle Weblogic version 12c or above
  6. Jetty version 8 or above
  7. JBoss version 6 or above

Creating Artefacts in Grails

Grails has a few handy targets, such as create-controller and create-domain-class, that will generate controllers and other artefact types for us. These are only provided for our convenience; we may just as easily use an IDE or our preferred text editor. For example, a domain model is often required to construct the foundation of an application.

We can see the model in the below example:

grails create-app helloninjas

 

cd helloninjas

 

grails create-domain-class coding


This will result in the construction of a domain class at   grails-app/domain/helloninjas/Coding.groovy, which will look like this:

package helloninjas

class Coding {
}

Generating an Application in Grails

To get started quickly with Grails, a feature called scaffolding is frequently beneficial for generating an application's skeleton. Use one of the generate-* commands, such as generate-all, to generate a controller (and its unit test) as well as the corresponding views:

grails generate-all helloninjas.Coding

Frequently Asked Questions

What is the use of Grails?

In order to get developers up and running quickly, Grails offers a development environment that includes a web server. The Grails distribution includes all necessary libraries, and it automatically gets the Java web environment ready for deployment.

Is Grails a good framework?

Since it has several characteristics that make it simpler to construct common web applications, Grails is a high-productivity framework. Creating an XML/JSON REST API is one of the things that Grails makes simpler.

Does Grails use Maven?

The Gradle wrapper will be executed automatically by the maven plugin within the grails application to build it. The build/libs directory is where the application war will be placed.

Conclusion

It's time for a quick recap! In this article, we learned the Grails framework from scratch. We saw everything from installation to creating and deploying an application using Grails.

We hope that this blog has helped you understand the Grails framework and its usefulness. To learn more about Grails and its several features, you can refer to

You can also visit our website to read more such blogs. Make sure you enroll in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can prepare for interviews with interview experiences and an interview bundle.

Keep learning and keep growing, Ninjas!

Thank you
Live masterclass