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
19 changes: 9 additions & 10 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 @@ -490,11 +489,11 @@ public String[] getArguments() {

@Override
public String toString() {
return StringUtils.join(getShellCommandline(), " ");
return StringUtils.join(getRawCommandline(), " ");
}

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 thread
axel3rd marked this conversation as resolved.
Resolved
if (!workingDir.exists()) {
throw new CommandLineException(
Expand All @@ -592,7 +591,7 @@ public Process execute() throws CommandLineException {
"Path \"" + workingDir.getPath() + "\" does not specify a directory.");
}

process = Runtime.getRuntime().exec(getCommandline(), environment, workingDir);
process = Runtime.getRuntime().exec(getRawCommandline(), environment, workingDir);
}
} catch (IOException ex) {
throw new CommandLineException("Error while executing process.", ex);
Expand Down
29 changes: 21 additions & 8 deletions src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.shell.BourneShell;
import org.codehaus.plexus.util.cli.shell.CmdShell;
import org.codehaus.plexus.util.cli.shell.Shell;
Expand Down Expand Up @@ -78,10 +77,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 thread
axel3rd marked this conversation as resolved.
Resolved
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 +97,18 @@ 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)) {
executable = "cmd";
cmd.createArg().setValue("/X");
cmd.createArg().setValue("/C");
cmd.createArg().setValue("echo");
}
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 +395,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 Expand Up @@ -493,7 +506,7 @@ private static void executeCommandLine(Commandline cmd) throws Exception {
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();

try {
System.out.println("Command line is: " + StringUtils.join(cmd.getShellCommandline(), " "));
System.out.println("Command line is: " + cmd.toString());

int exitCode = CommandLineUtils.executeCommandLine(cmd, new DefaultConsumer(), err);

Expand Down
Loading