Executes a series of Groovy statements.
| Method from org.codehaus.groovy.ant.Groovy Detail: |
protected void addClassPathes(GroovyClassLoader classLoader) {
if (classpath != null) {
for (int i = 0; i < classpath.list().length; i++) {
classLoader.addClasspath(classpath.list()[i]);
}
}
}
Adds the class pathes (if any) |
public void addFileset(FileSet set) {
filesets.addElement(set);
}
Adds a set of files (nested fileset attribute). |
public void addText(String txt) {
log("addText('" + txt + "')", Project.MSG_VERBOSE);
this.command += txt;
}
Set an inline command to execute.
NB: Properties are not expanded in this text. |
public Argument createArg() {
return cmdline.createArgument();
}
|
public Path createClasspath() {
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
}
Returns a new path element that can be configured.
Gets called for instance by Ant when it encounters a nested <classpath> element. |
protected void execGroovy(String txt,
PrintStream out) {
log.debug("execGroovy()");
// Check and ignore empty statements
if ("".equals(txt.trim())) {
return;
}
log.verbose("Script: " + txt);
if (classpath != null) {
log.debug("Explicit Classpath: " + classpath.toString());
}
if (fork) {
log.debug("Using fork mode");
try {
createClasspathParts();
createNewArgs(txt);
super.setFork(fork);
super.setClassname(useGroovyShell ? "groovy.lang.GroovyShell" : "org.codehaus.groovy.ant.Groovy");
super.execute();
} catch (Exception e) {
StringWriter writer = new StringWriter();
new ErrorReporter(e, false).write(new PrintWriter(writer));
String message = writer.toString();
throw new BuildException("Script Failed: " + message, e, getLocation());
}
return;
}
Object mavenPom = null;
final Project project = getProject();
final ClassLoader baseClassLoader;
// treat the case Ant is run through Maven, and
if ("org.apache.commons.grant.GrantProject".equals(project.getClass().getName())) {
try {
final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
final Field contextField = propsHandler.getClass().getDeclaredField("context");
contextField.setAccessible(true);
final Object context = contextField.get(propsHandler);
mavenPom = InvokerHelper.invokeMethod(context, "getProject", new Object[0]);
}
catch (Exception e) {
throw new BuildException("Impossible to retrieve Maven's Ant project: " + e.getMessage(), getLocation());
}
// let ASM lookup "root" classloader
Thread.currentThread().setContextClassLoader(GroovyShell.class.getClassLoader());
// load groovy into "root.maven" classloader instead of "root" so that
// groovy script can access Maven classes
baseClassLoader = mavenPom.getClass().getClassLoader();
} else {
baseClassLoader = GroovyShell.class.getClassLoader();
}
final String scriptName = computeScriptName();
final GroovyClassLoader classLoader = new GroovyClassLoader(baseClassLoader);
addClassPathes(classLoader);
final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
}
|
public void execute() throws BuildException {
log.debug("execute()");
command = command.trim();
if (srcFile == null && command.length() == 0 && filesets.isEmpty()) {
throw new BuildException("Source file does not exist!", getLocation());
}
if (srcFile != null && !srcFile.exists()) {
throw new BuildException("Source file does not exist!", getLocation());
}
// TODO: any of this used?
// deal with the filesets
for (int i = 0; i < filesets.size(); i++) {
FileSet fs = filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File srcDir = fs.getDir(getProject());
String[] srcFiles = ds.getIncludedFiles();
}
try {
PrintStream out = System.out;
try {
if (output != null) {
log.verbose("Opening PrintStream to output file " + output);
out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(output.getAbsolutePath(), append)));
}
// if there are no groovy statements between the enclosing Groovy tags
// then read groovy statements in from a text file using the src attribute
if (command == null || command.trim().length() == 0) {
createClasspath().add(new Path(getProject(), srcFile.getParentFile().getCanonicalPath()));
command = getText(new BufferedReader(new FileReader(srcFile)));
}
if (command != null) {
execGroovy(command, out);
} else {
throw new BuildException("Source file does not exist!", getLocation());
}
} finally {
if (out != null && out != System.out) {
out.close();
}
}
} catch (IOException e) {
throw new BuildException(e, getLocation());
}
log.verbose("statements executed successfully");
}
Load the file and then execute it |
public Path getClasspath() {
return classpath;
}
|
public static void main(String[] args) {
final GroovyShell shell = new GroovyShell(new Binding());
final Groovy groovy = new Groovy();
for (int i = 1; i < args.length; i++) {
final Commandline.Argument argument = groovy.createArg();
argument.setValue(args[i]);
}
final AntBuilder builder = new AntBuilder();
groovy.setProject(builder.getProject());
groovy.parseAndRunScript(shell, null, null, null, new File(args[0]), builder);
}
|
protected void printResults(PrintStream out) {
log.debug("printResults()");
StringBuffer line = new StringBuffer();
out.println(line);
out.println();
}
print any results in the statement. |
protected void runStatements(Reader reader,
PrintStream out) throws IOException {
log.debug("runStatements()");
StringBuffer txt = new StringBuffer();
String line = "";
BufferedReader in = new BufferedReader(reader);
while ((line = in.readLine()) != null) {
line = getProject().replaceProperties(line);
if (line.indexOf("--") >= 0) {
txt.append("\n");
}
}
// Catch any statements not followed by ;
if (!txt.toString().equals("")) {
execGroovy(txt.toString(), out);
}
}
Read in lines and execute them. |
public void setAppend(boolean append) {
this.append = append;
}
Whether output should be appended to or overwrite
an existing file. Defaults to false. |
public void setClasspath(Path classpath) {
this.classpath = classpath;
}
Sets the classpath for loading. |
public void setClasspathRef(Reference ref) {
createClasspath().setRefid(ref);
}
Set the classpath for loading
using the classpath reference. |
public void setFork(boolean fork) {
this.fork = fork;
}
Should the script be executed using a forked process. Defaults to false. |
public void setIncludeAntRuntime(boolean includeAntRuntime) {
this.includeAntRuntime = includeAntRuntime;
}
Should the system classpath be included on the classpath when forking. Defaults to true. |
public void setOutput(File output) {
this.output = output;
}
Set the output file;
optional, defaults to the Ant log. |
public void setSrc(File srcFile) {
this.srcFile = srcFile;
}
Set the name of the file to be run. The folder of the file is automatically added to the classpath.
Required unless statements are enclosed in the build file |
public void setStacktrace(boolean stacktrace) {
configuration.setDebug(stacktrace);
}
Enable compiler to report stack trace information if a problem occurs
during compilation. |
public void setUseGroovyShell(boolean useGroovyShell) {
this.useGroovyShell = useGroovyShell;
}
Should a new GroovyShell be used when forking. Special variables won't be available
but you don't need Ant in the classpath. |