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 2026/09/15 23:31:54 UTC

(maven-script-interpreter) branch agent/script-basedir-is-project-dir created (now 4b8204d)

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

slachiewicz pushed a change to branch agent/script-basedir-is-project-dir
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git


      at 4b8204d  Bind the script basedir variable to the project directory

This branch includes the following new commits:

     new 4b8204d  Bind the script basedir variable to the project directory

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.



(maven-script-interpreter) 01/01: Bind the script basedir variable to the project directory

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

slachiewicz pushed a commit to branch agent/script-basedir-is-project-dir
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git

commit 4b8204d014aac785193fc384048d3cd6301030dc
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Wed Sep 16 01:31:39 2026 +0200

    Bind the script basedir variable to the project directory
    
    The documented contract for basedir is the base directory of the test
    project, but it was bound to the directory holding the script file, so
    the two differed whenever the script lived in a subdirectory. Scripts
    that relied on the old value get the script's own directory from the
    new scriptdir variable.
    
    Fixes apache/maven-invoker-plugin#299
---
 .../shared/scriptinterpreter/ScriptRunner.java     | 20 ++++++++--
 src/site/markdown/index.md.vm                      |  7 +++-
 .../shared/scriptinterpreter/ScriptRunnerTest.java | 44 ++++++++++++++++++++++
 src/test/resources/basedir-test/scripts/verify.bsh | 23 +++++++++++
 .../resources/basedir-test/scripts/verify.groovy   | 23 +++++++++++
 5 files changed, 111 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
