Skip to content

Commit defa688

Browse files
committed
[gh-11363] Parse jvm.config lines into individual arguments
Lines can contain multiple space-separated arguments like: -Xmx2048m -Xms1024m -Dtest.prop1=value1 Parse each line into individual arguments, respecting quoted strings. Then quote each argument individually for batch script safety.
1 parent 2e3ba29 commit defa688

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

apache-maven/src/assembly/maven/bin/JvmConfigParser.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
2424
import java.nio.file.Paths;
25+
import java.util.ArrayList;
26+
import java.util.List;
2527
import java.util.stream.Stream;
2628

2729
/**
@@ -69,22 +71,16 @@ public static void main(String[] args) {
6971
line = line.replace("${MAVEN_PROJECTBASEDIR}", mavenProjectBasedir);
7072
line = line.replace("$MAVEN_PROJECTBASEDIR", mavenProjectBasedir);
7173

72-
// Strip outer single quotes if present (they're just for grouping in jvm.config)
73-
// But preserve already-doubled quotes (escaped quotes like ""value"")
74-
if (line.startsWith("\"") && line.endsWith("\"") && line.length() > 1) {
75-
// Check if it's NOT already escaped (doubled quotes at start/end)
76-
if (!(line.length() >= 4 && line.startsWith("\"\"") && line.endsWith("\"\""))) {
77-
line = line.substring(1, line.length() - 1);
78-
}
79-
} else if (line.startsWith("'") && line.endsWith("'") && line.length() > 1) {
80-
line = line.substring(1, line.length() - 1);
81-
}
74+
// Parse line into individual arguments (split on spaces, respecting quotes)
75+
List<String> args = parseArguments(line);
8276

83-
// Append quoted argument with space separator
84-
if (result.length() > 0) {
85-
result.append(' ');
77+
// Append each argument quoted
78+
for (String arg : args) {
79+
if (result.length() > 0) {
80+
result.append(' ');
81+
}
82+
result.append('"').append(arg).append('"');
8683
}
87-
result.append('"').append(line).append('"');
8884
});
8985

9086
System.out.print(result.toString());
@@ -93,5 +89,43 @@ public static void main(String[] args) {
9389
System.exit(1);
9490
}
9591
}
92+
93+
/**
94+
* Parse a line into individual arguments, respecting quoted strings.
95+
* Quotes are stripped from the arguments.
96+
*/
97+
private static List<String> parseArguments(String line) {
98+
List<String> args = new ArrayList<>();
99+
StringBuilder current = new StringBuilder();
100+
boolean inQuotes = false;
101+
boolean inSingleQuotes = false;
102+
103+
for (int i = 0; i < line.length(); i++) {
104+
char c = line.charAt(i);
105+
106+
if (c == '"' && !inSingleQuotes) {
107+
inQuotes = !inQuotes;
108+
// Don't include the quote character itself
109+
} else if (c == '\'' && !inQuotes) {
110+
inSingleQuotes = !inSingleQuotes;
111+
// Don't include the quote character itself
112+
} else if (c == ' ' && !inQuotes && !inSingleQuotes) {
113+
// Space outside quotes - end of argument
114+
if (current.length() > 0) {
115+
args.add(current.toString());
116+
current.setLength(0);
117+
}
118+
} else {
119+
current.append(c);
120+
}
121+
}
122+
123+
// Add last argument
124+
if (current.length() > 0) {
125+
args.add(current.toString());
126+
}
127+
128+
return args;
129+
}
96130
}
97131

0 commit comments

Comments
 (0)