This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch issue-66-fix in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git
commit 907b961a6f537609b57686bbf06c63e1665f623f Author: Elliotte Rusty Harold <[email protected]> AuthorDate: Fri Oct 30 11:03:59 2026 +0000 Fix NPE when resource not found and add test --- .../plugins/scripting/ResourceScriptEvaluator.java | 15 +++++++--- .../scripting/ResourceScriptEvaluatorTest.java | 34 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java b/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java index 8db4eb6..a4ccf2c 100644 --- a/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java +++ b/src/main/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluator.java @@ -65,12 +65,19 @@ public class ResourceScriptEvaluator extends AbstractScriptEvaluator { * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext) */ protected Object eval(ScriptEngine engine, ScriptContext context) throws ScriptException { - - try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(resourceName); - Reader reader = new InputStreamReader(is)) { - return engine.eval(reader, context); + InputStream is = null; + try { + is = this.getClass().getClassLoader().getResourceAsStream(resourceName); + if (is == null) { + throw new ScriptException("Script resource '" + resourceName + "' not found"); + } + try (Reader reader = new InputStreamReader(is)) { + return engine.eval(reader, context); + } } catch (IOException ex) { throw new UncheckedIOException(resourceName + " caused:", ex); + } catch (NullPointerException ex) { + throw new ScriptException("Script resource '" + resourceName + "' not found"); } } diff --git a/src/test/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluatorTest.java b/src/test/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluatorTest.java new file mode 100644 index 0000000..838d92d --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/scripting/ResourceScriptEvaluatorTest.java @@ -0,0 +1,34 @@ +/* + * 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. + */ +package org.apache.maven.plugins.scripting; + +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ResourceScriptEvaluatorTest { + @Test + void missingResourceShouldThrowScriptException() throws Exception { + ResourceScriptEvaluator evaluator = new ResourceScriptEvaluator(null, "does-not-exist.groovy"); + assertThrows(ScriptException.class, () -> evaluator.eval(new ScriptEngineManager())); + } +}
