Grails for PHP developers part 3

Now where were we? Oh yes. We’ve seen some of the basic features/reasoning that attracted me, some of the gotchas about dealing with the lack of $_SERVER, and then visited some basic looping syntax with a “hello world” thrown in for good measure. Can’t be a platform series without a “hello world”, right? I will veer yet again in to something somewhat unrelated to that which came before, and talk about the set up of Grails. I can’t give you too many more examples without having you have a working Grails system, so let’s get started.

Getting started

Grails assumes very little about your system, but it does assume a few things. It assumes that you have Java on your system already, which is a job in itself, or can be if you’ve never done it before. If you are anything like I was, and indeed still am, you’re wary of all the SJAs out there (“Sun Java Acronyms”). What’s the difference between a Java SDK, JVM, JRE, JEE, J2EE and other Java acronyms? It’s sometimes hard to decipher at first. This quote from Sun’s ‘Java.com’ site – apparently intended for consumers, states:

Java software for your computer, or the Java Runtime Environment, is also referred to as the Java Runtime, Runtime Environment, Runtime, JRE, Java Virtual Machine, Virtual Machine, Java VM, JVM, VM, or Java download. (from this page)

If that’s not enough to make your head spin, I dunno what is. The Grails ‘installation page’ states that you need a “Java SDK” of 1.4 or higher. The Java page at sun.som – java.sun.com – doesn’t have anything called an SDK. However, they do have a “JDK” – Java SE Development Kit – here. Agree to all the licensing stuff, download that and install. There are a number of choices, windows, linux or solaris, and self-extracting file vs RPM (on linux). There’s more options here than I can reasonably address, but at the end of the day you should be able to type

‘java -version’

at a command line and get something like this:

java version “1.7.0”
IcedTea Runtime Environment (build 1.7.0-Mandriva-19.202.56.2mdv2008.0)
IcedTea Client VM (build 11.0-b05, mixed mode)

Yeah, I’m running the IcedTea version of Java which is a bit ahead of the curve. Your output will probably say something about ‘Sun’ and ‘1.6’ and such. If so, you’re good to carry on. If not, email me and I’ll see what I can do to get you help.

Installing Grails

Once Java is set up on your system, Grails is comparatively a piece of cake. Go to the Grails download site and grab the most recent version. Right now it’s 1.0rc4 – hopefully 1.0 final will be available when you’re reading this. Once you’ve grabbed the .zip or tar.gz file, unzip it. You’ll find an ‘INSTALL’ file in the unzipped directory, which has instructions for Unix and Windows systems.

One of the things I’ve found helpful to do is to put those commands in to my .bash_profile file so that the environment variables that get set up are always correct. Another handy thing I’ve found is to keep the GRAILS_HOME environment variable always pointing to ‘/grails’ (without the version number) and to create a symbolic link between the ‘grails’ name and the specific version I’m using (grails-1.0-rc4, for example). I never have to change my environment variables then, just the symlink.

The current INSTALL instructions for a Unix environment look something like this:

> set GRAILS_HOME=/path/to/grailsdir
> export GRAILS_HOME
> cd ${GRAILS_HOME}
> chmod a+x bin/grails
> set PATH=${GRAILS_HOME}/bin:${PATH}
> export PATH

My .bash_profile has these lines in it:

GROOVY_HOME=/apps/downloads/groovy
GRAILS_HOME=/apps/downloads/grails
JAVA_HOME=/usr/lib/jvm/java-1.6.0-sun
JDK_HOME=/usr/lib/jvm/java-1.6.0-sun
export GRAILS_HOME
export JAVA_HOME
export JDK_HOME
export GROOVY_HOME
PATH=/usr/bin:”${PATH}”:${JAVA_HOME}/bin:${GROOVY_HOME}/bin:${GRAILS_HOME}/bin

The GROOVY_HOME is not necessary for Grails to run. Grails has its own Groovy language contained in it. I just have a separate Groovy interpreter for other development work.

Once this is done, you should be able to type

grails

at a command line and get some guff about using ‘grails help’ instead. If you type ‘grails help’ you’ll get a full list of all the commands available.

grails help

Running script /apps/downloads/grails/scripts/Help.groovy
[mkdir] Created dir: /home/michael/.grails/1.0-RC4/tmp

Usage (optionals marked with *):
grails [environment]* [target] [arguments]*

Examples:
grails dev run-app
grails create-app books

Available Targets (type grails help ‘target-name’ for more info):
grails bootstrap
grails bug-report
grails clean
grails compile
grails console
grails create-app
grails create-controller
grails create-domain-class
grails create-integration-test
grails create-plugin
grails create-script
grails create-service
grails create-tag-lib
grails create-unit-test
grails doc
grails generate-all
grails generate-controller
grails generate-views
grails help
grails init
grails install-plugin
grails install-templates
grails list-plugins
grails package
grails package-plugin
grails plugin-info
grails release-plugin
grails run-app
grails run-app-https
grails set-proxy
grails set-version
grails shell
grails stats
grails test-app
grails upgrade
grails war

