Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.Script

def src = new File('temp.clj')
src.text = '''
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
(println (factorial 4))
'''
def path = System.getProperty('user.home') + '/.groovy/grapes/org.clojure/clojure/jars/clojure-1.0.0.jar'
new AntBuilder().with {
    java(classname:Script.name, classpath:path) {
        arg(value:src.path)
    }
}

Another option is to use the Clojure API like this:

Code Block

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.Compiler
import clojure.lang.RT

def src = new File('temp.clj')
src.text = '''
(ns groovy)
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
'''

src.withReader { reader ->
    Compiler.load reader
}

def fac = RT.var('groovy', 'factorial')    
println fac.invoke(5)

There is also a Clojure plugin for Grails which provides easy access to execute clojure code from any Grails artifact (controllers, taglibs, services etc...): http://grails.org/plugin/clojure