How do you configure GroovyConsole, so I don’t need to import libraries at startup?

I have a groovy script that uses a third-party library. Every time I open the application and try to run my script, I need to import the desired library.

I would like to be able to open GroovyConsole and run my application without having to import the library.

+6
classpath import groovy groovy-console
source share
4 answers

On Linux, you also have

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

Here you can add your specific libraries:

 # 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 

Hope this helps, I had to look for a long time to find this (:

+8
source share

If you just want to add JARs to the classpath, copy (or symbolically) them to ~/.groovy/lib (or %USER_HOME%/.groovy/lib on Windows).

If you want the actual import statements to run each time the Groovy Console starts, edit the groovy -starter.conf file as suggested by Squelsh.

+6
source share

You can write an external Groovy script that does all the import, creates a GroovyConsole object, and calls the run () method on that object.

See also http://groovy.codehaus.org/Groovy+Console#GroovyConsole-EmbeddingtheConsole

For example: 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() 

The Groovy executable is called from the shell script, giving it the Groovy script:

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

Now, GroovyConsole code can now use the import made in start.groovy, as well as from variables created and passed using the setVariable ("service" method in the example).

+2
source share

At least on Linux groovy GroovyConsole - this Script has the following command:

 startGroovy groovy.ui.Console " $@ " 

startGroovy is a Script that runs Java. Inside startGroovy Script, you can change your classpath and add missing libraries.

From the beginning Groovy:

 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" \ " $@ " fi 
+1
source share

All Articles