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 20:18:51 UTC
[maven-script-interpreter] 01/01: [MSHARED-911] Replace
AntClassLoader by RootLoader
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 6f39a1ffd33f662a50dcdde40dba9f07c9cce6d5
Author: Slawomir Jaranowski <sl...@payu.pl>
AuthorDate: Sun May 31 14:54:03 2020 +0200
[MSHARED-911] Replace AntClassLoader by RootLoader
---
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 c8420ae..32c8631 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.
+ */