Grails Cookbook - A collection of tutorials and examples

Grails 3 IDE Setup

This guide will show how to setup your Windows Environment for Grails 3 development. The goal is to help beginners get started by overcoming challenges of doing the initial setup and installation of required software. This tutorial will also show the optional step of installing an IDE such as Eclipse, NetBeans, IntelliJ IDEA, and Notepad++.

Introduction

Grails is a very popular framework for building web applications because of it's simplicity and shallow learning curve. This guide will focus on setting up your environment for developing application using Grails 3 framework. As of the time of this writing, the latest version of Grails is 3.1.7. To minimize problems of setting up your environment, I recommend the following combination of software:
  • JDK 8
  • Grails 3.1.X
  • Optional IDE: Eclipse Mars, Netbeans 8.1, IntelliJ IDEA Community Edition 2016.1.2, or Notepad++ 6.9.2

Of course later combinations may work, but I tested this guide from the above configuration. Note that using an IDE such as Eclipse is optional, as you can do Grails programming with only command line, file explorer, and a text editor (which I will also show).

Here is the outline of this guide:
  • Install JDK 8
  • Install Grails 3.1.X
  • Setup Environment Variables
  • Create Hello World Application without IDE
  • Install and Use Eclipse IDE
  • Install and Use NetBeans
  • Install and Use IntelliJ IDEA Community Edition
  • Install And Use Notepad++
Note that this guide uses Grails version 3.x. You may go to this post instead if you wish to install the older Grails version 2.x.

Install JDK 8

Grails is a framework that uses the Groovy language. And since Groovy runs on top of the Java platform, we need a Java Development Kit (JDK) for development purposes. For better compatability, I recommend using the JDK version 8. As of the time of this writing, Java 9 is not yet officially released.

Go to Oracle website and download the JDK installer file. http://www.oracle.com/technetwork/java/javase/downloads/index.html


Install and follow the instructions

Install Grails

Next is we will download and install the Grails SDK. Go to Grails website and download the latest version of Grails 3 framework: https://grails.org/download.html

Just extract the downloaded ZIP file to a local directory:

Setup Environment Variables

Next is we need to set our environment variable. First is we set the JAVA_HOME and point it where JDK 8 was installed. It is a good practice to set this not only for Grails development but Java in general:

Next is set the GRAILS_HOME environment variable:

And lastly, add the bin folder of Grails framework to the Path environment variable, so that grails can be easily executed in command line:

Create Hello World Application without IDE

By this time we could say that our installation is complete, because we can already do Grails development with just JDK 8 and Grails installed in the computer. Here is a short guide on how to write our first program using just command line, file explorer, and text editor:

Create New Application Project
To create a new application project, just go to the command line and issue the following command:
grails create-app hello
This will create a project named hello contained in a folder with the same name. The skeleton of the project are also created inside the folder:

Run the Application in Development Mode
Just go to the new folder created and issue the following command:
grails run-app
This will compile your project and run it in a built-in server container. You may then access the application using any web browser:

It is worth noting that the default project already has a web design that is nice looking. And it is also built using the Twitter Bootstrap framework as shown by the view source:


Create a Controller
To get a taste on how easy it is to code in Grails, we may create a "Hello World" behavior. The entry point in a Grails application that a browser talks to is a controller. We may create a new controller using a command like below:
grails create-controller com.mydomain.myapp.Hello

This will create a groovy class HelloController.groovy under the package com.mydomain.myapp. A test class is also created for it.


We may use this code for our controller:

package com.mydomain.myapp
class HelloController {
    def index() { 
        render "Hello World!"
    }
}


Grails embraces the concept of convention over configuration. This means without any config changes, we may access the url http://localhost:8080/hello/index or just simply http://localhost:8080/hello. The hello in the url corresponds to the name of the controller and the index part corresponds to which action to invoke inside the controller. The code we wrote above simply renders the text "Hello World". If we fire a browser, we will see:

Using Views
We may modify the controller code to look like this:

package com.mydomain.myapp
class HelloController {
    def index() { 
        render "Hello World!"
    }
    def greet() {}
}

So this means a new url is accessible, which is http://localhost:8080/hello/greet. If we don't have any code that specifically invokes render, the framework will automatically look for a GSP file located in a folder with the same name as the controller under views. Create the said file using file explorer and text editor:

And use the code:
<!doctype html>
<html>
<head>
    <meta name="layout" content="main"/>
    <title>Welcome to Grails</title>
</head>
<body>
    Greetings to you all!
</body>
</html>

We will see the following output using a browser:

Install Eclipse IDE

