Description
The Clojure plugin enables compiling and running Clojure code on your Griffon application. This plugin is heavily influenced by its Grails counterpart, even borrows some of its code.
Installation
The current version of griffon-clojure-plugin is 0.4
To install just issue the following command
griffon install-plugin clojure
Usage
You must place all Clojure code under $appdir/src/clojure, it will be compiled first before any sources available on griffon-app or src/main which means you can't reference any of those sources from your Clojure code, while the Groovy sources can. You will be able to use the LangBridge Plugin facilities to communicate with other JVM languages.
Sources placed under $appdir/src/clojure will generate Java classes using macros provided by Clojure, for example
$appdir/src/clojure/griffon/clojure/Greet.clj
(ns griffon.clojure.Greet
(:gen-class
:methods [[greet [String] String]]))
(defn -greet
([this greetee]
(str "Hello " greetee "!")))
(defn -main
[greetee]
(println (.greet (griffon.clojure.Greet.) greetee)))
This will generate a Java class named griffon.clojure.Greet, which can be used inside any Griffon artifact, for example a Controller
import griffon.clojure.Greet class MyController { def action = { evt = null -> doOutside { def greeter = new Greet() def output = greeter.greet("Griffon") // do something with output } } }
Starting from version 0.2 you will be able to load clojure scripts at any time. By default all scripts placed at $basedir/griffon-app/resources/clj will be loaded when the application boostraps itself. For example griffon-app/resources/clj/demo.clj might look like this:
(ns grails) (defn add_numbers [a b] (+ a b))
With that code in place, the add_numbers function may be executed as a method call on a dynamic property named clj from a Griffon controller. See below:
class FooController {
def addNumbers = { evt = null ->
// invoke the function as a method on the
// clj dynamic property...
model.z = clj.add_numbers(model.x, model.y)
}
}
The dynamic property will be named clj by default. The name of the property may be set explicitly in griffon-app/conf/Application.groovy by assigning a value to the griffon.clojure.dynamicPropertyName property.
griffon.clojure.dynamicPropertyName = 'clojurePropertyName'
For most applications, the default name of clj should be fine. You can also alter in which artifacts the property gets injected, by default only controllers will have that property. See griffon-app/conf/Application.groovy and look for the following entry
griffon.clojure.injectInto = ["controller"]
Finally, you can load any Clojure script by calling cljLoad(String path) where path will be resolved using Spring's PathMatchingResourcePatternResolver. The default path used during bootstrap is "classpath*:/clj/*/.clj". It is also worth mentioning that this method will be injected to all artifacts controlled by griffon.clojure.injectInto and that the prefix clj will be affected by griffon.clojure.dynamicPropertyName.
History
| Version | Date | Notes |
|---|---|---|
| 0.4 | 01-01-10 | Upgraded to Clojure 1.1 |
| 0.3 | 11-15-09 | Added fixes from grails-clojure-plugin. Added repl and create-script/class scripts |
| 0.2 | 09-10-09 | Enable loading of clojure scripts at any time. Check timestamp of compiled sources to decide if recompile is needed |
| 0.1 | 07-28-09 | Initial release |


