Class ScriptRunner
- java.lang.Object
-
- org.apache.maven.shared.scriptinterpreter.ScriptRunner
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ScriptRunner extends java.lang.Object implements java.io.CloseableRuns pre-/post-build hook scripts.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringencodingThe file encoding of the hook scripts ornullto use platform encoding.private java.util.Map<java.lang.String,java.lang.Object>globalVariablesThe common set of global variables to pass into the script interpreter, nevernull.private static java.lang.ObjectLOCKprivate static org.slf4j.LoggerLOGprivate java.util.Map<java.lang.String,ScriptInterpreter>scriptInterpretersThe supported script interpreters, indexed by the lower-case file extension of their associated script files, nevernull.
-
Constructor Summary
Constructors Constructor Description ScriptRunner()Creates a new script runner with BSH and Groovy interpreters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddScriptInterpreter(java.lang.String id, ScriptInterpreter scriptInterpreter)Add new script Interpretervoidclose()Closes this script interpreter and releases any system resources associated with it.private voidexecuteRun(java.lang.String scriptDescription, java.io.File scriptFile, java.util.Map<java.lang.String,?> context, ExecutionLogger logger)private ScriptInterpretergetInterpreter(java.io.File scriptFile)Determines the script interpreter for the specified script file by looking at its file extension.private java.io.FileresolveScript(java.io.File scriptFile)Gets the effective path to the specified script.voidrun(java.lang.String scriptDescription, java.io.File basedir, java.lang.String relativeScriptPath, java.util.Map<java.lang.String,?> context, ExecutionLogger logger)Runs the specified hook script (after resolution).voidrun(java.lang.String scriptDescription, java.io.File scriptFile, java.util.Map<java.lang.String,?> context, ExecutionLogger logger)Runs the specified hook script.voidsetClassPath(java.util.List<java.lang.String> classPath)Sets the additional class path for the hook scripts.voidsetGlobalVariable(java.lang.String name, java.lang.Object value)Sets a global variable for the script interpreter.voidsetScriptEncoding(java.lang.String encoding)Sets the file encoding of the hook scripts.
-
-
-
Field Detail
-
LOCK
private static final java.lang.Object LOCK
-
LOG
private static final org.slf4j.Logger LOG
-
scriptInterpreters
private java.util.Map<java.lang.String,ScriptInterpreter> scriptInterpreters
The supported script interpreters, indexed by the lower-case file extension of their associated script files, nevernull.
-
globalVariables
private java.util.Map<java.lang.String,java.lang.Object> globalVariables
The common set of global variables to pass into the script interpreter, nevernull.
-
encoding
private java.lang.String encoding
The file encoding of the hook scripts ornullto use platform encoding.
-
-
Method Detail
-
addScriptInterpreter
public void addScriptInterpreter(java.lang.String id, ScriptInterpreter scriptInterpreter)Add new script Interpreter- Parameters:
id- The Id of interpreterscriptInterpreter- the Script Interpreter implementation
-
setGlobalVariable
public void setGlobalVariable(java.lang.String name, java.lang.Object value)Sets a global variable for the script interpreter.- Parameters:
name- The name of the variable, must not benull.value- The value of the variable, may benull.
-
setClassPath
public void setClassPath(java.util.List<java.lang.String> classPath)
Sets the additional class path for the hook scripts. Note that the provided list is copied, so any later changes will not affect the scripts.- Parameters:
classPath- The additional class path for the script interpreter, may benullor empty if only the plugin realm should be used for the script evaluation. If specified, this class path will precede the artifacts from the plugin class path.
-
setScriptEncoding
public void setScriptEncoding(java.lang.String encoding)
Sets the file encoding of the hook scripts.- Parameters:
encoding- The file encoding of the hook scripts, may benullor empty to use the platform's default encoding.
-
run
public void run(java.lang.String scriptDescription, java.io.File basedir, java.lang.String relativeScriptPath, java.util.Map<java.lang.String,?> context, ExecutionLogger logger) throws java.io.IOException, ScriptExceptionRuns the specified hook script (after resolution).- Parameters:
scriptDescription- The description of the script to use for logging, must not benull.basedir- The base directory of the project, must not benull.relativeScriptPath- The path to the script relative to the project base directory, may benullto skip the script execution and may not have extensions (resolution will search).context- The key-value storage used to share information between hook scripts, may benull.logger- The logger to redirect the script output to, may benullto use stdout/stderr.- Throws:
java.io.IOException- If an I/O error occurred while reading the script file.ScriptException- If the script did not returntrueof threw an exception.
-
run
public void run(java.lang.String scriptDescription, java.io.File scriptFile, java.util.Map<java.lang.String,?> context, ExecutionLogger logger) throws java.io.IOException, ScriptExceptionRuns the specified hook script.- Parameters:
scriptDescription- The description of the script to use for logging, must not benull.scriptFile- The path to the script, may benullto skip the script execution.context- The key-value storage used to share information between hook scripts, may benull.logger- The logger to redirect the script output to, may benullto use stdout/stderr.- Throws:
java.io.IOException- If an I/O error occurred while reading the script file.ScriptException- If the script did not returntrueof threw an exception.
-
executeRun
private void executeRun(java.lang.String scriptDescription, java.io.File scriptFile, java.util.Map<java.lang.String,?> context, ExecutionLogger logger) throws java.io.IOException, ScriptException- Throws:
java.io.IOExceptionScriptException
-
resolveScript
private java.io.File resolveScript(java.io.File scriptFile)
Gets the effective path to the specified script. For convenience, we allow to specify a script path as "verify" and have the plugin auto-append the file extension to search for "verify.bsh" and "verify.groovy".- Parameters:
scriptFile- The script file to resolve, may benull.- Returns:
- The effective path to the script file or
nullif the input wasnull.
-
getInterpreter
private ScriptInterpreter getInterpreter(java.io.File scriptFile)
Determines the script interpreter for the specified script file by looking at its file extension. In this context, file extensions are considered case-insensitive. For backward compatibility with plugin versions 1.2-, the BeanShell interpreter will be used for any unrecognized extension.- Parameters:
scriptFile- The script file for which to determine an interpreter, must not benull.- Returns:
- The script interpreter for the file, never
null.
-
close
public void close() throws java.io.IOExceptionCloses this script interpreter and releases any system resources associated with it.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- if an I/O error occurs.
-
-