There is an existing IDE for Grails based in Eclipse called Groovy/Grails Tool Suite or GGTS. All the required plugins for Grails development are bundled together for this package. However, Pivotal ended their development sponsorship of the Grails framework in 2015. It seems to affect also the progress of GGTS as progress halted and this tool only supports projects written in Grails 2.x.

Since the last GGTS release uses quite old plugins, it is not compatible with Grails 3.x. For example, the version of the Groovy language is not compatible with the requirements of Grails 3.x. And it is painful to make GGTS work with Grails version 3. I therefore recommend to just use Eclipse directly and ignore GGTS.

Here are the steps on how to setup Eclipse for Grails 3 development. Go to the download page of the Eclipse website and download Eclipse Mars: https://eclipse.org/downloads/

Download and extract to a directory on your computer:

Run eclipse and select the root folder where we created our first project above:

Eclipse will open with no project loaded:

Install Gradle Plugin
There is a big architectural change in Grails 3 because it is now based on Gradle. In my opinion, this is a great move forward for the framework. For Eclipse to recognize Gradle projects, we need to install a plugin. Just go to Marketplace:

And search for the Gradle plugin and install it:


Install Latest Groovy
Grails 3.1 requires Groovy 2.4 compiler. To install, go to Install New Software:

And use the update site: http://dist.springsource.org/snapshot/GRECLIPSE/e4.5/

And install Groovy 2.4 feature and the required components:

Import Existing Project
After installing Gradle and Groovy plugin, we may now import our existing project created earlier:

Choose Gradle Project type:

Browse to the root of our existing project and click Build Model:

Check the project and click Finish:

And it will open our project. Below is some screenshot of the view:


Run inside Eclipse
We may continue to run our Grails application using a command line. But if we wish to, we may also run inside Eclipse. Since Eclipse don't understand Grails based on our setup (but GGTS does), we can just use to run as External. Go to Run, External Tools and External Tools Configurations:

Create a new configuration

And fill Location using the Grails binary and the working directory should point to our project. Give it a nice name (RunHello) and use the Arguments "run-app". We may now click run:

And the Grails app will run and the console log will be shown at the bottom of the IDE:

If we wish to kill the app, just click the Terminate Button:

And if we wish to run again, there is a shortcut at the top:

Install NetBeans

NetBeans is another popular free IDE we can use for Grails development. Here are the steps on how to setup NetBeans for Grails 3 development. Go to NetBeans' website https://netbeans.org/downloads/:

Choose all and download. Install and follow instructions:

Here is how it looks like when run:

Import Project
To import our project, we may go to File and open project. Browse to our existing project and click Open Project:

And the project will be loaded to the IDE. Here are some views of the IDE:


Run Project
To run the project, just click on the Run Project button at the top:

And the console at the bottom will show the output similar to Eclipse:

Install IntelliJ IDEA Community Edition

IntelliJ IDEA is the best IDE for Grails development and also for Java in general. There is a community edition we can use but it doesn't fully support Grails. But it has support for the Groovy language.

Here are the steps on how to setup IntelliJ IDEA Community Edition for Grails 3 development. Go to JetBrains' website and download the community edition: https://www.jetbrains.com/idea/download/

Install and follow instructions:

Run the IDE and click Import Project:

Browse to our existing project:

Choose Gradle as Grails uses the Gradle build system:

This is how it will look when opened:


We may use the terminal portion at the bottom of the screen to run the application:


Install And Use Notepad++

If you do not need autocomplete functionality for Groovy and would be happy with just syntax coloring, I also recommend using Notepad++. It is a free editor that is lightweight and full of cool features. It is intended as a general purpose editor and is very simple to use.

Here are the steps on how to setup Notepad++ for Grails 3 development. Just go to their website https://notepad-plus-plus.org/ and download the installer:

Install and follow instructions:

Run it after installing. You will be presented with an editor with a blank document opened. Note that it supports tabs and many cool features. Aside from built-in features, there are many ways to customize it using plugins and other extensions:

Now let us open our project by having an explorer on the left side of the program. Go to view, project and Project Panel 1:

Add a new project:

And give it a name:

Add files from our project directory:

Point it to our project:

And our directory structure will be loaded now on the left side. This will help a lot because we don't need a separate Windows Explorer to navigate through our project source:

By default, Notepad++ doesn't have coloring as shown above. To fix this we need to download a ready made user defined language here: http://docs.notepad-plus-plus.org/index.php/User_Defined_Language_Files

Then we need to load the file:

Import it:

Select the file that we downloaded:

And it will fix our issue of color scheme.

Our editor now has colors for Groovy files to help us in coding. This program is simple but very lightweight and fast. This is good for workstations with low system requirements.
List of Tutorials for Beginners