Grails development in IntelliJ IDEA
- Download and install Groovy and Grails distributions
- Download and install IntelliJ IDEA 7 from www.jetbrains.com/idea/download/
- Download and install the JetGroovy plug-in using the Settings dialog in IntelliJ IDEA
- Configure the JetGroovy plug-in in the Settings dialog - set the paths to the Groovy and Grails
- Optionally download and install InspectorGroovy, a plugin containing Groovy-specific code inspections
Creating and working with a project
Create a Grails projectFigure 1 - Create new project
You start a Grails project just like any other type of project in IntelliJ IDEA. You have to select Grails Application as the project type. Once you specify project name and location, a new project with proper Grails application structure is generated.Figure 2 - The generated project structure
If you expand the individual folders you'll see their contents with different colors showing different types of content. Also all the necessary libraries have been added to the project, so that you can get intelligent assistance from the IDE.
Figure 3 - Colors indicating different content type
Editing configuration files
Although you are unlikely to spend excessive amount of time editing configuration files when working with Grails, you'll get syntax highlighting and all the Groovy code assistance when editing these files.Figure 4 - Editing configuration files
In the Project view, you can generate Grails objects either with Alt + Insert
key shortcut or with your mouse.Figure 5 - Generating Grails objects
The generated files can be opened in the editor and again you get all the Groovy-aware coding assistance. For domain classes, controllers, services, custom tag libs or other Groovy code.
Figure 6 - Coding assistance for Grails objects
Figure 7 - Code assistance for a custom TagLib
If you like graphical gadgets, you can use the graphical ER diagram designer to visualize the domain classes. Just click the Domain classes dependencies tab in the bottom of the editor window when editing a domain class to open the graphical designer. The designer allows you to update the domain classes, as well.Figure 8 - ER diagram designer
The editor is decorated with helpful buttons to quickly navigate among all the pieces related to the currently edited class.Figure 9 - Domain class editor decoration
The buttons can be used to generate the missing pieces, as well.
Of course, you can also edit the generated gsp files, again with Grails-aware coding assistance at hands.Figure 10 - Assistance inside gsp code
The gsp editor is aware of Grails tags as well as your own custom tags and provides code completion for them.Figure 11- Code completion for tags
The gsp editor is decorated, too, to allow quick navigation among different views or the other related Grails objects.Figure 13 - GSP editor navigation decorator
Using classes and libraries defined on a Grails plugin
Some Grails plugins you install will have classes you wish to extend. Take for instance the Jsecurity plugin, which provides the AbstractPermission class for permissions and JsecAuthBase for controllers. If you simply extend the controller class and attempt to run your project, Idea will raise a compile error before you run the application. You need to tell Idea where to find these sources.
The first step is to add the libraries to your project. Go to Preferences, and click on Project Settings. On the left, you'll see several options: General, Modules and Libraries will be the first three. Click on Libraries, and then the Add (+) button. Name your library, and then attach the Jar directories where the plugin libraries lie.
To add the sources, you will want to go to Preferences > Project Settings and then click on Modules. On the right you'll see your Content Root, with Source Folders and Test Source Folders under it. Besides it will be a tree of your project. Select the source directories where the plugin files are (plugins/jsecurity-0.1.1/grails-app/controllers in this example), right-click it and select Sources from the menu. This will add it to the source directories, and you will now be able to run your Grails application.
Running a Grails application
Grails applications are run through the Run Configuration just like other types of applications. You need to create a Grails Run Configuration and choose one of the three available configuration types to either run the application, tests or web tests.Figure 14 - Run Configuration set up
In Grails you may sometimes need to configure Spring directly. IDEA let's you edit the resources.xml file and gives you the full Spring code assistance you get for Spring projects in Java, including autowiring dependencies and relationship visualization. So have no fear to combine Groovy and Java Spring beans in your Grails projects.Figure 15 - Spring configuration assistanceFigure 16 - Spring configuration assistance
If you decide to specify custom hibernate mapping, you again get all the hibernate assistance available for Java projects. You can let IDEA generate hibernate entities from a database schema, check for errors in an existing hibernate configuration and provide developers with coding assistance.Figure 17 - Coding assistance in hibernate configuration filesFigure 18 - Database-aware coding assistance