2009-05-04 7 views

risposta

1

Almeno su Linux Groovy GroovyConsole è uno script ha il seguente comando:

startGroovy groovy.ui.Console "[email protected]" 

startGroovy per sé è uno script che inizia Java. All'interno dello script startGroovy dovresti essere in grado di modificare il tuo classpath e aggiungere i librerie mancanti.

Da startGroovy:

startGroovy () { 
    CLASS=$1 
    shift 
    # Start the Profiler or the JVM 
    if $useprofiler ; then 
     runProfiler 
    else 
     exec "$JAVACMD" $JAVA_OPTS \ 
      -classpath "$STARTER_CLASSPATH" \ 
      -Dscript.name="$SCRIPT_PATH" \ 
      -Dprogram.name="$PROGNAME" \ 
      -Dgroovy.starter.conf="$GROOVY_CONF" \ 
      -Dgroovy.home="$GROOVY_HOME" \ 
      -Dtools.jar="$TOOLS_JAR" \ 
      $STARTER_MAIN_CLASS \ 
      --main $CLASS \ 
      --conf "$GROOVY_CONF" \ 
      --classpath "$CP" \ 
      "[email protected]" 
    fi 
8

In Linux si hanno anche

/usr/share/groovy/conf/groovy-starter.conf 

Qui è possibile aggiungere le librerie specifiche:

# load user specific libraries 
load !{user.home}/.groovy/lib/*.jar 
load /home/squelsh/src/neo4j-community-1.4.M03/lib/*.jar 
load /home/squelsh/src/neo4j-community-1.4.M03/system/lib/*.jar 

Speranza che aiuta, dovuto cercare molto tempo per trovare questo (:

6

Se si desidera semplicemente aggiungere i JAR al classpath, copiare (o symlink) su ~/.groovy/lib (o %USER_HOME%/.groovy/lib su Windows).

Se si desidera che le istruzioni effettive di import vengano eseguite ogni volta che si avvia Groovy Console, modificare il file groovy-starter.conf come suggerito da Squelsh.

2

È possibile scrivere uno script Groovy esterno che esegue tutte le importazioni, crea un oggetto GroovyConsole e chiama il metodo run() su questo oggetto.

Vedi anche http://groovy.codehaus.org/Groovy+Console#GroovyConsole-EmbeddingtheConsole

Ad esempio: start.groovy

import groovy.ui.Console; 

import com.botkop.service.* 
import com.botkop.service.groovy.* 

def env = System.getenv() 
def service = new ServiceWrapper(
    userName:env.userName, 
    password:env.password, 
    host:env.host, 
    port:new Integer(env.port)) 

service.connect() 

Console console = new Console() 
console.setVariable("service", service) 
console.run() 

Da uno script chiamare l'eseguibile scanalato fornendo con lo script scanalato:

#!/bin/bash 

if [ $# -ne 4 ] 
then 
    echo "usage: $0 userName password host port" 
    exit 10 
fi 

export userName=$1 
export password=$2 
export host=$3 
export port=$4 

export PATH=~/apps/groovy/bin:/usr/bin:$PATH 
export CLASSPATH=$(find lib -name '*.jar' | tr '\n' ':') 

groovy start.groovy 

Il codice in GroovyConsole ora può fare uso delle importazioni fatte in start.groovy, così come delle variabili create e passate con il metodo setVariable ('servizio' nell'esempio le).