Monday, July 2, 2012

Grails 2.0 Get started, 1 of 3

I'm getting to be a great fan of Grails. With 2.0, the framework is ready for prime time. If you like to go through the whole documentation, you can find it here . If you prefer a video, there is a good one here .  But if you'd rather get going fast to see if this framework is for you: keep reading... below is a step by step to get you started.

  • If you don't already have it on your computer, get it here . This post is made with version 2.0.4. If you want to use Maven (which I would strongly advise for real work, you'll need to use 2.0.3
  • Obviously, since Grails is a Groovy framework, you'll need Groovy, which you can get here
  • In case you need to install it: JDK can be downloaded from here.
  • If you are using OSX, as I am, you can get it from Apple: https://developer.apple.com/
  • Well, once you are all set, a couple of quick tests from the command line should tell you the good news:
      Groovy and Grails installed
  • Now it's time to play. I'll be using IntelliJ but it has no impact on this step by step. All IntelliJ does is make it somewhat easier than the command line (this is very debatable as I was once very partial to VI but since I don't code quite as often, I like the IntelliJ environment as it is quite well thought out and caters to my laziness :-));
  1. I assume that you have a dedicated directory for your project. Mine is "workspace". Navigate to this directory
  2. grails create-app checklist. This will create a sub-directory within the "~/workspace" directory, i.e. ~/workspace/checklist. Navigate to this new directory: it will be our playground for this project;
  3. Take a look around: you'll see all the work accomplished by your hard work i.e. typing "grails create-app checklist":
    1. application.properties
    2. grails-app
    3. lib
    4. scripts
    5. src
    6. test
    7. web-app
  4. What you have is a fully functional MVC web application ready to be taylored to your needs. Go ahead and run grails run-app. You'll see that you have something that, although blend, is working.  That command will generate a web site that you can access here:
http://localhost:8080/localhost

Now, obviously, this cannot be the end of our work. As you can see, we don't have any controllers. Before we get going on this, we'll do a bit of tailoring. Back at the command line, if you want to use IntelliJ, go ahead and type:
  • ctrl-c to kill the web application (That is just for now. Later on we'll use a nice feature of Grails and modify the application while it is running);
  • grails integrate-with --intellij . This will allow us to use, you guessed it, IntelliJ. In IntelliJ, click on Open Project, navigate to your project and choose checklist.ipr. Since I'm using intelliJ 11, I get a message stating that the project is in an older format. No worries: just click yes and IntelliJ will to the rest.
What this gives you is a nive IDE envionment like this:

Your project in IntelliJ 11


In the folder grails-app, open the sub-folder conf and then the file BuildConfig.groovy . In this file, find the section plugin:
 

By default, Grails includes hibernate, jquery and resources. After the included plugins, go ahead and add (never mind the comment after the //, that's just so you know why we're adding those plugins):
  • runtime ":cached-resources:1.0" //Provides a "hash and cache" mapper for the resources framework
  • runtime ":zipped-resources:1.0" //apply gzip encoding to static resources
  • runtime ":yui-minify-resources:0.1.5" //cleans up the JS
  • compile ":cached-headers:1.1.5" //Adds methods to controllers for controlling client-cachability of the response
  • runtime ":spring-security-core:1.2.7.3" //Secure your applications using the powerful Spring Security library
"Runtime" and "Compile" pertain to the scope whee the plugin applies. From Grails documentation:

3.7.1 Configurations and Dependencies

Grails features five dependency resolution configurations (or 'scopes'):
  • build: Dependencies for the build system only
  • compile: Dependencies for the compile step
  • runtime: Dependencies needed at runtime but not for compilation (see above)
  • test: Dependencies needed for testing but not at runtime (see above)
  • provided: Dependencies needed at development time, but not during WAR deployment
You'll know (at least that is how I do) which to use by looking up the plugin. For example, cached-headers includes this information:
 
 Having been bitten more than once with other frameworks, I like to do a build to make sure that the plugins are working fine together so click on the "run" button in IntelliJ or type grails run-app and see if all is fine, which it is in my case. No need to stop the server for the moment.

Now we're ready to create our first domain object. In Grails, the heart of the application is the domain object from which we'll spawn the controller and the view. In IntelliJ, right-click on the root folder-->new-->Grails Domain Class and fill the text box with com.agileintuition.checklist.Items and add the following properties:
String name
String description


Now, create a controller for it. There is a couple of ways to do that. The first on is to follow the same path that we used for the domain object but, instead of new-->Grails Domain Class, you choose Grails controller.  Grails is big on convention over configuration so if you use the name of the domain object followed by Controller, Grails will know which domain object to use such as:
com.agileintuition.checklist.ItemsController
The other way, which we'll use, allows me to display two features in Grails: scaffolding and interactive mode.  Don't worry about using both command line and IntelliJ. the latter will synchronize itself the next time it has focus. So, on the command line, type: grails. This gets you in interactive mode. Then type "create-scaffold-controller com.agileintuition.checklist.ItemsController". Go back to your browser and refresh the page, you'll see the new controller listed. Go ahead and click on the link, you'll see once again how much work Grails has done for you i,e, it has created the CRUD views and methods for your domain object (if for some reason the application does not synchronize, just stop the server and run grails run-app to see  the magic).

I think this could be a nice place to stop. If something is not clear or could be done better, feel free to post comment.
See you soon.



1 comment:

  1. You don't need to install Groovy to use Grails, only to use Groovy by itself. Grails comes with the version of Groovy it uses.

    ReplyDelete