Creating a simple Grails application with IntelliJ IDEA
From IntelliJ-Wiki
Contents |
Overview
IntelliJ IDEA tightly integrates with Grails http://www.grails.org, and makes it possible to work with Grails applications from within the IDE, sparing you from the need to use command line. Grails support in IntelliJ IDEA helps
- Create a Grails application using the New Project wizard.
- Explore a Grails application as a tree-view of files and folders, or as a collection of models, controllers, and views.
- Create elements in your Grails application (domain classes, controllers, and views).
- Run your Grails application.
This tutorial will walk you step-by-step through developing and launching a simple library management system.
Prerequisites
- You are working with IntelliJ IDEA Ultimate edition, version 9 or higher. See http://www.jetbrains.com/idea/index.html
- Grails SDK is installed on your machine. You can download Grails SDK from http://grails.org/Download
Creating a new project
Let’s start from the very beginning, that is, create a new project from scratch. To do that, choose File | New Project on the main menu, or click the New Project icon on the Welcome screen. On the first page of the New Project wizard, make sure that the option Create project from scratch is selected:
You are going to create a project that already contains a Grails module. So, on the next page of the wizard, you have to specify your project name, location of the project files, module type (Grails application), location of the content root and module file:
Now, let IntelliJ IDEA know where the Grails SDK resides. You can choose one of the SDKs that already exist on your computer, or define a new one:
Finally, click Finish. The project is almost ready, and IntelliJ IDEA will ask you whether you want to create a Grails library on the project level – for the current project only, or on the global level, to make it available to other Grails projects:
To create a Grails application, click the Run ‘create-app’ button. This will launch the create-app target:
IntelliJ IDEA executes the create-app target, which generates the directory structure of a Grails application. All output information is displayed in the Console:
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.)
See the difference:
The project is now ready. All you have to do is to add some meaningful contents to it, which will be described in the next section.
Creating elements in your Grails project
To illustrate IntelliJ IDEA's abilities, we'll start developing a very basic library management system. To start with, let’s create a domain class for that 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 Ctrl+Alt+G, and enter Grails target name. Note that code completion Ctrl+Space is available in the Run Grails target dialog box:
- Right-click the Grails tool window background, and choose New |Grails Domain class on the context menu:
As a result, two stub classes are created in the project:
- Domain class Book.groovy
- Test class BookTests.groovy
Note that IntelliJ IDEA diligently shows all output messages in the console. For the purposes of our short guide, we’ll continue working with the domain class Book.groovy. Now it is just a stub, and we want to add the following fields to it:
- Book title
- Author name (may be two author names?)
- Description
- Publisher
- Date published
- Copyright
- ISBN
- Reader name
- Date taken
Open Book.groovy for editing (F4), and type these fields in your code, using the code completion (Ctrl + Space):
The next step is to provide a controller and views. Again, you can do it in two ways: either run the Grails target generate-all Book, or use Scaffolding - the handy tool that you can find at the top of the domain class editor:
IntelliJ IDEA works hard (you can see that in the console), and produces the BookController.groovy class:
Next, create views the same way:
For each method of the controller, IntelliJ IDEA generates a file with the .gsp extention (create.gsp, edit.gsp, list.gsp, show.gsp).
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 Shift+F10 – after turmoil of messages in the Console, your application starts in your default browser, with the following URL in the address bar: http://localhost:8080/GrailsAppProject1/.
On that page, you will see something like this:
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:
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 Ctrl+Alt+G, type stats in the pop-up window, and see the results in the Console:
More information
Managing Grails plugins: http://wiki.jetbrains.net/intellij/Managing_Grails_plugins

















