10
votes

I have heard and read quite a bit on the Grails framework. I understand from their website that it's a framework designed to target the Groovy language.

Grails looks like a framework more suitable for small to medium applications than frameworks like Spring and Struts which need too many configurations to setup. But at the same time I'm quite reluctant to jump right into the Groovy language for reasons such as not mature enough, difficult to find developers, etc.

I want to know whether I can use Java instead of Groovy and yet leverage the benefits of Grails (or something that models after Rails). So, can I use the Grails framework but code in Java language? Even though Groovy seems like a superset of Java and they both can work seamlessly with each other, I can't find a place that says specifically whether or not I can use Grails and write in Java.

2
Care to backup the "not mature enough" part? I've got production code in Groovy for three years or so, and my java programmers start coding in groovy right away (though they take some time to write idiomatic groovy)Will

2 Answers

12
votes

You can, but you'll lose all benefits of Grails. Like dynamic methods, artifacts, controllers, most of plugins, etc. And I don't think that you will be able to use GORM (database mapping) from Java.

What you'll lose:

  • GORM and Domains -> you can use Hibernate instead
  • Controllers and URL Mapping -> Spring MVC
  • Services - > you can use plain old Spring Beans instead, it's basically the same thing
  • most plugins, because most of them are based on some Groovy features, dynamic methods or GORM

So, basically you'll get just standard Spring MVC app with limited GSP views and, maybe, more convenient way to configure app, easier usages of static files/resources and i18n.

PS You could always write Groovy code with Java-like syntax. And use all Grails features at this case (moving slowly to Groovy, you'll see)

PPS There is no shortage of Groovy developers, btw. Every Java developer can develop Groovy. It just takes 2-3 hours to read some introduction to Groovy for Java programmer.

3
votes

Groovy is a superset of Java, so you can write pure Java in a Groovy file, thus taking advantage of Grails' tools. The caveat is that you'd need to give your files the .groovy extension so they're processed by Groovy.

If you want to stick to pure Java, you're probably better sticking to a pure-Java framework. On the other hand, maybe you've got a great opportunity to learn Groovy. Groovy's learning curve is very shallow (after all, you can write standard Java).

Part of what makes Grails so quick and easy is its heavy use of Groovy, in particular Controllers (can) look like this:

def index = {
  def model = Person.findAllByCity("Oxford")
  respond model
} 

The biggest benefit (IMHO) here from Groovy (and why using Grails without it isn't really feasible) is the Person.findAllByCity("Oxford") method call, where GORM dynamically creates methods at runtime based on the attributes of your domain class.

It's possible to write the above in a more Java-esque way, though you can't get rid of the Groovy completely:

void index = {
  List<Person> model = Person.findAllByCity("Oxford") // This is still a dynamic method added by the Groovy MOP
  respond(model)
} 

Note that I've not tested the above code at all.