2009-05-04 17 views

Trả lời

1

Ít nhất trên Linux groovy GroovyConsole là một Script có lệnh sau:

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

startGroovy chính nó là một kịch bản mà bắt đầu Java. Trong kịch bản startGroovy bạn có thể sửa đổi classpath của bạn và thêm các thư viện còn thiếu.

Từ 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

Trong Linux, bạn cũng có

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

Ở đây bạn có thể thêm libs cụ thể của bạn:

# 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 

Hy vọng nó giúp, phải tìm thời gian dài để tìm điều này (:

6

Nếu bạn chỉ muốn thêm các JAR vào đường dẫn lớp, sao chép (hoặc liên kết tượng trưng) chúng vào ~/.groovy/lib (hoặc %USER_HOME%/.groovy/lib trên Windows).

Nếu bạn muốn báo cáo thực tế import chạy mỗi khi Groovy Console khởi động, hãy chỉnh sửa tệp groovy-starter.conf như được đề xuất bởi Squelsh.

2

Bạn có thể viết một kịch bản Groovy bên ngoài thực hiện tất cả các lần nhập, tạo ra một đối tượng GroovyConsole và gọi phương thức run() trên đối tượng này.

cũng http://groovy.codehaus.org/Groovy+Console#GroovyConsole-EmbeddingtheConsole

Xem Ví dụ: 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() 

Từ một kịch bản shell gọi thực thi groovy cung cấp nó với kịch bản hấp dẫn:

#!/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 

Các mã trong GroovyConsole bây giờ có thể tận dụng việc nhập khẩu được thực hiện trong start.groovy, cũng như các biến được tạo và được truyền bằng phương thức setVariable ('dịch vụ' trong examp le).