This component provides some utilities to interpret/execute some scripts for various implementations: groovy or beanshell.
<dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-script-interpreter</artifactId> <version>1.2</version> </dependency>
ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); ByteArrayOutputStream out = new ByteArrayOutputStream(); interpreter.evaluateScript( script content, extra classPath entries, Map<String, ? extends Object> globalVariables, new PrintStream( out ) ); out.toString() returns script output
ScriptInterpreter interpreter = new GroovyScriptInterpreter(); ByteArrayOutputStream out = new ByteArrayOutputStream(); interpreter.evaluateScript( script content, extra classPath entries, Map<String, ? extends Object> globalVariables, new PrintStream( out ) ); out.toString() returns script output
ScriptRunner class will detect the script file to run based on supported extensions (.bsh,.groovy).
This class will search in the provided directory the script with the provided fileName and the supported extensions.
See javadoc for run method.
SystemStreamLog systemStreamLog = new SystemStreamLog(); ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(), new FileLogger( logFile ), "foo", true );
Your scripts will have by default two global variables:
You can add more global variables as it.
SystemStreamLog systemStreamLog = new SystemStreamLog(); ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); scriptRunner.setGlobalVariable( name, value );
You can pass some values to your script using a execution context which have the type Map<String, ? extends Object> context.
Map<String, Object> context = new HashMap<String, Object>(); context.put( "foo", "bar" ); return context; // in your bsh script String value = context.get( "foo" ); value will be "bar" // in your groovy script context.get("foo")
You can add some additional classpath entries for your script execution
SystemStreamLog systemStreamLog = new SystemStreamLog(); List<String> classpathEntries = list of jar paths ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); scriptRunner.setClassPath( classpathEntries ); scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(), new FileLogger( logFile ), "foo", true );