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
15 changes: 7 additions & 8 deletions src/main/java/org/codehaus/plexus/util/cli/Commandline.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,19 +428,18 @@ public String[] getEnvironmentVariables() throws CommandLineException {
}

/**
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
* @deprecated Use {@link org.codehaus.plexus.util.cli.Commandline#getRawCommandline()} method instead.
* @return Returns the executable and all defined arguments.
* For Windows Family, {@link Commandline#getShellCommandline()} is returned
*/
@Deprecated
public String[] getCommandline() {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
return getShellCommandline();
}

return getRawCommandline();
}

/**
* Returns the executable and all defined arguments.
* Returns the executable and all defined arguments.<br>
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
* @return the command line as array not escaped neither quoted
*/
public String[] getRawCommandline() {
Expand Down Expand Up @@ -494,7 +493,7 @@ public String toString() {
}

public int size() {
return getCommandline().length;
return getRawCommandline().length;
}

@Override
Expand Down Expand Up @@ -582,7 +581,7 @@ public Process execute() throws CommandLineException {

try {
if (workingDir == null) {
process = Runtime.getRuntime().exec(getCommandline(), environment, workingDir);
process = Runtime.getRuntime().exec(getRawCommandline(), environment, workingDir);
} else {
Comment on lines 583 to 585
if (!workingDir.exists()) {
throw new CommandLineException(
Expand Down
27 changes: 21 additions & 6 deletions src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ void commandlineWithCommandInConstructor() {
@Test
void executeBinaryOnPath() throws Exception {
// Maven startup script on PATH is required for this test
String binary = "mvn";
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
binary += ".cmd";
}
Comment on lines +81 to +84
Commandline cmd = new Commandline();
cmd.setWorkingDirectory(baseDir);
cmd.setExecutable("mvn");
assertEquals("mvn", cmd.getShell().getOriginalExecutable());
cmd.setExecutable(binary);
assertEquals(binary, cmd.getShell().getOriginalExecutable());
cmd.createArg().setValue("-version");
Process process = cmd.execute();
String out = IOUtil.toString(process.getInputStream());
Expand All @@ -94,11 +98,19 @@ void executeBinaryOnPath() throws Exception {
@Test
void execute() throws Exception {
// allow it to detect the proper shell here.
String executable = "echo";
Commandline cmd = new Commandline();
cmd.setWorkingDirectory(baseDir);
cmd.setExecutable("echo");
assertEquals("echo", cmd.getShell().getOriginalExecutable());
cmd.createArgument().setValue("Hello");
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
cmd.createArg().setValue("Hello");
executable = "cmd";
cmd.createArg().setValue("/X");
cmd.createArg().setValue("/C");
cmd.createArg().setValue("echo");
}
Comment on lines +104 to +110
cmd.setExecutable(executable);
assertEquals(executable, cmd.getShell().getOriginalExecutable());
cmd.createArg().setValue("Hello");

Process process = cmd.execute();
assertEquals("Hello", IOUtil.toString(process.getInputStream()).trim());
Expand Down Expand Up @@ -385,7 +397,10 @@ void dollarSignInArgumentPath() throws Exception {
cmd.getShell().setQuotedArgumentsEnabled(true);
cmd.setExecutable("cat");
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
cmd.setExecutable("dir");
cmd.setExecutable("cmd");
cmd.createArg().setLine("/X");
cmd.createArg().setLine("/C");
cmd.createArg().setLine("dir");
}
cmd.setWorkingDirectory(dir);
cmd.createArg().setLine("test$1.txt");
Expand Down
Loading