Apache Maven Script Interpreter
This library runs Groovy scripts from Java code. Maven plugins use it to run the scripts that belong to a build: Maven Invoker Plugin, for example, runs the selector, pre-build and post-build scripts of each integration test with it.
ScriptRunner finds a script by name, passes it variables and a shared context, puts extra entries on its classpath and captures its output in a log file. A script succeeds when it returns true or nothing. Any other return value fails the run with a ScriptReturnException, and an error in the script fails it with a ScriptEvaluationException.
BeanShell is deprecated since 1.10: port .bsh scripts to Groovy. Running a BeanShell script logs a warning once per script file, also in the build log of the script, and support for BeanShell will be removed in 1.12. Callers can switch the warning off with ScriptRunner.setBeanShellWarning(false), for example to let their users silence it while they port their scripts. A script with an unknown file extension runs as Groovy; up to 1.9 it ran as BeanShell. The retirement is tracked in apache/maven#13207.
Dependency declaration
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-script-interpreter</artifactId>
<version>...</version>
</dependency>
The current version is on the Dependency Information page.
maven-script-interpreter has dependency only to core interpreters library, all specific extensions should be added in your project.
For example, if you want to use Grape in a Groovy script, you must add a dependency to Ivy in your project or in the plugin that will invoke the script:
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<version>...</version>
</dependency>
Using ScriptRunner
ScriptRunner class will detect the script file to run based on supported extensions (.bsh, .groovy). A script with any other extension runs as Groovy since 1.10; up to 1.9 it ran as BeanShell.
This class will search in the provided directory the script with the provided fileName and the supported extensions.
See javadoc for run(...) methods.
try (ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.run("test", new File("src/test/resources/bsh-test"), "verify", buildContext(),
new FileLogger(logFile));
}
Mirror output from script interpreter
In order to do something more with script output, eg. log by your application you must implement FileLoggerMirrorHandler
class MyMirrorHandler implements FileLoggerMirrorHandler {
public void consumeOutput(String message) {
// this method is invoked every time when flush occurs on the underlying stream.
}
}
Now use it:
try (ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.run("test", new File("src/test/resources/bsh-test"), "verify", buildContext(),
new FileLogger(logFile, new MyMirrorHandler()));
}
Global variables
Your scripts will have by default three global variables:
basedir: the base directory passed torun(...), which is the project directory. For therun(...)overload that takes a script file and no base directory, it is the directory containing the script.scriptdir: the directory containing the script, which differs frombasedirwhen the script lives in a subdirectory of the projectcontext: the build context (see below)
You can add more global variables as it.
try (ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.setGlobalVariable( name, value );
...
}
Variables for a single run
Since 1.10, a run(...) overload takes variables that apply to that run only. They win over a global variable of the same name and do not change the runner, so one ScriptRunner can serve concurrent runs that each need their own values. The variables basedir, scriptdir and context cannot be overridden.
Map<String, Object> variables = new HashMap<>();
variables.put("name", value);
try (ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.run("test", basedir.toPath(), "verify", buildContext(), variables,
new FileLogger(logFile));
}
Build context
You can pass some values to your script using an execution context which have the type Map<String, ? extends Object> context:
private Map<String, Object> buildContext() {
Map<String, Object> context = new HashMap<String, Object>();
context.put("foo", "bar");
return context;
}
Then values are available in scripts context:
// in your bsh script
String value = context.get( "foo" );
value will be “bar”
// in your Groovy script
context.get("foo")
Additional classpath entries
You can add some additional classpath entries for your script execution
List<String> classpathEntries = list of jar paths
try (ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.setClassPath( classpathEntries );
scriptRunner.run("test", new File("src/test/resources/bsh-test"), "verify", buildContext(),
new FileLogger(logFile));
}

