Switching to Eclipse/STS For Grails Development

Post by Matt Nohr

My preferred tool for doing Grails development is the SpringSource Tool Suite (STS), which is built on the free Eclipse IDE that you download with a number of plugins already installed. It is totally free and has a number of nice features you do not get with a simple text editor.

Why Switch?

Why should you use STS? Since STS is built on Eclipse, you get a number of nice features built in. Here are just a couple of my favorites:

Outline View:
STS outline view

Content-assist / Autocomplete:
STS Content Assist

Automatic refactoring tools (like renaming all instances of a variable):
STS refactor rename

And many more:

  • Support for groovy, gsps, java, css, javascript, and many more
  • Auto formatting of files including fixing indents and spacing
  • Highly configurable workspace (tabbed editing, split-pane editing, task lists, etc)
  • Huge Plugin community (source control, other languages, etc)


Installation

Assuming you are already doing grails development, installation is very straight-forward. Follow the instructions on the grails.org site here: http://grails.org/STS+Integration

If you are starting from scratch, you will have to install Java and probably specific versions of Grails before you can install STS.

Out of the box, STS is geared towards Spring development. Since Grails runs on top of Spring, after you install STS you have to install a couple plugins for Groovy and Grails. It is all detailed in the grails.org post. Assuming you already have Grails installed on your computer, you do not need to install the Grails language extension. You will just need the Grails and Groovy “tooling extensions.”

Initial Setup

Once STS is installed and open, here are a couple of things to get started.

First, setup your Grails installations if you did not do that during the installation. Go to the Preferences (MacOS: SpringSource Tool Suite > Preferences, Windows/Linux: Window > Preferences) and then navigate to Groovy > Grails. Just browse to your grails directory and STS will fill in all the details for you. You can do this for as many versions of Grails as you want. The default will be used for new projects, but you can always specify which version to use for a specific project.

Next, switch to the Grails perspective and you are ready to go.
STS Grails Perspective

Importing Existing Project

To pull your existing Grails project into STS, just follow these steps

  1. File > New… > Grails Project
  2. Under Contents, change it to “Use External Location” and Browse to your existing Grails project

STS Import Project
There are a couple things that may now happen, depending on which version of Groovy and Grails you are using.

First, you could get a “Incompatible Groovy Compiler Version” warning
STS Import Error 1
In my case I was importing a Grails 1.3.7 project and my Groovy version was 1.8.6, which is more recent than Grails 1.3.7 uses. Most of the time I just ignore this message because I do not have any problems with using so-called “incompatible” versions. However, you can easily switch to a different version of Groovy if needed. Go to the Preferences > Groovy > Compiler. In here is a button to switch versions of Groovy and a link with more details.

Second, you could get a “Grails version mismatch detected” warning
STS Import Error 2
In my case, the default Grails in STS was 2.0.3 and my project was 1.3.7. If you say “Yes” it will run “grails upgrade” on your project. If you say “No” it will configure you project to use the correct version, 1.3.7 in my case. You can see what version of Grails is configured for a project by right-clicking (control-click on a Mac) on the project name and choosing Properties then Grails.

Third, you could get a “Grails version x.x.x not installed” warning
STS Import Error 3
As you can guess, this means the version of Grails specified by your project is not installed in Grails. You can either upgrade your project through STS (clicking Yes) or go back and configure STS to use that version of Grails.

Features of STS with Grails

Project Explorer:
Since Grails uses “convention over configuration”, STS can easily identify your domain, controller, view, service, etc classes and will group them accordingly:
STS Project Explorer

Running Grails Commands:
Built into STS is the ability to run any Grails command, just like you would from the command line. Some are tightly integrated and others not so much. For example, you can do File > New > Domain Class (or Controller, or Service, etc) which will run “grails create-domain-class MyDomain” automatically.

You can also run any other Grails command with the “Grails Command” button in the toolbar:
STS Grails Run Button
This will bring up a prompt and you can enter any command:
STS Grails Run Dialog
If you enter run-app like I did, it will bring up a server within STS and you can click on the URL in the console view. That said, I typically still run the application from the command line out of habit.
STS Run App

You can still run commands from the command line. Just remember to refresh your project in STS if you are creating any new files.

Note on Source Control

Using STS will create a new target-eclipse directory in your project and may put some items in the .settings directory. You will probably want to exclude these from your source control (using .gitignore or similiar)

This entry was posted in Software Development and tagged , , . Bookmark the permalink.

Related Posts:

5 Responses to Switching to Eclipse/STS For Grails Development

  1. AGo says:

    STS is good but IntelliJ IDEA has got much, much better support for GRAILS and GROOVY.
    I use IDEA at work and STS at home because STS is cost free.
    In my opinion STS SHOULD look for a bunch of super features IDEA have and … simple copy them.

  2. Pingback: Blog bookmarks 04/13/2012 « My Diigo bookmarks

  3. Matt Nohr says:

    There is also a free version of IntelliJ IDEA, though it does not to appear to have Grails (or Spring, Hibernate, or some other) support.

  4. Sasi says:

    I tried to install grails 1.3.4 by choosing its path (i.e C:\grails1.3.4). But getting “Specified directory does not appear to be a Grails installation in sts” error message. Any suggestions will be appreciable.

  5. Matt Nohr says:

    Sasi- Where are you trying to set the Grails installation? It should be under Preferences > Groovy > Grails and then add a new installation that points to the grails folder you downloaded.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>