Feb 15, 2011

Start Using Eclipse with Grails Support via STS

This old dog is caving in and trying to learn new tricks. I’m behind the curve, it seems, when it comes to embracing Grails and Groovy, as I’m still vigorously clinging to the old reliable, Spring framework. Today I open my arms to getting the ball rolling with new adventures in Grails, starting with integrating the Grails support in Eclipse.

Torey Lomenda wrote a great getting-started article (http://www.objectpartners.com/2009/05/27/eclipse-setup-for-grails-11-development/) for configuring an Eclipse IDE with the then-latest tools for Grails. It’s been a while, and a lot of new things have been released, making the environment easier to start working with. I’m going to try to not repeat too much of what Torey said, but I’m sure there’s likely to be plenty of overlap.

Java Required

It’s assumed you’ve got a JDK installed on your system, so I won’t go into that. If you haven’t, hop over to http://java.oracle.com/ and grab the JDK for your environment, installing as it makes sense for the download you select. There are both expand-and-use and installer versions available.

It’s a little weird for those developing on MacOS, as Apple’s moved away from supporting the JDK. Right now, you can still download one from their developer’s area, if you can find it (http://developer.apple.com/java). Quite popular is the SoyLatte port of the BSD-licensed JDK (http://landonf.bikemonkey.org/static/soylatte/). You can also check out http://openjdk.java.net/projects/macosx-port/ to see how the JDK7 port to MacOS is coming.

Getting Started: The (Slightly) Harder Way

One way to go is to start with a clean copy of Eclipse and add the plug-ins necessary. This is also the way to go if you’ve already got a running copy of Eclipse and just want to add the Grails parts to it. I also prefer starting with Eclipse from that source, as it has some of the additional features like the new marketplace. For the sake of understanding the tools involved a little better, let’s look at this method first.

Start with Eclipse

If you haven’t got it already, or you want to start fresh, visit http://www.eclipse.org and you’ll find the latest is Helios (version 3.6, for the number fans) Service-Release 1. I recommend grabbing the IDE for JEE developers, as it has all of the nice WTP (Web Tools Platform) plug-ins for JSP development and interacting with servers already packaged; you can opt for a smaller install, but many of these tools will be added as dependencies anyway. Unpack the archive when the download finishes, and run the appropriate “eclipse” executable in the folder it creates. Installation, done; make shortcuts if you desire.

A quick tip, the default JVM that Eclipse uses is the first JRE on your path, not the JDK, even if that’s the first on your path or what’s configured as your JAVA_HOME. To change this, edit the eclipse.ini file and add two lines “–vm” and “/full/path/to/your/jdk” together, somewhere before the -vmargs; I’ve added mine after the “-startup” and first “plugins/…” lines, making them lines three and four in my eclipse.ini file. Grails and Maven both will appreciate this.

Add the Spring Tool Suite

Once Eclipse is installed, we’ll add the Spring Tool Suite, which gives us the foundation for the latest Grails plug-ins (at least as far as we’re concerned for this discussion). Visiting the STS site at http://www.springsource.com/developer/sts shows us that the current version is 2.5.2. There are a handful of ways to get the STS installed, as the documentation you can download from the STS site will show. I’ve found the easiest and most reliable is to use the Eclipse “Install New Software” tool, from the Help menu.

Some will surely point out that STS is available as an add-on from the Eclipse marketplace (also in the Help menu of Helios). While your mileage may vary, I was unable to get this to work on a any of my systems while testing for the article, on either Linux or Windows, while downloading from the “Install New Software” worked every time.

Technically, what happened is that STS would install nicely, but neither of the Grails options discussed below wouldn’t install or work after installing.

Start Eclipse, hit the Help menu, and pick “Install New Software.” Click the Add button on the Install dialog box that opens. Enter the URL http://dist.springsource.com/release/TOOLS/market-place/e3.6/ (but don’t bother visiting that with a web browser, as you’ll get nothing but access denied messages…). A set of things will be listed as available from the site, but only a few need to be selected for the base STS install, and this will meet the needs of our Grails environment. Choose the following:

  • Core / dm Server Tools
  • Core / Spring IDE
  • Core /STS
  • Extensions (Incubation) / Spring IDE
  • Extensions /Spring IDE
  • Integrations /Spring IDE

With these selected, finish the install by agreeing to the information in the dialog boxes, and clicking Next and Finish as appropriate.

When the installation is complete, Eclipse will restart, and STS will be available.

Getting Started: The Easier Way

Rather than start with a raw Eclipse install and add the STS and Grails by hand, one can visit the STS download page from http://www.springsource.com/developer/sts and choose the appropriate installer. This will install a re-branded Eclipse with all of the STS tools already packaged. The re-branding means the splash screen and some of the menus and such are different. It also comes with a different packaging of Maven and Roo and the Spring Tomcat server; separate instead of as Eclipse plug-ins.  There are a few versions to choose from, but the easiest to discuss is the archive (download and unpack) version. After exploding the archive, a springsource folder should be created and contain a number of folders, the more important for this discussion is the sts-2.5.2.RELEASE. Inside that folder is the STS executable as appropriate for your OS.

Starting the STS application will show the green STS splash screen welcoming you to STS.

Adding Grails

Note that Grails does not come installed by default with STS, no matter which way it is installed.

If you’ve chosen the Eclipse installation with STS added as outlined in the hard way above, Grails support is included, but Grails is not. Visit http://grails.org/ and download the latest (currently 1.3.6), and unpack this somewhere on your system. Then in Eclipse, visit the Windows menu Preferences item, find Groovy item and then Grails in the list, click the Add button on the right side, and finally use the Brows button to browse to where Grails was unpacked. Finishing this action will allow full use of the STS Grails tools!

If you’ve chosen the STS installation  as outlined in the easy way above, Grails support is not included and must be added. There are two ways to add Grails support.

If you already have Grails installed, or choose to download and install it as above, then you can add Grails support by use of the SpringSource Tool Suite Extensions. The default view in STS in the Spring perspective (until you start changing your workspace around), and the Dashboard should be open. If it isn’t open, can be opened by picking Dashboard from the Help menu. On the bottom of the dashboard is an Extensions tab. Selecting the Extensions tab will open a mini-market of add-ons. Select the item “Grails Support” and hit the install button at the bottom. When the installation finishes, find the Groovy and Grails items in the Windows preferences and add the Grails installation as outlined above.

If you haven’t got Grails installed, and don’t want to download it directly, either for the Eclipse or straight-up STS installation, visit the Extensions page, as described above, and find “Grails (current production release).” This will install Grails for you (in your home directory, in a grails-1.3.6 folder), add Grails Support, and make the Grails installation entry to the properties outlined in the paragraph above. Note this will also work if you already have Grails downloaded and installed, but you may end up with a new copy of the SDK, or overwriting the one in your  home folder if you’re not careful.

Grails Support Quirk

It might just be me (but probably isn’t), but it seems that the Grails support is defined per-workspace, not with the Eclipse installation. When switching workspaces, I’ve noticed that the Grails SDK isn’t populated in the Workspace Preferences. The Grails Support doesn’t need to be re-added, but for each new workspace that needs Grails support, the preferences will need to be visited and the SDK found and added.

A quick trick: once Grails support is installed, visiting the Grails configuration can easily be done when adding a Grails project to the workspace. Next to the box where the Grails installation can be chosen is a link to open the Grails installation preferences window. This is a handy shortcut to know if you find yourself in a workspace that hasn’t been configured with the Grails installation, and you forget to configure that before trying to start a new project.

Speaking of new projects, the Grails installation is now complete! Time to get started.

Cursory Examples Of Using Grails

Once STS and Grails support are installed, and the Grails installation is configured in Eclipse, many of the Grails bits that used to require accessing command lines are now embedded in the usual Eclipse tools.

The Grails Perspective

There’s a Grails perspective that brings some of the Grails menu items and toolbars with it. When active, the File menu New item has Grails Project right at the top of the list. Using the Package Explorer or Navigator views have Grails Project in their context menu. Curiously, the context menu in Project Explorer does not, and you have to hit the “other” project dialog to find Grails Project. In the context menu, and File menu New item, are also items for making Grails classes, like domain classes, controllers, and services.

Most of these menu items are also available in the JavaEE perspective, with the same view limitations, in case that’s your favorite.

Starting A New Grails Project

Creating a Grails project in Eclipse configures the Java Build Path with the Grails libraries. It creates the file structure as you would expect when using “grails create-app APPNAME” including  the grails-app, lib, scripts, src, test, and web-app folders. Additionally, it creates the Eclipse specific bits, as if with “grails integrate-with –eclipse” and then importing the new Eclipse project into your workspace.

Importing An Existing Grails Project

If you’ve got existing Grails projects, you can easily import them, and the Grails tools will add the missing Eclipse parts. Start as if creating a new grails project, but in the dialog box, choose the folder containing the existing source. If necessary, the Grails tools will add the necessary Eclipse parts, or will use them as with any other pre-existing Eclipse project.

Creating Groovy Classes

Creating Groovy classes is as easy as right-clicking on the project and selecting New, and then the appropriate type of class. This includes domain classes, services, tests, and controllers. Selecting “New” and then “Domain Class” will create the .groovy script in the grails-app/domain folder, while Controller will create a grails-app/controllers script, and so on. Creating domain classes, services, and controllers will automatically generate test classes of the appropriate name in the test/unit folder.

The Eclipse views help everyone see the project in their favorite way, and in the same fashion as with other Eclipse projects. In the Navigator view, the raw directory structure is shown. In the Project Explorer view, separate tree elements for each entity will provide an aggregated view. All of the domain classes end up under the domain tree node, for example. In the Package Explorer view, the different aggregations are represented as the source folders, so that those domain classes are shown in the grails-app/domain node of the tree. It takes just a moment to get comfortable with one of the views and the layout representing the Grails environment.

Create Domain Class

With the appropriate Grails project selected, right-click and select New from the menu, and then select Domain Class. The Domain Class wizard is pretty primitive, expecting just a name. Upon entering a name, the Groovy script is run for you, and the domain class is created in the project’s grails-app/domain folder, with the appropriate path as provided. The name can contain a fully-qualified path as necessary, but if the wizard is provided a name without fully-qualifying it, the class will be created with the package name the same as the project, but in all lower-case.

The wizard doesn’t offer any opportunity to add members, so the Groovy script created will have a mostly empty class generated like the following example class that gets created when providing just the name “DomainClass” to the wizard:

package projectname

class DomainClass {
  static constraints = {
  }
}

This will also create a test class in the test/unit folder, in a file named DomainClassTests.groovy. The auto-generated test classes have simple setUp(), tearDown(), and testSomething() methods. Running the tests is as simple as right-clicking on the class or folder in the view, selecting “Run As” and then “Grails Command (test-app).”

Adding fields is done by editing the DomainClass.groovy file (with the actual name of your class, of course). Adding a couple frivolous fields, and we get a class that looks like the following:

package projectname

class DomainClass {
  static constraints = {
  }

  String someString
  Date someDate
  Long someLong
}

Create Controllers

Once the domain class is created, a corresponding Controller can be created using a keyboard shortcut “Alt-g, c” (discussed below); there are also tool bar buttons to do this, when an appropriate class is opened in a Groovy editor. The wizard will populate with the fully-qualified name of the domain class, and create a controller class like the following example:

package projectname

class DomainClassController {
  def index = { }
}

Like the domain class, this will also create a test class, with the file name DomainClassControllerTest.groovy, and with the same simple starter methods.

Much of the navigation between related Grails items can be done using the hot keys using the Alt+g combination. Hitting Alt+g will pop-up a handy menu, to make it easy to find the related target. For example, creating (or otherwise jumping to) our related controller from the domain class would be done with Alt+g, c; that is hold Alt, hit g, release Alt, and hit c. Controllers are c, domain classes are d, GPSs are g…it’s not too hard to remember.

And when the focus is on a Groovy class, the Groovy tool bar elements appear, giving you some of the very common links to the domain class, controller, service, taglib, and GSP that are related to the current editor.

As with all Grails tutorials, we can complete the trivial app by adding the automatic scaffolding to the class, as below:

package projectname

class DomainClassController {
  def scaffold = DomainClass
}

Running The Trivial App

Right-clicking on the project and selecting “Run As” and then “Grails Command (run-app)” will build the classes and fire up the web server. Checking the console, you’ll see the URL as something like http://localhost:8080/projectname and visiting that site will give the default Grails looking site, with a link to your DomainClassController. Clicking that will give you the default CRUD pages with our trivial fields for someString, someDate, and someLong.

Summary

Some would argue that the other IDEs have much better integration. This may be true for some or all aspects of Grails support. However, if you’re already comfortable with the editing, plug-ins, and other tools offered by Eclipse, adding STS with Grails support is a pretty quick and easy way to start jumping in the the Groovy/Grails game.

About the Author

Object Partners profile.

One thought on “Start Using Eclipse with Grails Support via STS

  1. Another resource is http://grails.org/STS+Integration/ which the Groovy-Eclipse guys maintain and keep updated fairly regularly

  2. Torey Lomenda says:

    As the author of the original “Setting up Eclipse for Grails Development” I can happily say that the latest STS makes that article obsolete.

    I have only started using it, but with the Grails perspective, built in command support, and other features, STS has made Eclipse a good option for Grails development IDE.

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]