You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2020/07/22 21:31:18 UTC

[maven-script-interpreter] branch MSHARED-911 updated (6f39a1f -> 9b23f91)

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a change to branch MSHARED-911
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git.


 discard 6f39a1f  [MSHARED-911] Replace AntClassLoader by RootLoader
     add 1a5d47f  Downgrade Apache Groovy to 2.4.20
     new 9b23f91  [MSHARED-911] Replace AntClassLoader by RootLoader

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (6f39a1f)
            \
             N -- N -- N   refs/heads/MSHARED-911 (9b23f91)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .github/dependabot.yml | 2 +-
 pom.xml                | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)


[maven-script-interpreter] 01/01: [MSHARED-911] Replace AntClassLoader by RootLoader

Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch MSHARED-911
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git

commit 9b23f9180f234b0562035ecc317187715c35ca7e
Author: Slawomir Jaranowski <sl...@payu.pl>
AuthorDate: Sun May 31 14:54:03 2020 +0200

    [MSHARED-911] Replace AntClassLoader by RootLoader
    
    Closes #13
---
 pom.xml                                            | 11 -----
 .../scriptinterpreter/GroovyScriptInterpreter.java | 24 +++++------
 src/test-class-path/class-path.txt                 | 18 ++++++++
 .../GroovyScriptInterpreterTest.java               | 49 ++++++++++++++++++----
 src/test/resources/class-path.txt                  | 18 ++++++++
 5 files changed, 90 insertions(+), 30 deletions(-)

diff --git a/pom.xml b/pom.xml
index da24d0a..80a799b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,17 +88,6 @@
       <artifactId>bsh</artifactId>
       <version>2.0b5</version>
     </dependency>
-    <dependency>
-      <groupId>org.apache.ant</groupId>
-      <artifactId>ant</artifactId>
-      <version>1.9.15</version>
-      <exclusions>
-        <exclusion>
-          <groupId>org.apache.ant</groupId>
-          <artifactId>ant-launcher</artifactId>
-        </exclusion>
-      </exclusions>
-    </dependency>
 
     <dependency>
       <groupId>org.slf4j</groupId>
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
index c15e2cf..dd76784 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
@@ -21,12 +21,13 @@ package org.apache.maven.shared.scriptinterpreter;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
-import org.apache.tools.ant.AntClassLoader;
 import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.tools.RootLoader;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintStream;
-import java.io.PrintWriter;
+import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
@@ -50,31 +51,26 @@ class GroovyScriptInterpreter
         PrintStream origOut = System.out;
         PrintStream origErr = System.err;
 
-        try
+        try ( RootLoader childFirstLoader = new RootLoader( new URL[] {}, getClass().getClassLoader() ) )
         {
-            CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
 
             if ( scriptOutput != null )
             {
                 System.setErr( scriptOutput );
                 System.setOut( scriptOutput );
-                config.setOutput( new PrintWriter( scriptOutput ) );
             }
 
-            ClassLoader loader = null;
             if ( classPath != null && !classPath.isEmpty() )
             {
-                AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
                 for ( String path : classPath )
                 {
-                    childFirstLoader.addPathComponent( new File( path ) );
+                    childFirstLoader.addURL( new File( path ).toURI().toURL() );
                 }
-                loader = childFirstLoader;
             }
 
-            Binding binding = new Binding( globalVariables );
-
-            GroovyShell interpreter = new GroovyShell( loader, binding, config );
+            GroovyShell interpreter = new GroovyShell( childFirstLoader,
+                    new Binding( globalVariables ),
+                    new CompilerConfiguration( CompilerConfiguration.DEFAULT ) );
 
             try
             {
@@ -89,6 +85,10 @@ class GroovyScriptInterpreter
                 throw new ScriptEvaluationException( e );
             }
         }
+        catch ( IOException e )
+        {
+            throw new ScriptEvaluationException( e );
+        }
         finally
         {
             System.setErr( origErr );
diff --git a/src/test-class-path/class-path.txt b/src/test-class-path/class-path.txt
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/src/test-class-path/class-path.txt
@@ -0,0 +1,18 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java
index 6758051..fa47c72 100644
--- a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java
+++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java
@@ -22,40 +22,75 @@ package org.apache.maven.shared.scriptinterpreter;
 import org.junit.Test;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.PrintStream;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
 
 /**
  * Tests the Groovy interpreter facade.
- * 
+ *
  * @author Benjamin Bentmann
  */
 public class GroovyScriptInterpreterTest
 {
     @Test
-    public void testEvaluateScript()
-        throws Exception
+    public void testEvaluateScript() throws Exception
     {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         ScriptInterpreter interpreter = new GroovyScriptInterpreter();
         assertEquals( Boolean.TRUE,
-                interpreter.evaluateScript( "print \"Test\"\nreturn true", null, null, new PrintStream( out ) ) );
+                interpreter.evaluateScript( "print \"Test\"\nreturn true",
+                        null, null, new PrintStream( out ) ) );
         assertEquals( "Test", out.toString() );
     }
 
     @Test
-    public void testEvaluateScriptVars()
-        throws Exception
+    public void testEvaluateScriptWithDefaultClassPath() throws Exception
+    {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ScriptInterpreter interpreter = new GroovyScriptInterpreter();
+
+        assertEquals( Boolean.TRUE, interpreter.evaluateScript(
+                "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true",
+                null, null, new PrintStream( out ) ) );
+
+        String testClassPath = new File( "target/test-classes/class-path.txt" )
+                .toURI().getPath();
+        assertEquals( testClassPath, out.toString() );
+    }
+
+    @Test
+    public void testEvaluateScriptWithCustomClassPath() throws Exception
+    {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ScriptInterpreter interpreter = new GroovyScriptInterpreter();
+
+        List<String> classPath = Collections.singletonList( new File( "src/test-class-path" ).getAbsolutePath() );
+
+        assertEquals( Boolean.TRUE, interpreter.evaluateScript(
+                "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true",
+                classPath, null, new PrintStream( out ) ) );
+
+        String testClassPath = new File( "src/test-class-path/class-path.txt" )
+                .toURI().getPath();
+        assertEquals( testClassPath, out.toString() );
+    }
+
+    @Test
+    public void testEvaluateScriptVars() throws Exception
     {
         Map<String, Object> vars = new HashMap<>();
         vars.put( "testVar", "data" );
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         ScriptInterpreter interpreter = new GroovyScriptInterpreter();
         assertEquals( Boolean.TRUE,
-                interpreter.evaluateScript( "print testVar\nreturn true", null, vars, new PrintStream( out ) ) );
+                interpreter.evaluateScript( "print testVar\nreturn true",
+                        null, vars, new PrintStream( out ) ) );
         assertEquals( "data", out.toString() );
     }
 
diff --git a/src/test/resources/class-path.txt b/src/test/resources/class-path.txt
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/src/test/resources/class-path.txt
@@ -0,0 +1,18 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */