Search

Dark theme | Light theme

October 18, 2011

Groovy Goodness: Run Remote Scripts via URL

Since Groovy 1.8.3 we can run remote Groovy scripts. We can use an URL to refer to the Groovy script that we want to execute. This can be very useful to build a library of Groovy scripts and publish them on a web server or a code repository like Github. Then we can run those scripts by referring the scripts by URL.

// File:
// http://www.mrhaki.com/samples/remotesample.groovy

printMessage 'Remote Groovy script @ http://www.mrhaki.com/samples/remotesample.groovy'

['Groovy', 'rocks'].each {
    print "${it.toUpperCase()} "
}
println '!'

def printMessage(message) {
    def LINE_LENGTH = message.size() + 4
    println '*' * LINE_LENGTH
    println "* $message *"
    println '*' * LINE_LENGTH
}

On a command line we run $ groovy http://www.mrhaki.com/samples/remotesample.groovy and we get the following output:

$ groovy http://www.mrhaki.com/samples/remotesample.groovy
****************************************************************************
* Remote Groovy script @ http://www.mrhaki.com/samples/remotesample.groovy *
****************************************************************************
GROOVY ROCKS !
$