Did you know that you can have a complete modular enterprise application up and running in under 5 minutes with Grails? The architecture will be a lot more flexible and maintainable than any “Enterprise Java” structure with Ant or Maven too!
First things first, how many different modules should an application have? Well, every application probably needs three essential things these days: website, mobile website and API. Wait, why do you need an API if you don’t necessarily have any plans of sharing data? Well, the answer is you might need native Android and iPhone apps in the future, so you might as well plan for them right away. The HTML5 frameworks are getting pretty good though! Take a look at Sencha Touch if you haven’t yet as it’s pretty cool! So why can’t you just use one project for the API and the website? Well, I don’t recommend having a controller that returns JSON or HTML based on the request. It seems cluttered to me plus you might want to return more or less data given the target and have a completely different security setup for each (basic, digest or token for API vs forms for web). Also, versioning the API is a lot easier if it’s separate from the website. Plus as you’ll see in a bit, it’s really easy with Grails to separate out the API into a separate project and reuse code.
First, create a parent folder for the entire application. Then create three separate Grails applications in this parent folder.
- grails create-app sampleapp-web
- grails create-app sampleapp-api
- grails create-app sampleapp-mobile
Next, create a Grails plugin for the core part of the application. This is where you’ll put all reusable code for your application like domain classes and services.
- grails create-plugin sampleapp-core
Now we need to set a reference to our plugin in each of the three Grails applications we created. This is slightly different from installing regular Grails plugins. Since this plugin is going to be changing a lot as you develop the application, you don’t want to be constantly packaging and installing the plugin into each of the three apps. So, instead add this line to the end of grails-app/conf/BuildConfig.groovy for each of the three (web, api and mobile).
- grails.plugin.location.’sampleapp-core’ = “../sampleapp-core”
Now you can add a class to “core” at any time and it’s automatically available to all three projects! You never have to compile the core plugin, but rather when you run or test any of the three main apps, it will automatically pick up changes made in core. Pretty slick, huh! We’re not done yet though! You obviously want to test all three at the same time, right? Ha! Ok, maybe not, but I do for some reason! Plus I like to run other stuff on 8080 anyway, like Jenkins for example, so let’s add this line to grails-app/conf/BuildConfig.groovy so that they run on different ports and there’s no conflict on port 8080.
- grails.server.port.http = 8081
- grails.server.port.http = 8082
- grails.server.port.http = 8083
Next, I’m never nearly as good of programmer as I pretend to be, so I always add the code review plugin!
- grails install-plugin codenarc
Finally, the last thing actually has nothing to do with setting up an application, but rather is just cause I’m kind of lazy, ha! Let’s shorten the typing required for run-app and test-app scripts. This may seem really trivial, but think about how many times you’ll type these commands, makes sense to make them as short as possible, doesn’t it? Plus, it will also show how cool our setup is by being able to reuse scripts across all four Grails projects! You could also create scripts that are available to all Grails projects on your computer, but I think it’s better to keep each application completely independent.
Run the following command while in the core plugin (sampleapp-core):
- grails create-script ra
- grails create-script ta
This will create two files in sampleapp-core/scripts. Erase everything from these two files and add this:
- sampleapp-core/scripts/Ra.groovy: includeTargets << grailsScript(“RunApp”)
- sampleapp-core/scripts/Ta.groovy: includeTargets << grailsScript(“TestApp”)
With these two scripts setup in core, you can now simply execute grails ra or grails ta instead of grails run-app and grails test-app in any of the four Grails projects!
Well, I hope you enjoyed my Grails Setup article! It certainly is a better setup than any crazy stuff I used to do with Ant or Maven! Let me know if you have any suggestions for ways to improve it. Happy coding!