Plugin page: http://artifacts.griffon-framework.org/plugin/clojure
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.
You must place all Clojure code under $appdir/src/clojure
, it will be compiled first before any
sources available in 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 ->
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 bootstraps itself. For example
griffon-app/resources/clj/demo.clj
might look like this:
(ns griffon)
(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/Config.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/Config.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
.
- create-clojure-script - creates a new Clojure script in
$basedir/griffon-app/resources/clj
. - create-clojure-class - creates a new Clojure (macro based) class in
$basedir/src/clojure
. - clojure-repl - executes a Clojure REPL with the application's classpath fully configured.