IntelliJ IDEA 2017.1 Help

Getting Started with Grails 1/2

This feature is supported in the Ultimate edition only.

IntelliJ IDEA tightly integrates with Grails , and makes it possible to work with Grails applications from within the IDE, sparing you from the need to use a command line. Grails support in IntelliJ IDEA lets you do the following:

Before you start

Before you start creating your Grails project, make sure that you have Grails SDK downloaded on your machine. You can download the latest SDK version from the Grails page. Also, make sure that you are working with IntelliJ IDEA ultimate edition, version 9 or higher. See the latest available version.

Creating Grails Project

Do one of the following:
  • If you are going to create a new project: click Create New Project on the Welcome screen or select File | New | Project.

    As a result, the New Project wizard opens.

  • If you are going to add a module to an existing project: open the project you want to add a module to, and select File | New | Module.

    As a result, the New Module wizard opens.

On the first page of the wizard, in the left-hand pane, select Grails. In the right-hand part of the page, specify the following setting:
  • Project SDK that you are going to use.
  • Grails SDK Home - select a local Grails installation that is represented by a library.
  • Create create-app or create-plugin - select one of these options depending on what you want to create.
  • Options - specify additional options.

Click Next.

Specify the name and location settings. For more information, see Project Name and Location or Module Name and Location.

Click Finish.

Since we chose to create an application, IntelliJ IDEA executes the create-app target, which generates the directory structure of a Grails application. All output information is displayed in the Console:

/help/img/idea/2017.1/grails_console.png

Exploring Grails Application

IntelliJ IDEA enables you to explore your Grails application from two different viewpoints:

  • The Project tool window shows the typical Grails directory structure.
  • The Grails tool window shows the logical set of Grails application elements (Domain classes, Controllers, Views, etc.)
/help/img/idea/2017.1/grails_compare_project_and_view.png

Creating Elements in your Grails Project

To illustrate IntelliJ IDEA abilities, let's start developing a very basic library management system.

  1. Create a domain class for the library system. This class will represent a book within a library.
    There are two possible ways of doing that in IntelliJ IDEA:
    • Execute a Grails target. Press ?, and enter Grails target name.
      Note that code completion ? is available in the Run Grails target dialog box:
      /help/img/idea/2017.1/run_grails_target.png
    • Right-click the Grails tool window background, and choose New | Grails Domain class on the context menu:
      /help/img/idea/2017.1/grails_new_domain_class.png
    As a result, two stub classes are created in the project:
    • Domain class Book.groovy
    • Test class BookTests.groovy
    IntelliJ IDEA diligently shows all output messages in the Console.
    For the purposes of our tutorial, we will work with the domain class Book.groovy.
    Now it is just a stub, and we'll add the following fields:
    • Book title
    • Author name (may be two author names?)
    • Description
    • Publisher
    • Date published
    • Copyright
    • ISBN
    • Reader name
    • Date taken
  2. Open Book.groovy for editing (F4), and type these fields in your code, using the code completion Ctrl+Space:
    /help/img/idea/2017.1/grails_domain_class_editor.png
  3. Provide a controller and views.
    You can do it in two ways:
    • run the Grails target generate-all Book
    • use Scaffolding - the handy tool that you can find at the top of the domain class editor:
      /help/img/idea/2017.1/grails_generate_controller.png

IntelliJ IDEA works hard (you can see that in the console), and produces the BookController.groovy class:

/help/img/idea/2017.1/grails_book_controller.png
Next, create views the same way:
/help/img/idea/2017.1/grails_generate_views.png
For each method of the controller, IntelliJ IDEA generates a file with the .gsp extension (create.gsp, edit.gsp, list.gsp, show.gsp).
/help/img/idea/2017.1/grails_view_gsp.png

Running the Application

There are more things you might want to do to make your application useful, but let's try to run it straight away with the default settings. To do that, press ? and after a turmoil of messages in the Console, your application starts in your default browser, with the following URL in the address bar: http://localhost:8080/GrailsDemo.
On that page, you will see something like this:

/help/img/idea/2017.1/grails_bookContoller_link.png
Click the controller link to open the list of books, which is empty by default. Now, you can try to fill out the entries of your library management system, for example, click the New Book button to add a book:
/help/img/idea/2017.1/grails_run_app_output.png
As you see, our basic library management system is ready. If you want to extend its functionality or you are not very happy with the code generated by Grails, you can modify the files in the IntelliJ IDEA editor to fit your particular needs, and rerun the application.
Finally, if you want to evaluate your effort for creating and running your Grails application under IntelliJ IDEA, view the number of files and lines of source code.
Press ?, type stats in the pop-up window, and see the results in the Console:
/help/img/idea/2017.1/grails_console_run.png

See Also

Last modified: 18 July 2017