You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/10/15 21:03:13 UTC

[PR] [Backport maven-4.0.x] fix: honour quotes in MAVEN_ARGS in bin/mvn (fix #13413) [maven]

gnodet opened a new pull request, #13551:
URL: https://github.com/apache/maven/pull/13551

   Backport of #13478 to `maven-4.0.x`.
   
   `bin/mvn` passed `MAVEN_ARGS` to the JVM unquoted, so the shell split on spaces and quote characters reached Maven as part of the arguments. This broke builds in workspaces with spaces in the path (e.g. Jenkins Pipeline Maven Plugin passing `--settings` or `-Dmaven.repo.local`).
   
   The fix adds a POSIX-portable quote parser that handles single and double quotes, so a value like `"-Dmaven.repo.local=/path with space/repo"` is correctly passed as a single argument.
   
   Adaptation from master: the `eval exec` block on 4.0.x had a different structure (conditional on `MAVEN_MAIN_CLASS`) which is replaced by the unified `eval exec "$cmd" '"$@"'` after the quote parser populates `"$@"`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Backport maven-4.0.x] fix: honour quotes in MAVEN_ARGS in bin/mvn (fix #13413) [maven]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #13551:
URL: https://github.com/apache/maven/pull/13551#discussion_r4288309816


##########
apache-maven/src/assembly/maven/bin/mvn:
##########
@@ -388,22 +389,74 @@ cmd="\"$JAVACMD\" \
   \"-Dmaven.multiModuleProjectDirectory=$MAVEN_PROJECTBASEDIR\" \
   $LAUNCHER_CLASS"
 
+# MAVEN_ARGS is only passed for the default Maven build command (MavenCling),
+# not for sub-commands like --up, --enc, --shell or --val which have their own options.
+#
+# Its words are separated by white space, and single or double quotes group a word
+# containing white space, so that a path with a space can be passed.
+# Nothing else is interpreted: no backslash escape, no ${...} placeholder, no globbing
+# and no command substitution, so the values reach Maven as they were written.
+# A word is made of adjacent quoted and unquoted parts, and "" is an empty word.
+# The words go in front of the user arguments, "$@" is rotated to get them in order.
+if [ "$MAVEN_MAIN_CLASS" = "org.apache.maven.cling.MavenCling" ] && [ -n "$MAVEN_ARGS" ]; then
+  maven_args_user_count=$#
+  maven_args_rest=$MAVEN_ARGS
+  maven_args_word=
+  maven_args_in_word=false
+  maven_args_quote=
+  maven_args_tab=`printf '\t'`
+  maven_args_nl='
+'
+  while [ -n "$maven_args_rest" ]; do
+    maven_args_tail=${maven_args_rest#?}
+    maven_args_char=${maven_args_rest%"$maven_args_tail"}
+    maven_args_rest=$maven_args_tail
+    if [ -n "$maven_args_quote" ]; then
+      if [ "$maven_args_char" = "$maven_args_quote" ]; then
+        maven_args_quote=
+      else
+        maven_args_word=$maven_args_word$maven_args_char
+      fi
+    else
+      case $maven_args_char in
+        '"' | "'")
+          maven_args_quote=$maven_args_char
+          maven_args_in_word=true
+          ;;
+        ' ' | "$maven_args_tab" | "$maven_args_nl")
+          if [ "$maven_args_in_word" = true ]; then
+            set -- "$@" "$maven_args_word"
+            maven_args_word=
+            maven_args_in_word=false
+          fi
+          ;;
+        *)
+          maven_args_word=$maven_args_word$maven_args_char
+          maven_args_in_word=true
+          ;;
+      esac
+    fi
+  done
+  if [ -n "$maven_args_quote" ]; then
+    echo "Warning: MAVEN_ARGS has an unterminated $maven_args_quote quote, the rest of the value is treated as part of the last word" >&2
+  fi
+  if [ "$maven_args_in_word" = true ]; then
+    set -- "$@" "$maven_args_word"
+  fi
+  while [ "$maven_args_user_count" -gt 0 ]; do
+    set -- "$@" "$1"
+    shift
+    maven_args_user_count=$((maven_args_user_count - 1))
+  done
+fi
+
 if [ -n "$MAVEN_DEBUG_SCRIPT" ]; then
   echo "[DEBUG] Launching JVM with command:" >&2
   printf '[DEBUG]   %s' "$cmd" >&2
-  if [ "$MAVEN_MAIN_CLASS" = "org.apache.maven.cling.MavenCling" ]; then
-    printf ' %s' "$MAVEN_ARGS" >&2
-  fi
   printf ' "%s"' "$@" >&2; echo >&2
 fi
 
-# User arguments ("$@") and MAVEN_ARGS are passed outside the eval'd string
-# to preserve literal values (backslashes, ${...} placeholders) without
-# shell re-parsing. Only the base command uses eval for MAVEN_OPTS word splitting.
-# MAVEN_ARGS is only passed for the default Maven build command (MavenCling),
-# not for sub-commands like --up, --enc, or --shell which have their own options.
-if [ "$MAVEN_MAIN_CLASS" = "org.apache.maven.cling.MavenCling" ]; then
-  eval exec "$cmd" '$MAVEN_ARGS' '"$@"'
-else
-  eval exec "$cmd" '"$@"'
-fi
+# The arguments ("$@") are passed outside the eval'd string to preserve literal
+# values (backslashes, ${...} placeholders) without shell re-parsing.
+# Only the base command uses eval for MAVEN_OPTS word splitting.
+eval exec "$cmd" '"$@"

Review Comment:
   🐛 **Bug: Missing closing single-quote — script is broken**
   
   The single-quote before `"$@"` is never closed, causing a shell syntax error (SC1073: Couldn't parse this single-quoted string). The shell will refuse to parse `bin/mvn` at all — every Maven invocation will fail.
   
   ```suggestion
   eval exec "$cmd" '"$@"'
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Backport maven-4.0.x] fix: honour quotes in MAVEN_ARGS in bin/mvn (fix #13413) [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #13551:
URL: https://github.com/apache/maven/pull/13551


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org