Monday, October 19, 2009

Eclipse+Grails: Setting up a new project

Here is my current setup for creating a Grails project with Eclipse:
  1. Installed Eclipse jee 3.5 Galileo SR1 (download)
  2. Installed SVN plugin Subclipse (Update site: http://subclipse.tigris.org/update_1.6.x)
  3. Installed Groovy plugin (Update site: http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.5/)
  4. Installed Ivy plugin (http://www.apache.org/dist/ant/ivyde/updatesite)
I am then going to setup the launcher for the Grails commands through the "External Tools Configuration". I am specifying the 'location of Grails" (i.e. /Users/paolociccarese/Library/Grails-1.1.1/bin/grails), then ${project_loc} as 'Working directory' (that means the command will be related to the selected project in the Eclipse workspace. Finally I define as 'Arguments': ${string_prompt} . This will allow me to run the Grails commands like I would do from the command line. To be honest I also use the command line when I am managing grails projects, for some operations is more practical.

Finally I create the GRAILS_HOME variable in Eclipse->Preferences->Java->Build Path->Classpath Variables. This is used by Eclipse to compile the project. You may notice that GROOVY_HOME and IVY_HOME have already been created by the plugins.

Now I want to create a new Grails application that I will call 'FirstApp'. For doing this I will use the command line and, after positioning myself in the root of the current workspace, I type:
> grails create-app FirstApp

This will create the structure of my new application (in the newly created directory: FirstApp). I can now import this structure as 'Import as Existing Project' in my workspace. I can enable the Groovy capabilities right clicking on the project and selecting: Configure-> Convert to Groovy project.

Now if you right-click on the project and select: Properties->Java Build Path you will notice tha all the libraries are linked in a subdirectory of GRAILS_HOME. This is not my solution as I want to be dependent from the ivy.xml file. To do so, I select the ivy.xml file, I right-clik and select 'Add Ivy Library...'. This will link, through ivy, all the libraries necessary to the newly created Grails application to compile and run. Once performed this step I can safely remove all the libraries linked in the subdirectory GRAILS_HOME from the Properties->Java Build Path. You should also notice a new icon in the toolbar with tooltip 'Resolve All Dependencies'. You can use that icon to retrieve new dependencies that you specified in the ivy.xml file.

Now I can test if the application is actually running. I select a file in the project and I run the external application 'Grails' that I previously created. A window will pop up and we can fill out the text box with the command 'run-app'. The alternative would have been to use the shall and, after positioning in the root of the project, type:
> grails run-app

If everything went right we can now browse: http://localhost:8080/FirstApp/ getting back the Grails welcome screen.

No comments: