From e92ac02b9c9ef093dfe904eca18c752751f36d05 Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Tue, 30 Apr 2019 14:37:45 -0400 Subject: [PATCH] fix executing script from URL in Groovy older than 1.7.0 --- .../codehaus/gmavenplus/mojo/ExecuteMojo.java | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java index 75f60a761..0986dfec0 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java @@ -21,6 +21,7 @@ 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.model.Version; import org.codehaus.gmavenplus.util.FileUtils; import org.codehaus.gmavenplus.util.NoExitSecurityManager; @@ -45,6 +46,11 @@ @Mojo(name = "execute", requiresDependencyResolution = ResolutionScope.TEST, configurator = "include-project-test-dependencies", threadSafe = true) public class ExecuteMojo extends AbstractToolsMojo { + /** + * Groovy 1.7.0 version. + */ + protected static final Version GROOVY_1_7_0 = new Version(1, 7, 0); + /** * Groovy scripts to run (in order). Can be an actual Groovy script or a {@link java.net.URL URL} to a Groovy script (local or remote). */ @@ -173,24 +179,23 @@ protected Object setupShell(final Class groovyShellClass) throws InvocationTa * @throws MojoExecutionException when an exception occurred during script execution (causes a "BUILD ERROR" message to be displayed) */ protected void executeScripts(final Class groovyShellClass, final Object shell) throws InvocationTargetException, IllegalAccessException, MojoExecutionException { - Method evaluateUrl = findMethod(groovyShellClass, "evaluate", Reader.class); - Method evaluateFile = findMethod(groovyShellClass, "evaluate", File.class); - Method evaluateString = findMethod(groovyShellClass, "evaluate", String.class); int scriptNum = 1; for (String script : scripts) { try { // TODO: try as file first, then as URL? try { // it's a URL to a script - executeScriptFromUrl(shell, evaluateUrl, script); + executeScriptFromUrl(groovyShellClass, shell, script); } catch (MalformedURLException e) { // it's not a URL to a script, try as a filename File scriptFile = new File(script); if (scriptFile.isFile()) { getLog().info("Running Groovy script from " + scriptFile.getCanonicalPath() + "."); + Method evaluateFile = findMethod(groovyShellClass, "evaluate", File.class); invokeMethod(evaluateFile, shell, scriptFile); } else { // it's neither a filename or URL, treat as a script body + Method evaluateString = findMethod(groovyShellClass, "evaluate", String.class); invokeMethod(evaluateString, shell, script); } } @@ -208,27 +213,41 @@ protected void executeScripts(final Class groovyShellClass, final Object shel /** * Executes a script at a URL location. * + * @param groovyShellClass the GroovyShell class * @param shell a groovy.lag.GroovyShell object - * @param evaluateUrl the evaluate method on groovy.lag.GroovyShell * @param script the script URL to execute * @throws IOException when the stream can't be opened on the URL * @throws InvocationTargetException when a reflection invocation needed for script execution cannot be completed * @throws IllegalAccessException when a method needed for script execution cannot be accessed */ - protected void executeScriptFromUrl(Object shell, Method evaluateUrl, String script) throws IOException, InvocationTargetException, IllegalAccessException { + protected void executeScriptFromUrl(Class groovyShellClass, Object shell, String script) throws IOException, InvocationTargetException, IllegalAccessException { URL url = new URL(script); getLog().info("Running Groovy script from " + url + "."); - BufferedReader reader = null; - try { - if (sourceEncoding != null) { - reader = new BufferedReader(new InputStreamReader(url.openStream(), sourceEncoding)); - } else { - reader = new BufferedReader(new InputStreamReader(url.openStream())); + if (groovyAtLeast(GROOVY_1_7_0)) { + Method evaluateUrlWithReader = findMethod(groovyShellClass, "evaluate", Reader.class); + BufferedReader reader = null; + try { + if (sourceEncoding != null) { + reader = new BufferedReader(new InputStreamReader(url.openStream(), sourceEncoding)); + } else { + reader = new BufferedReader(new InputStreamReader(url.openStream())); + } + invokeMethod(evaluateUrlWithReader, shell, reader); + } finally { + FileUtils.closeQuietly(reader); + } + } else { + Method evaluateUrlWithStream = findMethod(groovyShellClass, "evaluate", InputStream.class); + InputStream inputStream = null; + try { + if (sourceEncoding != null) { + getLog().warn("Source encoding does not apply to Groovy versions previous to 1.7.0, ignoring."); + } + inputStream = url.openStream(); + invokeMethod(evaluateUrlWithStream, shell, inputStream); + } finally { + FileUtils.closeQuietly(inputStream); } - - invokeMethod(evaluateUrl, shell, reader); - } finally { - FileUtils.closeQuietly(reader); } }