Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:groovy

Dies ist eine alte Version des Dokuments!


Groovy

Getting Help

Install

Download the binary and unpack it to /usr/local and make a symlink: /usr/local# ln -s groovy-1.6.5 groovy

Add groovy bin directory to your invironment variables:

# /etc/profile.d/jdk.sh
...
export GROOVY_HOME=/usr/local/groovy
export PATH="$PATH:$GROOVY_HOME/bin"

Tools

groovyConsole is a GUI app for evaluating Groovy scripts and groovysh is an interactiove CLI app for evaluating Groovy expressions.

groovy ist the interpreter for Groovy scripts (see below) and groovyc is the compiler which compiles Groovy scripts to java classes (see below)

Script Files

Shebang:

#!/usr/bin/env groovy
println "Hallo Sandy!"

A script automatically creates a class which has the same name as the filename of the script:

#!/usr/bin/env groovy
// Filname: groovytest
println this.class      // prints: class groovytest
println getClass()      // prints: class groovytest
println this.getClass() // prints: class groovytest

The first version is a class literal and seems to be a shortcut for calling getClass().

println seems to call implizitely toString() on any object. (This is the same as System.out.println() in standard java.):

println this.class            // prints: class groovytest
println this.class.toString() // prints: class groovytest

The parent class of the autogenerated script class is groovy.lang.Script:

println this.class.getSuperclass() // prints: groovy.lang.Script

println() is a method of the autogenerated script class (inherited from groovy.lang.Script). It can be called without this. and braces. This seems to be added automatically, because the following call prints: …Exception: No signature of method: groovytest.println() is applicable for argument types: (java.lang.Integer, java.lang.Integer)…

#!/usr/bin/env groovy
// Filename: groovytest
println 0,0

Compile Groovy Scripts

Groovy scripts can be compiled to java class files with groovyc. In order to run those classes with the normal java command, you either have to include /usr/local/groovy/embeddable/goovy-all-1.6.5.jar to the CLASSPATH variable or better :?:1) make a symlink in the Java Installed Extensions directory:

cd /usr/lib/java/jre/lib/ext
ln -s /usr/local/groovy/embeddable/goovy-all-1.6.5.jar

Note that this would have to be done on every system which wants to run a compiled script. Therefore it is better to create a jar file wich already includes the necessary depcendencies. This is described here.

Groovy and Java Web Start

FIXME

Groovy and Apache Pivot

FIXME

Dynamic Typing

def is a replacement for a type name e.g. in method signatures. It is used to indicate that you don't care about the type. You can omit def in global variables of a script:

def seven=7;   println "seven.class: "+ seven.class // -> class java.lang.Integer 
int eight=8;   println "eight.class: "+ eight.class // -> class java.lang.Integer
nine=9;        println "nine.class: " + nine.class  // -> class java.lang.Integer
 
seven="seven"; println "seven.class: "+ seven.class // -> class java.lang.String
//eight="eight"; println "eight.class: "+ eight.class // GroovyCastException!!!
nine="nine";   println "nine.class: " + nine.class  // -> class java.lang.String

Collections: Lists, Ranges and Maps

#!/usr/bin/env groovy
myList = [1976, 1969] // create a list
myList << 2010        // append something to the end
println myList        // => [1976, 1969, 2010]
println myList[2]     // => 2010
println myList[3]     // => null
println myList.size   // => 3
println myList.class  // => class java.util.ArrayList

Each list creates an implementation of java.util.List. Hence the complete API of List is available:

println myList.get(0) // => 1976
println myList.size() // => 2

More on lists: usage examples, added methods to the standard List interface

Unsorted

Open Questions / To do

1)
to be tested!
Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/groovy.1268242622.txt.gz · Zuletzt geändert: 2010-03-10 17:37 von becki

Impressum - Datenschutzerklärung