Last active
December 21, 2016 04:03
-
-
Save aalmiray/5395913 to your computer and use it in GitHub Desktop.
Custom Script subclass. Example taken from http://zeroturnaround.com/labs/scripting-your-java-application-with-groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// zt.Engine.java | |
package zt; | |
public interface Engine { | |
void start(); | |
void stop(); | |
} | |
// zt.EngineScript.java | |
package zt; | |
import groovy.lang.Script; | |
public abstract class EngineScript extends Script implements Engine { | |
} | |
// engine.groovy | |
void start(){ | |
println "Start the engines!" | |
} | |
void stop(){ | |
println "Stop at once!" | |
} | |
this | |
// runner.groovy | |
import zt.* | |
import org.codehaus.groovy.control.CompilerConfiguration | |
// create a configuration | |
def config = new CompilerConfiguration() | |
// tweak the configuration | |
config.scriptBaseClass = 'zt.EngineScript' | |
// run your script | |
def shell = new GroovyShell(config) | |
engineScript = shell.evaluate(new File('engine.groovy').text) | |
assert engineScript instanceof Engine | |
engineScript.start() | |
engineScript.stop() | |
// run it with | |
javac -d classes -cp $GROOVY_HOME/embeddable/groovy-all-<version>.jar zt/* | |
groovy -cp classes runner.groovy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good