Defining load order and dependencies to your grails plugins 0

Hello all,

I’m buiding a simple Grails Plugin that will use GORM dynamic finders and methods, but as we should know, the create-plugin grails command just install a fresh skeleton of your grails application, without any other dependencies.

First of all, you shoud install the hibernate plugin inside your plugin. Don’t worry, the hibernate will not be packaged with your plugin, but will be there to your plugin classes use.

grails install-plugin hibernate

So, now your plugin have the hibernate plugin installed and will be capable to use GORM facilities. But, if people wants to use your plugin, they will have to have hibernate plugin installed, correct? I know it cames by default in our grails application, but can be uninstalled. So we have to find some way to make our plugin dependent on hibernate plugin. Asking on the grails-user list and reading this topic in grails.org wiki, I remembered on two plugin configurations that can help us:

  • dependsOn
  • loadAfter

The dependsOn it’s a map inside our XptoGrailsPlugin that tells witch plugins our one depends, it takes the name of the respective plugin and its version, in my case, that’s what I did:

def dependsOn = ["hibernate":"1.1 > *"]

So, our plugin will depend on grails hibernate plugin, and at least the 1.1 version of it, none previous will be accepted and any future ones will be ok!

But sometimes this is not enougth, besides being dependent, our plugin uses dynamic finders, and runtime added method on our domain classes, so, it’ll only be successfully loaded after hibernate plugin load and adds this methods. To achieve this, we’ll use the other tag, the loadAfter to tell that our plugin will wait hibernate plugin to be loaded and the load itself.

def loadAfter = ['hibernate']

That’s it. Doing this we’ll make things work as we wanted.

PS.: just noticed, right now that Graeme Rocher pushed to github’s grails master branch, a commit that will install de default plugins into new plugin projetcs too. This sounds cool, and will avoid us the first step above, the plugin installation. Here is the commit id and link: 9cb23f5b835b633cf43079ca7e58e29c64bd3b3c

Discovering your grails application version 0

In many projects around, one of the requisites is that our customer should know what version of the application was running in the environment. The first approach that people reaches is to insert a comment in the HTML source code or some static page that contains this info.

Using grails, we manage our application versions using the set-version grails command as you should know. After creating your grails application, the default version your application assumes is the 0.1. After some development you’re highly recommended on changing this. There is infinite approaches on managing app versions, I really like this one:

grails set-version 20090403-1

That says this is the first (1) version of today (2009 april’s third). After that, whe you create your war, the version will be appended in the war file’s name.

Ok, now that you have your application “versioned”, you can retrieve it with this simple line:

def version = grailsApplication.metadata['app.version']

Simple and useful!!! This will get the grailsApplication reference, that can get this information for you.

Are you following me and reading my blog’s feed? Be the first to know when I publish some interesting article signing up to my feed and following me on twitter!

Quick Tip [RoR]: Ordenando valores fixos em um select 0

Quando temos um campo select em um form html usando Ruby on Rails, o procedimento ‘default’ (para selects não dinâmicos), seria algo assim….

<%= f.select :campo, {}, @valores %>

Onde @valores foi definido por um hash, por exemplo:

@valores = {'Lucas' =>'123', 'Xpto' => '111', 'Sbrobow' => '444'}

Porém, como (agora fazendo um paralelo com Java), isso refletiria algo semelhante a um HashMap e não a um LinkedHashMap, fazendo com que a ordem das entradas não seja garantida, para garantir isto, a maneira mais fácil é usar um Array de arrays, desta maneira:

@valores = [['Lucas', '123'], ['Xpto','111'], ['Sbrobow','444']]

Go!

[]s,

Lucas

Web Analytics