Last updated by 4 years ago

Page: STS Integration, Version:6

The STS and Groovy team have recently announced the release of some basic Grails support in STS. In this tutorial, I am going to introduce this support and describe how to create your first Grails project in STS. This includes how to install Grails support, how to create a new Grails project, how to import an existing Grails project, and how to use the Grails tooling to execute Grails commands.

Grails tooling requires Groovy tooling, and we have just released Groovy-Eclipse 2.0.0-M1, so feel free to explore that as well.

Install

To begin, install STS 2.2.0, which is freely available for download here. After installing and running, you will be greeted with a welcome screen:

At the bottom, you will see the Extensions tab:

Select that you will find several available STS extensions, including Groovy and Grails support:

Choose Groovy and Grails support. Then follow the steps to install and restart STS. After restarting, you will now have Groovy and Grails tooling support available.

Project creation

To create a new Grails project click on:

File -> New -> Other

And find the Grails Project wizard

Click _Next_:

Uh oh...we don't have any Grails installations configured. No worries. Let's do that now. Select Configure Grails Installations_, click _Add and choose a Grails installation folder. You can choose the Grails installation that comes with STS, or you can choose your own version:

Click Finish and wait for the Grails command to be completed. You have now created your first Grails project!STS+Integration/

STS+Integration/

Importing an Existing Grails Project

To import an existing Grails project, use the standard Eclipse import wizard for existing projects. One way that you can find it by right clicking anywhere in the Package Explorer and selecting Import..._:

Choose _Existing Projects into Workspace_:

Select the root directory containing your Grails project or projects (the import wizard can be used to import multiple projects at once):

Then click _Finish_. Your project will be imported. Uh oh, there are errors:

This is because, the Grails tooling support for Eclipse is not configured properly when a Grails app is created (we expect to have this fixed in future releases). You must explicitly add the Grails nature to the project. Right click on the project or projects you imported and select (in the _Configure sub-menu):

And now you have successfully imported your project into Eclipse.

Using Grails Tooling

If you haven't already done so, you must point Eclipse to a Grails runtime. You can do this in the Eclipse preferences page:

All Grails projects are also Groovy projects. So, open any Groovy file in your Grails project and you will see the Groovy support. Syntax highlighting, content assist, outline views are all available:

To execute a Grails command, go to the Navigate menu:

Or, while in a Groovy file press CTRL-G (or CMD-G on Macs). Now the Grails command prompt appears:

Content assist is available CTRL-Space (or CMD-Space on Macs):

Completing the command and pressing enter will kick off a the Grails command in the console (this particular command created a domain class):

After Grails completes the execution of the command, the workspace will be refreshed and your new domain class (or whatever you have just created) is available for editing:

Grails tooling will manage your classpath. The classpath is refreshed automatically when the project is created or imported. However, when the Grails classpath changes, such as when you add or remove plugins, you must explicitly schedule an Eclipse classpath refresh:

Running and Testing

STS supports you in running your Grails apps. Execute the {{run-app}} command:

And the console will run the app for you, providing you with a URL that you can navigate to:

Similarly, for testing, executing the {{test-app}} command will execute all of your project's unit and integration tests:

Closing words

And that is Grails support in Eclipse. We will be working hard on enhancing Grails support in future versions of STS. There are quite a few features that we will be providing in the future. This includes gsp editing support, Grails-specific content assist and quick fixes in the Groovy editor, fully automated management of plugins, and a more tightly integrated Grails shell.

Here is the support information: