Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,55 +85,56 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
}

// get classes we need with reflection
Class<?> consoleClass;
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());
}

// get classes we need with reflection
Class<?> consoleClass;
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");

// create console to run
Object console = setupConsole(consoleClass, bindingClass);
// create console to run
Object console = setupConsole(consoleClass, bindingClass);

// run the console
invokeMethod(findMethod(consoleClass, "run"), console);
// run the console
invokeMethod(findMethod(consoleClass, "run"), console);

// 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);
// 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
loadScript(consoleClass, console);
// open script file
loadScript(consoleClass, console);

// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
throw new MojoExecutionException("Groovy 1.7.6 and 1.7.7 have a dependency on Ivy to run the console. Either change your Groovy version or add Ivy as a project or plugin dependency.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
throw new MojoExecutionException("Groovy 1.7.6 and 1.7.7 have a dependency on Ivy to run the console. Either change your Groovy version or add Ivy as a project or plugin dependency.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
}
}

Expand Down
54 changes: 28 additions & 26 deletions src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,36 +136,38 @@ protected synchronized void doExecute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
return;
}

final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());
}

// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");
// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");

// create a GroovyShell to run scripts in
Object shell = setupShell(groovyShellClass);
// create a GroovyShell to run scripts in
Object shell = setupShell(groovyShellClass);

// run the scripts
executeScripts(groovyShellClass, shell);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
// run the scripts
executeScripts(groovyShellClass, shell);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
}
}

Expand Down
75 changes: 38 additions & 37 deletions src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,46 @@ public void execute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}

// get classes we need with reflection
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
Class<?> ioClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO");
Class<?> verbosityClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
Class<?> loggerClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.util.Logger");

// create shell to run
Object shell = setupShell(shellClass, bindingClass, ioClass, verbosityClass, loggerClass);

// run the shell
invokeMethod(findMethod(shellClass, "run", String.class), shell, (String) null);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
throw new MojoExecutionException("Unable to get a JLine class from classpath. This might be because of a JLine version mismatch. If you are using Groovy < 2.2.0-beta-1, make sure you include JLine 1.0 as a runtime dependency in your project or the plugin.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
}
} else {
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a shell. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping shell startup.");
}

final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("This feature relies on Java's SecurityManager, which is deprecated for removal in Java 17. Java 18 and later will require `-Djava.security.manager=allow` be used to continue using this feature.");
System.setSecurityManager(new NoExitSecurityManager());
}

// get classes we need with reflection
Class<?> shellClass = classWrangler.getClass(groovyAtLeast(GROOVY_4_0_0_ALPHA1) ? "org.apache.groovy.groovysh.Groovysh" : "org.codehaus.groovy.tools.shell.Groovysh");
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
Class<?> ioClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO");
Class<?> verbosityClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.IO$Verbosity");
Class<?> loggerClass = classWrangler.getClass("org.codehaus.groovy.tools.shell.util.Logger");

// create shell to run
Object shell = setupShell(shellClass, bindingClass, ioClass, verbosityClass, loggerClass);

// run the shell
invokeMethod(findMethod(shellClass, "run", String.class), shell, (String) null);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && e.getCause().getMessage() != null && e.getCause().getMessage().contains("jline")) {
throw new MojoExecutionException("Unable to get a JLine class from classpath. This might be because of a JLine version mismatch. If you are using Groovy < 2.2.0-beta-1, make sure you include JLine 1.0 as a runtime dependency in your project or the plugin.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
System.setSecurityManager(sm);
}
}
}

/**
Expand Down