Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -225,9 +224,9 @@ protected Context doCreate(Path mavenHome, ExecutorRequest executorRequest) {
ArrayList<String> mavenArgs = new ArrayList<>();
String mavenArgsEnv = System.getenv("MAVEN_ARGS");
if (useMavenArgsEnv && mavenArgsEnv != null && !mavenArgsEnv.isEmpty()) {
Arrays.stream(mavenArgsEnv.split(" "))
.filter(s -> !s.trim().isEmpty())
.forEach(s -> mavenArgs.add(0, s));
List<String> parsed = parseArguments(mavenArgsEnv);
Collections.reverse(parsed);
mavenArgs.addAll(parsed);
}

Properties properties = prepareProperties(executorRequest);
Expand Down Expand Up @@ -441,4 +440,47 @@ protected String getMavenVersion(Class<?> clazz) throws IOException {
return UNKNOWN_VERSION;
}
}

/**
* Parses a string of arguments respecting quoted strings.
* Handles both single and double quotes, and preserves backslashes
* (important for Windows paths).
*/
static List<String> parseArguments(String args) {
List<String> result = new ArrayList<>();
StringBuilder current = new StringBuilder();
boolean inDoubleQuotes = false;
boolean inSingleQuotes = false;
for (int i = 0; i < args.length(); i++) {
char c = args.charAt(i);
if (inDoubleQuotes) {
if (c == '"') {
inDoubleQuotes = false;
} else {
current.append(c);
}
} else if (inSingleQuotes) {
if (c == '\'') {
inSingleQuotes = false;
} else {
current.append(c);
}
} else if (c == '"') {
inDoubleQuotes = true;
} else if (c == '\'') {
inSingleQuotes = true;
} else if (Character.isWhitespace(c)) {
if (!current.isEmpty()) {
result.add(current.toString());
current.setLength(0);
}
} else {
current.append(c);
}
}
if (!current.isEmpty()) {
result.add(current.toString());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -116,9 +115,7 @@ protected int doExecute(ExecutorRequest executorRequest) throws ExecutorExceptio

String mavenArgsEnv = System.getenv("MAVEN_ARGS");
if (useMavenArgsEnv && mavenArgsEnv != null && !mavenArgsEnv.isEmpty()) {
Arrays.stream(mavenArgsEnv.split(" "))
.filter(s -> !s.trim().isEmpty())
.forEach(cmdAndArguments::add);
cmdAndArguments.addAll(parseArguments(mavenArgsEnv));
}

cmdAndArguments.addAll(executorRequest.arguments());
Expand Down Expand Up @@ -223,4 +220,47 @@ public void close() throws ExecutorException {
// nothing yet
}
}

/**
* Parses a string of arguments respecting quoted strings.
* Handles both single and double quotes, and preserves backslashes
* (important for Windows paths).
*/
static List<String> parseArguments(String args) {
List<String> result = new ArrayList<>();
StringBuilder current = new StringBuilder();
boolean inDoubleQuotes = false;
boolean inSingleQuotes = false;
for (int i = 0; i < args.length(); i++) {
char c = args.charAt(i);
if (inDoubleQuotes) {
if (c == '"') {
inDoubleQuotes = false;
} else {
current.append(c);
}
} else if (inSingleQuotes) {
if (c == '\'') {
inSingleQuotes = false;
} else {
current.append(c);
}
} else if (c == '"') {
inDoubleQuotes = true;
} else if (c == '\'') {
inSingleQuotes = true;
} else if (Character.isWhitespace(c)) {
if (!current.isEmpty()) {
result.add(current.toString());
current.setLength(0);
}
} else {
current.append(c);
}
}
if (!current.isEmpty()) {
result.add(current.toString());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
*/
package org.apache.maven.cling.executor.embedded;

import java.util.List;

import org.apache.maven.api.cli.Executor;
import org.apache.maven.cling.executor.MavenExecutorTestSupport;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Embedded executor UT
Expand All @@ -30,4 +35,34 @@ public class EmbeddedMavenExecutorTest extends MavenExecutorTestSupport {
protected Executor doSelectExecutor() {
return new EmbeddedMavenExecutor();
}

@Test
void testParseArgumentsSimple() {
assertEquals(
List.of("-T", "4", "clean", "install"), EmbeddedMavenExecutor.parseArguments("-T 4 clean install"));
}

@Test
void testParseArgumentsDoubleQuoted() {
assertEquals(
List.of("-f", "C:\\Program Files\\project\\pom.xml"),
EmbeddedMavenExecutor.parseArguments("-f \"C:\\Program Files\\project\\pom.xml\""));
}

@Test
void testParseArgumentsSingleQuoted() {
assertEquals(
List.of("-f", "/path with spaces/pom.xml"),
EmbeddedMavenExecutor.parseArguments("-f '/path with spaces/pom.xml'"));
}

@Test
void testParseArgumentsEmpty() {
assertEquals(List.of(), EmbeddedMavenExecutor.parseArguments(""));
}

@Test
void testParseArgumentsExtraWhitespace() {
assertEquals(List.of("clean", "install"), EmbeddedMavenExecutor.parseArguments(" clean install "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
*/
package org.apache.maven.cling.executor.forked;

import java.util.List;

import org.apache.maven.api.cli.Executor;
import org.apache.maven.cling.executor.MavenExecutorTestSupport;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Forked executor UT
Expand All @@ -30,4 +35,33 @@ public class ForkedMavenExecutorTest extends MavenExecutorTestSupport {
protected Executor doSelectExecutor() {
return new ForkedMavenExecutor();
}

@Test
void testParseArgumentsSimple() {
assertEquals(List.of("-T", "4", "clean", "install"), ForkedMavenExecutor.parseArguments("-T 4 clean install"));
}

@Test
void testParseArgumentsDoubleQuoted() {
assertEquals(
List.of("-f", "C:\\Program Files\\project\\pom.xml"),
ForkedMavenExecutor.parseArguments("-f \"C:\\Program Files\\project\\pom.xml\""));
}

@Test
void testParseArgumentsSingleQuoted() {
assertEquals(
List.of("-f", "/path with spaces/pom.xml"),
ForkedMavenExecutor.parseArguments("-f '/path with spaces/pom.xml'"));
}

@Test
void testParseArgumentsEmpty() {
assertEquals(List.of(), ForkedMavenExecutor.parseArguments(""));
}

@Test
void testParseArgumentsExtraWhitespace() {
assertEquals(List.of("clean", "install"), ForkedMavenExecutor.parseArguments(" clean install "));
}
}