Grails Applications for Java Shops

Grails and Java together

I knew I liked Grails, but I wasn't ready to throw away my Java comfort blanket yet. I still wanted my Service and Repository tiers that I knew just worked.

But I also liked Grails dynamic reloading of controllers, i18n messages, commands etc.

Is there a hybrid I can use?

The answer turns out to be YES.

It's not as clean as it needs to be, but it's getting there. And reading a recent blog post and one reply got me thinking there are other people who want to do this.

So here's an example of how to do it with Grails 1.1.1.

Download Example Code
Example source code can be downloaded here
You can build the example using mvn clean install from the top level basic-grails-java-integration directory.
You can then run the example using mvn grails:run-app from the web sub directory. The application should then be running at http://localhost:8080/web
Grails 1.2
Grails 1.2 has offered improvements (most notably around being able to configure an external hibernate.cfg.xml file for Grails to use) and probably others that I don't know about yet. I'll let you know when I migrate to 1.2.

Adopting Grails - a phased approach

So you're in a Java shop. You know Java very well. You like Spring. Maybe you use Spring MVC - even Webflow. You've heard about Grails - looks cool.

But you can't use it all yet. Your boss will freak out if you tell him you're moving to something called 'Groovy'. How can you adopt Grails in stages? Well the approach we landed on is to use Grails controllers, services and repos and domain objects initially.

All Grails - no Java

We got going very fast. We were cranking out screens but our standard coverage tool is Clover, and build tool was maven. Support for Grails with these tools just wasn't up to scratch. I felt very uncomfortable not reporting my Clover stats like the other Java teams. I didn't want to say our team was 'special' - needing to switch to Cobertura for reporting.

But Cobertura reporting in maven with aggregated reports etc. it just doesn't work. Clover doesn this very nicely (as long as you use clover2:setup and a temporary local maven repo so it doesn't overwrite your productions jars with clovered versions).

Next the generated schema was not coming out right. We wanted more control so we migrated our Groovy domain objects over to Java hibernate annotated pojos. That worked nicely, however we had to move all or none. Grails doesn't support mixed mode domain objects.

I spoke to Graeme Rocher about it and he told me they'd have to refactor the core of Grails Hibernate support to get this working - not a trivial thing.

Also I'd moved away from the service / repository paradigm the team were used to - this was uncharted territory.

I needed my Java comfort blanket!

Domain objects, Repositories and Services in Java

We ended up moving all these tiers into Java.

If I had my time again I'd just write the whole domain model in Java Hiberate Annotated Pojos to begin with. You don't really lose anything important but you gain full control over your generated data model.

Now I had my comfort blanket again. I was testing using spring-test libraries again, getting clover coverage reports like everyone else, having full control over the schema etc. and even writing my unit/integration tests in Groovy! Nothing like writing tests to improve one's Groovy.

Groovy JUnit Test Warning
If you download IntelliJ 9 community you can run Groovy Unit tests in situ for free. I have not got this to work in Eclipse / STS. My team members who use Eclipse are not happy. I have IntelliJ 8 Pro so my life is happy

My controllers, commands and gsps are all Grails with dynamic reloading etc. This still gives me a lot of productivity. I also like that I get a dynamic persistence behaviours still with Java annotated pojos. I can still play with prototyping a service in Groovy before moving it over to Java later where I'll start again using TDD. I could/should be able to write the services TDD style in Groovy before moving them to Java but I just haven't got to grips with all Grails testing tooling yet.

I also really like web flow in Grails. The DSL is just so easy to use.

Architectural Summary

So I've got the following architectural layers

===========grails====================
-----------gsps----------------------
-----------controllers---------------
-----------commands------------------
===========grails====================
===========java======================
-----------services------------------
-----------repositories--------------
-----------domain objects------------
===========java======================

I use grails built in support for external Spring beans to wire everything together. One sticking point at the moment is that Grails demands its own sessionFactory and datasource - even if you have no intention of using the GORM capabilities in Grails.

So you have to give it something to shut it up (say an in memory data source that you'll never use). But other than that you can happily create an external datasource with your services etc. and use that.

Note that in my example I cheat - I use the grails demanded datasource to set up a database. I then access that same database using an externally configured datasource.

In my production code I have a separate database project that sets up a real database which I connect to with the external datasource. I have Grails pointing to a ne'er used in-memory database.

And finally

Maven integration is provided and was questioned by someone commenting on the blog post. Well this does work - sort of, but it could be a lot better.

The problem is that the Grails/Groovy guys are a lot like the code they write - very opinionated. Graeme Rocher has never hidden the fact he hates Maven with a passion, which is fine. Sometimes I get annoyed with Maven - mostly because of the ridiculously long maven release process, but the fact is many, many Java developers use Maven.

If Grails / Groovy is really going to take off it needs to start attracting Java developers - so Maven support needs to be improved, but for now it kind of works.

It would be great to see more Java developers starting to use Grails. I'm much more productive with it than the two other Web frameworks I've used (Struts, Spring MVC).

I'd also like to see some of these integration oddities address (like Grails running in a mode where it didn't always expect an internal datasource).

Hopefully this has inspired you to try using Grails with your existing Java applications/Java skills.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Feb 11, 2010

    Anonymous says:

    The fact is many, many do not use maven. probably more don't than do, so having ...

    The fact is many, many do not use maven. probably more don't than do, so having maven support will not necessarily bring in more java developers.

    You can uninstall the GORM plugin as well, if you don't intend on using it.

    1. Feb 11, 2010

      Neil Thorne says:

      I do still intend using GORM and find it useful for prototyping as I mentioned "...

      I do still intend using GORM and find it useful for prototyping as I mentioned "I also like that I get a dynamic persistence behaviours still with Java annotated pojos. I can still play with prototyping a service in Groovy before moving it over to Java later...".

      I for one would appreciate tighter maven integration but then I don't expect it.

      I just think it's a shame if Maven guys are put of using Grails because they can't see how they'd integrate it.

    2. Feb 13, 2010

      Neil Thorne says:

      Here's just one stat for maven usage vs ant usage - suggests maven usage is slig...

      Here's just one stat for maven usage vs ant usage - suggests maven usage is slightly less than half the number of those using ant.

      So probably more don't than do - but that's still a lot of people who do.

      http://www.indeed.com/trendgraph/jobgraph.png?q=maven+java,ant+java

  2. Feb 11, 2010

    Anonymous says:

    How do you modulize your project and manage your dependency without maven? check...

    How do you modulize your project and manage your dependency without maven? checking in jar to your scm is so old school.

Add Comment