Skip to content

Commit fbdb26b

Browse files
committed
[gh-11485] Replace xargs -0 with POSIX-compliant alternative
The xargs -0 option is not POSIX-compliant and is not available on AIX, FreeBSD, and other systems. This commit replaces the use of xargs -0 in the concat_lines function with a pure shell solution that reads NUL-terminated strings using the read builtin with -d '' option. The new implementation: - Maintains the same functionality of handling special characters like pipes, quotes, and spaces correctly - Avoids the non-portable xargs -0 option - Uses a while loop with read -r -d '' to process NUL-terminated strings - Builds the result string incrementally instead of using tr to convert newlines to spaces This fix ensures that the mvn script works correctly on all POSIX-compliant systems while preserving the behavior introduced in commit aeff353 to handle quoted pipes and other special characters in .mvn/jvm.config. Fixes #11485
1 parent 77f52a4 commit fbdb26b

File tree

1 file changed

+33
-17
lines changed
  • apache-maven/src/assembly/maven/bin

1 file changed

+33
-17
lines changed

apache-maven/src/assembly/maven/bin/mvn

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,39 @@ find_file_argument_basedir() {
168168
# concatenates all lines of a file and replaces variables
169169
concat_lines() {
170170
if [ -f "$1" ]; then
171-
# First convert all CR to LF using tr
172-
tr '\r' '\n' < "$1" | \
173-
sed -e '/^$/d' -e 's/#.*$//' | \
174-
# Replace LF with NUL for xargs
175-
tr '\n' '\0' | \
176-
# Split into words and process each argument
177-
# Use -0 with NUL to avoid special behaviour on quotes
178-
xargs -n 1 -0 | \
179-
while read -r arg; do
180-
# Replace variables first
181-
arg=$(echo "$arg" | sed \
182-
-e "s@\${MAVEN_PROJECTBASEDIR}@$MAVEN_PROJECTBASEDIR@g" \
183-
-e "s@\$MAVEN_PROJECTBASEDIR@$MAVEN_PROJECTBASEDIR@g")
184-
185-
echo "$arg"
186-
done | \
187-
tr '\n' ' '
171+
result=""
172+
# Read the file line by line
173+
while IFS= read -r line || [ -n "$line" ]; do
174+
# Convert CR to LF
175+
line=$(echo "$line" | tr '\r' '\n')
176+
# Remove comments
177+
line=$(echo "$line" | sed 's/#.*$//')
178+
# Skip empty lines
179+
[ -z "$(echo "$line" | tr -d ' \t')" ] && continue
180+
181+
# Process each argument in the line using eval to handle quotes
182+
eval "set -- $line"
183+
for arg in "$@"; do
184+
# Replace variables
185+
arg=$(echo "$arg" | sed \
186+
-e "s@\${MAVEN_PROJECTBASEDIR}@$MAVEN_PROJECTBASEDIR@g" \
187+
-e "s@\$MAVEN_PROJECTBASEDIR@$MAVEN_PROJECTBASEDIR@g")
188+
189+
# Quote the argument if it contains spaces or special shell characters
190+
case "$arg" in
191+
*[\ \|\&\;\<\>\(\)\$\`\\\"\'\~\*\?\[\]\#\~\=]*)
192+
arg="\"$arg\""
193+
;;
194+
esac
195+
196+
if [ -n "$result" ]; then
197+
result="$result $arg"
198+
else
199+
result="$arg"
200+
fi
201+
done
202+
done < "$1"
203+
echo "$result"
188204
fi
189205
}
190206

0 commit comments

Comments
 (0)