Monday, October 19, 2009

Eclipse+Grails: Adding a 'live' plugin to the project

Once created the new project 'FirstApp' I want to be able to create a Grails plugin in the Eclipse IDE. To create a plugin is pretty straightforward, I use the command line, after positioning in the root of the current workspace, and I type:
> grails create-plugin FirstPlugin

The Grails plugin structure is almost identical to the Grails application structure. See my previous post about the Grails plugin. I am going to import in Eclipse the plugin with the classic 'Import as Existing Project'. As I did for the 'FirstApp' I can enable the Groovy capabilities right clicking on the project and selecting: Configure-> Convert to Groovy project. Also, exactly as I did for the Grails application, 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.

For testing purposes I can run the plugin as it was an application with:
> grails run-app

After checking that the plugin can run I want to use the Grails plugin 'FirstPlugin' in the Grails application 'FirstApp'. The 'classic' way would be to package the plugin by command line. After positioning in the root of the plugin:
> grails package-plugin
will create a file like grails-firstplugin-0.1.zip. After positioning in the root of the app, I can then the installation of the plugin:
>grails install-plugin ../FirstPlugin/grails-firstplugin-0.1.zip

Unfortunately, this approach is really annoying and time consuming when using the Eclipse framework. In fact, every time the plugin is changing I have to package it again and install it again.

Well, in Eclipse there is a better way to go. First of all we create the file conf/BuildConfig.groovy in the 'FirstApp' project. In this file we are going to type:
grails.plugin.location.firstPlugin = "../FirstPlugin"

This will basically tell Grails to refer to that plugin specifying the plugin folder. To test this, after creating the file, you can proceed creating a controller in the plugin. After positioning in the plugin root, I type:
>grails create-controller org.example.First

This will generate a new controller
org.example.FirstController .To prove that the BuildConfig.goovy file is working you can run the application. After positioning in the root of the application:
>grails run-app

Checking the URL: http://localhost:8080/FirstApp/ you should see the controller (of the plugin) showing up. There is still a problem though if you create a second controller (or a change in the code of the plugin), this will not show up in the application unless you restart the server. This might become extremely annoying on the long run.

The solution to that is the creation of a soft link. We are going to create a link to the plugin project inside the code of the application. First I create the directory plugins in the FirstApp project:
>mkdir plugins

Then, in that directory, I am going to create the link to the FirstPlugin:
>ln -s ../../FirstPlugin

Right. Now, if you followed correctly (...and if I have not forgot any step), while the server is running, you should be able to create a new controller in the 'FirstPlugin' and see it in the list of the controllers in the running application: http://localhost:8080/FirstApp/

One more thing, remember to refresh the eclipse project every time you make use of a Grails command that creates/modify files.

No comments: