Skip to content

Commit 3f5f5d4

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 3f5f5d4

File tree

4 files changed

+160
-17
lines changed

4 files changed

+160
-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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.it;
20+
21+
import java.io.File;
22+
import java.util.Properties;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
28+
/**
29+
* This is a test set for <a href="https://github.com/apache/maven/issues/11485">GH-11485</a>:
30+
* Verify that @ character in .mvn/jvm.config values is handled correctly.
31+
* This is important for Jenkins workspaces like workspace/project_PR-350@2
32+
*/
33+
public class MavenITgh11485AtSignInJvmConfigTest extends AbstractMavenIntegrationTestCase {
34+
35+
@Test
36+
public void testAtSignInJvmConfig() throws Exception {
37+
File testDir = extractResources("/gh-11485-at-sign");
38+
39+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
40+
verifier.addCliArgument(
41+
"-Dexpression.outputFile=" + new File(testDir, "target/pom.properties").getAbsolutePath());
42+
verifier.setForkJvm(true); // custom .mvn/jvm.config
43+
verifier.addCliArgument("validate");
44+
verifier.execute();
45+
verifier.verifyErrorFreeLog();
46+
47+
Properties props = verifier.loadProperties("target/pom.properties");
48+
String expectedPath = testDir.getAbsolutePath().replace('\\', '/');
49+
assertEquals(
50+
expectedPath + "/workspace@2/test",
51+
props.getProperty("project.properties.pathWithAtProp").replace('\\', '/'),
52+
"Path with @ character should be preserved");
53+
assertEquals(
54+
"value@test",
55+
props.getProperty("project.properties.propWithAtProp"),
56+
"Property value with @ character should be preserved");
57+
}
58+
}
59+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-Dpath.with.at=${MAVEN_PROJECTBASEDIR}/workspace@2/test
2+
-Dprop.with.at=value@test
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.its.gh11485</groupId>
26+
<artifactId>test</artifactId>
27+
<version>1.0</version>
28+
<packaging>pom</packaging>
29+
30+
<name>Test @ character in jvm.config</name>
31+
<description>
32+
Verify that @ character in jvm.config values is handled correctly.
33+
This is important for Jenkins workspaces like workspace/project_PR-350@2
34+
</description>
35+
36+
<properties>
37+
<pathWithAtProp>${path.with.at}</pathWithAtProp>
38+
<propWithAtProp>${prop.with.at}</propWithAtProp>
39+
</properties>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.its.plugins</groupId>
45+
<artifactId>maven-it-plugin-expression</artifactId>
46+
<version>2.1-SNAPSHOT</version>
47+
<executions>
48+
<execution>
49+
<phase>validate</phase>
50+
<goals>
51+
<goal>eval</goal>
52+
</goals>
53+
<configuration>
54+
<outputFile>target/pom.properties</outputFile>
55+
<expressions>
56+
<expression>project/properties/pathWithAtProp</expression>
57+
<expression>project/properties/propWithAtProp</expression>
58+
</expressions>
59+
</configuration>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
66+

0 commit comments

Comments
 (0)