From f4809059a527553b2170e6465228a0050a8183bc Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Sun, 22 Oct 2023 19:33:20 -0400 Subject: [PATCH] Print a warning about SecurityManager being deprecated --- .../codehaus/gmavenplus/mojo/ConsoleMojo.java | 17 ++++++++--------- .../codehaus/gmavenplus/mojo/ExecuteMojo.java | 18 +++++++++--------- .../codehaus/gmavenplus/mojo/ShellMojo.java | 17 ++++++++--------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java index 49da7cf0..ff50eaf0 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java @@ -85,12 +85,15 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().debug("Unable to log project test classpath"); } - if (groovyVersionSupportsAction()) { + 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."); + } + + if (!allowSystemExits) { final SecurityManager sm = System.getSecurityManager(); try { - if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); - } + 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; @@ -128,12 +131,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { } catch (InstantiationException e) { throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e); } finally { - if (!allowSystemExits) { - System.setSecurityManager(sm); - } + 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."); } } diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java index 976b2618..89c8a54c 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java @@ -136,12 +136,16 @@ protected synchronized void doExecute() throws MojoExecutionException { getLog().debug("Unable to log project test classpath"); } - if (groovyVersionSupportsAction()) { + 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; + } + + if (!allowSystemExits) { final SecurityManager sm = System.getSecurityManager(); try { - if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); - } + 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"); @@ -160,12 +164,8 @@ protected synchronized void doExecute() throws MojoExecutionException { } catch (IllegalAccessException e) { throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e); } finally { - if (!allowSystemExits) { - System.setSecurityManager(sm); - } + 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."); } } diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java index 112bf639..6fbead4e 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ShellMojo.java @@ -90,12 +90,15 @@ public void execute() throws MojoExecutionException { getLog().debug("Unable to log project test classpath"); } - if (groovyVersionSupportsAction()) { + 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."); + } + + if (!allowSystemExits) { final SecurityManager sm = System.getSecurityManager(); try { - if (!allowSystemExits) { - System.setSecurityManager(new NoExitSecurityManager()); - } + 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"); @@ -122,12 +125,8 @@ public void execute() throws MojoExecutionException { } catch (InstantiationException e) { throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e); } finally { - if (!allowSystemExits) { - System.setSecurityManager(sm); - } + System.setSecurityManager(sm); } - } else { - getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a shell. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping shell startup."); } }