2 Getting Started - Reference Documentation
Authors: Andres Almiray
Version: 1.2.0
Table of Contents
2 Getting Started
This chapter will help you get started with Griffon by explaining how to download and install the project, how to use Griffon from within your favorite IDE, and how to write your first application.2.1 Downloading and Installing
The first step to getting up and running with Griffon is to install the distribution. To do so follow these steps:- Download a binary distribution of Griffon and extract the resulting zip file to a location of your choice
- Set the GRIFFON_HOME environment variable to the location where you extracted the zip
- On Unix/Linux based systems this is typically a matter of adding something like the following
export GRIFFON_HOME=/path/to/griffon
to your profile - On Windows this is typically a matter of setting an environment variable under
My Computer/Advanced/Environment Variables
- Now you need to add the
bin
directory to yourPATH
variable: - On Unix/Linux base system this can be done by doing a
export PATH="$PATH:$GRIFFON_HOME/bin"
- On windows this is done by modifying the
Path
environment variable underMy Computer/Advanced/Environment Variables
griffon
in the terminal window and see output similar to the below:
Welcome to Griffon 1.2.0 - http://griffon-framework.org/
Licensed under Apache Standard License 2.0
Griffon home is set to: /usr/local/griffon-1.2.0
No script name specified. Use 'griffon help' for more info
2.2 Creating an Application
To create a Griffon application you first need to familiarize yourself with the usage of thegriffon
command which is used in the following manner:griffon [command name]
griffon create-app demoConsole
cd demoConsole
2.3 A Groovy Console Example
The "create-app" target created a Griffon MVC Triad for you in the models, views, and controllers directory named after the application. Hence you already have a model class DemoConsoleModel in the models directory.The application model for this application is simple: it contains properties that hold the script to be evaluated and the results of the evaluation. Make sure you paste the following code intogriffon-app/models/democonsole/DemoConsoleModel.groovy
.package democonsoleimport groovy.beans.Bindableclass DemoConsoleModel { String scriptSource @Bindable def scriptResult @Bindable boolean enabled = true }
griffon-app/controllers/democonsole/DemoConsoleController.groovy
.package democonsoleclass DemoConsoleController { private GroovyShell shell = new GroovyShell() // these will be injected by Griffon def model def view def executeScript = { evt = null -> model.enabled = false def result try { result = shell.evaluate(model.scriptSource) } finally { model.enabled = true model.scriptResult = result } } }
evt
parameter, and the default value so it can be called without an action event.Finally, the Griffon framework can be configured to inject portions of the builders it uses. By default, the Threading classes are injected into the controller, allowing the use of the edt
, doOutside
and doLater
methods from SwingBuilder.You may notice that there's no explicit threading management. All Swing developers know they must obey the Swing Rule: long running computations must run outside of the EDT; all UI components should be queried/modified inside the EDT. It turns out Griffon is aware of this rule, making sure an action is called outside of the EDt by default, all bindings made to UI components via the model will be updated inside the EDT also. We'll setup the bindings in the next example.The view classes contain the visual components for your application. Please paste the following code into griffon-app/views/democonsole/DemoConsoleView.groovy
.package democonsoleapplication(title:'DemoConsole', pack:true, locationByPlatform:true, iconImage: imageIcon('/griffon-icon-48x48.png').image, iconImages: [imageIcon('/griffon-icon-48x48.png').image, imageIcon('/griffon-icon-32x32.png').image, imageIcon('/griffon-icon-16x16.png').image]) { panel(border:emptyBorder(6)) { borderLayout() scrollPane(constraints:CENTER) { textArea(text:bind(target: model, 'scriptSource'), enabled: bind { model.enabled }, columns: 40, rows: 10) } hbox(constraints:SOUTH) { button("Execute", actionPerformed: controller.executeScript, enabled: bind { model.enabled }) hstrut 5 label 'Result:' hstrut 5 label text: bind { model.scriptResult } } } }
griffon run-app
This command should compile all sources and package the application, you'll see a similar result as depicted by the following screenshot after a few seconds
2.4 Getting Set-up in an IDE
IntelliJ IDEA
IntelliJ IDEA and the JetGroovy plug-in offer good support for Groovy/Grails/Griffon developers. Refer to the section on Groovy and Grails support on the JetBrains website for a feature overview.Integrating an existing Griffon project
IntelliJ 10 and newer supports Griffon form the get go. You can open existing projects as well as create new ones. However you can also create the required IntelliJ project files by running the following command:griffon integrate-with --intellij
Creating a new Griffon project
Follow these steps to create and run a new Griffon project with IDEA#1 Bring up the "New Project" wizard. You should see Griffon as one of the available options



NetBeans
A good Open Source alternative is Oracle's NetBeans, which provides a Groovy/Griffon plugin that automatically recognizes Griffon projects and provides the ability to run Griffon applications in the IDE, code completion and integration with Oracle's Glassfish server.Integrating an existing Griffon project
NetBeans does not require any special integration support, it understands the layout of a Griffon project as long as the Griffon plugin is installed. Just select "Open" from the menu and locate the folder that contains your project. It's that simple. Follow these steps to install the Griffon NetBeans plugin.Prerequisites: Java, Groovy and Grails plugins installed and up to date.#1 Download the pluginFollow this link to download the latest zip distribution of the plugin.#2 Unpack the zip file into a directory of your choosing#3 Open the plugin manager dialog. Go to the "Downloaded" tab, then click on the "Add Plugins..." button. Locate and select the NBM files that were uncompressed in the previous step.#4 Select both plugins (using the checkboxes) and click on "Install".
Creating a new Griffon project
Prerequisites: You must have the Griffon plugin installed. Follow the steps explained in the previous section to get the job done.#1 Bring up the "New Project" wizard. Click on "Groovy" then on "Griffon Application".



Eclipse
We recommend that users of Eclipse looking to develop Griffon application take a look at SpringSource Tool Suite, which offers built in support for Groovy.Integrating an existing Griffon project
To integrate Griffon with Eclipse run the following command to generate appropriate project files:griffon integrate-with --eclipse
griffon install-plugin eclipse-support




Running Griffon commands within Eclipse
We'll rely on Eclipse's Ant support to get the job done, but first we need to generate an Ant build filegriffon integrate-with --ant

TextMate
Since Griffon' focus is on simplicity it is often possible to utilize more simple editors and TextMate on the Mac has an excellent Groovy/Griffon bundle available.Follow these steps to install the Groovy bundle#1 Create a local bundle directory
mkdir ~/Library/Application Support/TextMate/Bundles/
#2a If you have git installed then just clone the repository
cd ~/Library/Application Support/TextMate/Bundles/
git clone https://github.com/textmate/groovy.tmbundle.git
#2b Alternatively download a copy of the latest version from github as a zip and unpack it. Rename the unpacked directory to groovy.tmbundle
.Follow these steps to install the Griffon bundle#1 Create a local bundle directory
mkdir ~/Library/Application Support/TextMate/Bundles/
#2a If you have git installed then just clone the repository
cd ~/Library/Application Support/TextMate/Bundles/
git clone https://github.com/griffon/griffon.tmbundle.git
#2b Alternatively download a copy of the latest version from github as a zip and unpack it. Rename the unpacked directory to griffon.tmbundle
.Now configure the PATH
environment variable within TextMate. Make sure that $GRIFFON_HOME/bin
in expanded form is set
Integrating an existing Griffon project
To integrate Griffon with TextMate run the following command to generate appropriate project files:griffon integrate-with --textmate
mate .

Running Griffon commands within TextMate
The Griffon bundle provides new commands under the "Bundles" menu. Search for the "Griffon submenu".

Sublime Text 2
To integrate Griffon with Sublime Text 2 run the following command to generate appropriate project files:griffon integrate-with --sublimetext2
Project
menu, locate the generated project file and open it. You should get a screen similar to the next one
2.5 Convention over Configuration
Griffon uses "convention over configuration" to configure itself. This typically means that the name and location of files is used instead of explicit configuration, hence you need to familiarize yourself with the directory structure provided by Griffon.Here is a breakdown and links to the relevant sections:griffon-app
- top level directory for Groovy sources.conf
- Configuration sources.models
- Models.views
- Views.controllers
- Controllers.services
- Services.resources
- Images, properties files, etc.i18n
- Support for internationalization (i18n).scripts
- Gant scripts.src
- Supporting sources.main
- Other Groovy/Java sources.test
- Unit and integration tests.
2.6 Running an application
Griffon applications can be run in standalone mode using the run-app command:griffon run-app
griffon run-applet
griffon run-webstart
2.7 Testing an Application
Thecreate-*
commands in Griffon automatically create integration tests for you within the test/integration
directory. It is of course up to you to populate these tests with valid test logic, information on which can be found in the section on Testing. However, if you wish to execute tests you can run the test-app command as follows:griffon test-app
build.xml
which can also run the tests by delegating to Griffon' test-app command:ant test
2.8 Creating artefatcs
Griffon ships with a few convenience targets such as create-mvc, create-script and so on that will create Controllers and different artifact types for you.These are merely for your convenience and you can just as easily use an IDE or your favorite text editor.There are many such
create-*
commands that can be explored in the command line reference guide.