There’s quite a lot of functionality we can have grails do for us. I’ll touch on many of these in the coming articles.

The ‘grails’ command itself invokes a number of supplied scripts to manage the creation and updating of your Grails application. This is somewhat analogous to the Symfony framework’s ‘symfony‘ command line file ‘symfony’ or CakePHP’s ‘bake‘ command. I’m not an expert in those projects, but the concept of command line driven app generation seems common these days, likely all springing from Ruby On Rails. Grails implements this same strategy.

Start a Grails app

To get a Grails application started, you simply need to run the ‘create app’ task with an application name and Grails will create all the internal structure necessary for your app to be functional.

grails create-app test

will yield

Welcome to Grails 1.0-RC4 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /apps/downloads/grailsBase Directory: /home/michael/tmp
Environment set to development
Note: No plugin scripts found
Running script /apps/downloads/grails/scripts/CreateApp.groovy
[mkdir] Created dir: /home/michael/tmp/test/src
[mkdir] Created dir: /home/michael/tmp/test/src/java
[mkdir] Created dir: /home/michael/tmp/test/src/groovy
[mkdir] Created dir: /home/michael/tmp/test/grails-app
[mkdir] Created dir: /home/michael/tmp/test/grails-app/controllers
[mkdir] Created dir: /home/michael/tmp/test/grails-app/services
[mkdir] Created dir: /home/michael/tmp/test/grails-app/domain
[mkdir] Created dir: /home/michael/tmp/test/grails-app/taglib
[mkdir] Created dir: /home/michael/tmp/test/grails-app/utils
[mkdir] Created dir: /home/michael/tmp/test/grails-app/views
[mkdir] Created dir: /home/michael/tmp/test/grails-app/views/layouts
[mkdir] Created dir: /home/michael/tmp/test/grails-app/i18n
[mkdir] Created dir: /home/michael/tmp/test/grails-app/conf
[mkdir] Created dir: /home/michael/tmp/test/test
[mkdir] Created dir: /home/michael/tmp/test/test/unit
[mkdir] Created dir: /home/michael/tmp/test/test/integration
[mkdir] Created dir: /home/michael/tmp/test/scripts
[mkdir] Created dir: /home/michael/tmp/test/web-app
[mkdir] Created dir: /home/michael/tmp/test/web-app/js
[mkdir] Created dir: /home/michael/tmp/test/web-app/css
[mkdir] Created dir: /home/michael/tmp/test/web-app/images
[mkdir] Created dir: /home/michael/tmp/test/web-app/WEB-INF/classes
[mkdir] Created dir: /home/michael/tmp/test/web-app/META-INF
[mkdir] Created dir: /home/michael/tmp/test/lib
[mkdir] Created dir: /home/michael/tmp/test/grails-app/conf/spring
[mkdir] Created dir: /home/michael/tmp/test/grails-app/conf/hibernate
[propertyfile] Creating new property file: /home/michael/tmp/test/application.properties
[copy] Copying 2 files to /home/michael/tmp/test
[copy] Copying 2 files to /home/michael/tmp/test/web-app/WEB-INF
[copy] Copying 5 files to /home/michael/tmp/test/web-app/WEB-INF/tld
[copy] Copying 87 files to /home/michael/tmp/test/web-app
[copy] Copying 16 files to /home/michael/tmp/test/grails-app
[copy] Copying 1 file to /home/michael/tmp/test/grails-app/conf/spring
[copy] Copying 1 file to /home/michael/tmp/test
[copy] Copying 1 file to /home/michael/tmp/test
[copy] Copying 1 file to /home/michael/tmp/test
[propertyfile] Updating property file: /home/michael/tmp/test/application.properties
Created Grails Application at /home/michael/tmp/test

Holy cow that’s a lot of stuff that just happened! Grails has created the entire skeleton of our project for us, again similar to many current PHP frameworks. This is where the ‘convention’ part of convention over configuration thinking comes in to play. Grails expects files which certain types of functionality to be in certain folders. All grails functionality is in the ‘grails-app’ directory, and in there we have, among other things, directories for ‘controllers’, ‘domain’ and ‘views’.

Coming up

If you’ve got this far, congratulations! I think I’d like to introduce the idea of domains next, and walk you through that, as it’s one of the most powerful aspects of Grails, in my opinion. I’m also not aware of anything quite like it in the PHP world (but if there is something like it, please let me know!). So, plan on some hands on walk throughs on domains, databases and scaffolding in the next few installments. Thanks for reading so

Be Sociable, Share!