Skip to content

Commit

Permalink
Merge pull request #167 from groovy/165
Browse files Browse the repository at this point in the history
Add ability to choose a script to load into console
  • Loading branch information
keeganwitt authored Sep 1, 2020
2 parents 1956fc0 + 0476a72 commit fda479c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.gmavenplus.util.NoExitSecurityManager;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand All @@ -44,6 +46,14 @@
@Mojo(name = "console", requiresDependencyResolution = ResolutionScope.TEST, configurator = "include-project-test-dependencies")
public class ConsoleMojo extends AbstractToolsMojo {

/**
* Script file to load into console. Can also be a project property referring to a file.
*
* @since 1.10.1
*/
@Parameter(property = "consoleScript")
protected String consoleScript;

/**
* Executes this mojo.
*
Expand Down Expand Up @@ -94,6 +104,24 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// TODO: for some reason instantiating AntBuilder before calling run() causes its stdout and stderr streams to not be captured by the Console
bindAntBuilder(consoleClass, bindingClass, console);

// open script file
if (consoleScript != null) {
Method loadScriptFile = findMethod(consoleClass, "loadScriptFile", File.class);
File consoleScriptFile = new File(consoleScript);
if (consoleScriptFile.isFile()) {
invokeMethod(loadScriptFile, console, consoleScriptFile);
} else if (project.getProperties().containsKey(consoleScript)) {
consoleScriptFile = new File(project.getProperties().getProperty(consoleScript));
if (consoleScriptFile.isFile()) {
invokeMethod(loadScriptFile, consoleScriptFile);
} else {
getLog().warn("consoleScript ('" + consoleScript + "') doesn't exist in project properties or as a file.");
}
} else {
getLog().warn("consoleScript ('" + consoleScript + "') doesn't exist in project properties or as a file.");
}
}

// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
Expand Down

0 comments on commit fda479c

Please sign in to comment.