index 893853a..0efed83 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
@@ -128,6 +128,10 @@ public class ScriptRunner implements Closeable {
 
     /**
      * Runs the specified hook script (after resolution).
+     * <p>
+     * The script is given the global variables <code>basedir</code>, holding the project base directory passed in
+     * here, and <code>scriptdir</code>, holding the directory the script file itself lives in. The two differ when
+     * <code>relativeScriptPath</code> points into a subdirectory of the project.
      *
      * @param scriptDescription The description of the script to use for logging, must not be <code>null</code>.
      * @param basedir The base directory of the project, must not be <code>null</code>.
@@ -161,11 +165,14 @@ public class ScriptRunner implements Closeable {
             return;
         }
 
-        executeRun(scriptDescription, scriptFile, context, logger);
+        executeRun(scriptDescription, basedir, scriptFile, context, logger);
     }
 
     /**
      * Runs the specified hook script.
+     * <p>
+     * As no project base directory is passed in, both the <code>basedir</code> and the <code>scriptdir</code> global
+     * variables are set to the directory the script file lives in.
      *
      * @param scriptDescription The description of the script to use for logging, must not be <code>null</code>.
      * @param scriptFile The path to the script, may be <code>null</code> to skip the script execution.
@@ -183,11 +190,15 @@ public class ScriptRunner implements Closeable {
             return;
         }
 
-        executeRun(scriptDescription, scriptFile, context, logger);
+        executeRun(scriptDescription, scriptFile.getParentFile(), scriptFile, context, logger);
     }
 
     private void executeRun(
-            final String scriptDescription, File scriptFile, final Map<String, ?> context, final ExecutionLogger logger)
+            final String scriptDescription,
+            final File basedir,
+            File scriptFile,
+            final Map<String, ?> context,
+            final ExecutionLogger logger)
             throws IOException, ScriptException {
         ScriptInterpreter interpreter = getInterpreter(scriptFile);
         if (LOG.isDebugEnabled()) {
@@ -219,7 +230,8 @@ public class ScriptRunner implements Closeable {
             PrintStream out = (logger != null) ? logger.getPrintStream() : null;
 
             Map<String, Object> scriptVariables = new HashMap<>(this.globalVariables);
-            scriptVariables.put("basedir", scriptFile.getParentFile());
+            scriptVariables.put("basedir", basedir);
+            scriptVariables.put("scriptdir", scriptFile.getParentFile());
             scriptVariables.put("context", context);
 
             synchronized (LOCK) {
diff --git a/src/site/markdown/index.md.vm b/src/site/markdown/index.md.vm
index 6b57eec..a2ba72d 100644
--- a/src/site/markdown/index.md.vm
+++ b/src/site/markdown/index.md.vm
@@ -84,9 +84,12 @@ try (ScriptRunner scriptRunner = new ScriptRunner()) {
 
 #[[### Global variables]]#
 
-Your scripts will have by default two global variables:
+Your scripts will have by default three global variables:
 
-- `basedir`: the base directory of your script
+- `basedir`: the base directory passed to `run(...)`, which is the project directory. For the `run(...)` overload that
+  takes a script file and no base directory, it is the directory containing the script.
+- `scriptdir`: the directory containing the script, which differs from `basedir` when the script lives in a
+  subdirectory of the project
 - `context`: the build context (see below)
 
 You can add more global variables as it.
diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
index 86d4309..bdfbbee 100644
--- a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
+++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
@@ -264,6 +264,50 @@ class ScriptRunnerTest {
         assertTrue(logContent.contains("wireMockServer stopped"));
     }
 
+    /**
+     * A script living in a subdirectory of the project must still see the project directory as <code>basedir</code>,
+     * with its own directory available as <code>scriptdir</code>.
+     *
+     * @see <a href="https://github.com/apache/maven-invoker-plugin/issues/299">maven-invoker-plugin#299</a>
+     */
+    @ValueSource(strings = {"bsh", "groovy"})
+    @ParameterizedTest
+    void scriptInSubDirectoryShouldSeeProjectBasedir(String scriptType) throws Exception {
+        File logFile = new File(tempDir, "build.log");
+        File basedir = new File("src/test/resources/basedir-test");
+
+        Map<String, Object> context = new HashMap<>();
+
+        try (FileLogger logger = new FileLogger(logFile);
+                ScriptRunner scriptRunner = new ScriptRunner()) {
+            scriptRunner.run("test", basedir, "scripts/verify." + scriptType, context, logger);
+        }
+
+        assertEquals(basedir, context.get("basedir"));
+        assertEquals(new File(basedir, "scripts"), context.get("scriptdir"));
+    }
+
+    /**
+     * The overload without a project directory has no better candidate, so both variables point at the script's own
+     * directory.
+     */
+    @ValueSource(strings = {"bsh", "groovy"})
+    @ParameterizedTest
+    void scriptRunWithoutBasedirShouldUseScriptDirectory(String scriptType) throws Exception {
+        File logFile = new File(tempDir, "build.log");
+        File scriptDir = new File("src/test/resources/basedir-test/scripts");
+
+        Map<String, Object> context = new HashMap<>();
+
+        try (FileLogger logger = new FileLogger(logFile);
+                ScriptRunner scriptRunner = new ScriptRunner()) {
+            scriptRunner.run("test", new File(scriptDir, "verify." + scriptType), context, logger);
+        }
+
+        assertEquals(scriptDir, context.get("basedir"));
+        assertEquals(scriptDir, context.get("scriptdir"));
+    }
+
     /**
      * Verifies that a Groovy script containing non-ASCII characters (UTF-8 encoded)
      * is decoded correctly when no explicit encoding is set. The script file
diff --git a/src/test/resources/basedir-test/scripts/verify.bsh b/src/test/resources/basedir-test/scripts/verify.bsh
new file mode 100644
index 0000000..bfc9fd0
--- /dev/null
+++ b/src/test/resources/basedir-test/scripts/verify.bsh
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+context.put("basedir", basedir);
+context.put("scriptdir", scriptdir);
+
+return true;
diff --git a/src/test/resources/basedir-test/scripts/verify.groovy b/src/test/resources/basedir-test/scripts/verify.groovy
new file mode 100644
index 0000000..13a2c19
--- /dev/null
+++ b/src/test/resources/basedir-test/scripts/verify.groovy
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+context.put("basedir", basedir)
+context.put("scriptdir", scriptdir)
+
+return true