Skip to content

Commit 5f9a5ec

Browse files
committed
Revert usage of cmd on Windows
1 parent 900acba commit 5f9a5ec

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/main/java/org/codehaus/plexus/util/cli/Commandline.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -491,34 +491,32 @@ public String[] getEnvironmentVariables()
491491
}
492492

493493
/**
494+
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
495+
* @deprecated Use {@link org.codehaus.plexus.util.cli.Commandline#getRawCommandline()} method instead.
494496
* @return Returns the executable and all defined arguments.
495-
* For Windows Family, {@link Commandline#getShellCommandline()} is returned
496497
*/
498+
@Deprecated
497499
public String[] getCommandline()
498500
{
499-
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
500-
{
501-
return getShellCommandline();
502-
}
503-
504501
return getRawCommandline();
505502
}
506503

507504
/**
508-
* Returns the executable and all defined arguments.
505+
* Returns the executable and all defined arguments.<br>
506+
* Warning: For built-in commands like 'echo' on {@link Os#FAMILY_WINDOWS}, use <code>cmd /X /C echo</code>
509507
* @return the command line as array not escaped neither quoted
510508
*/
511509
public String[] getRawCommandline()
512510
{
513511
final String[] args = getArguments();
514-
String executable = getLiteralExecutable();
512+
String executableTmp = getLiteralExecutable();
515513

516-
if ( executable == null )
514+
if ( executableTmp == null )
517515
{
518516
return args;
519517
}
520518
final String[] result = new String[args.length + 1];
521-
result[0] = executable;
519+
result[0] = executableTmp;
522520
System.arraycopy( args, 0, result, 1, args.length );
523521
return result;
524522
}
@@ -568,7 +566,7 @@ public String toString()
568566

569567
public int size()
570568
{
571-
return getCommandline().length;
569+
return getRawCommandline().length;
572570
}
573571

574572
@Override
@@ -668,7 +666,7 @@ public Process execute()
668666
{
669667
if ( workingDir == null )
670668
{
671-
process = Runtime.getRuntime().exec( getCommandline(), environment, workingDir );
669+
process = Runtime.getRuntime().exec( getRawCommandline(), environment, workingDir );
672670
}
673671
else
674672
{

src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ public void testExecuteBinaryOnPath()
7777
throws Exception
7878
{
7979
// Maven startup script on PATH is required for this test
80+
String binary = "mvn";
81+
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
82+
{
83+
binary += ".cmd";
84+
}
8085
Commandline cmd = new Commandline();
8186
cmd.setWorkingDirectory( baseDir );
82-
cmd.setExecutable( "mvn" );
83-
assertEquals( "mvn", cmd.getShell().getOriginalExecutable() );
87+
cmd.setExecutable( binary );
88+
assertEquals( binary, cmd.getShell().getOriginalExecutable() );
8489
cmd.createArg().setValue( "-version" );
8590
Process process = cmd.execute();
8691
String out = IOUtil.toString( process.getInputStream() );
@@ -94,11 +99,20 @@ public void testExecute()
9499
throws Exception
95100
{
96101
// allow it to detect the proper shell here.
102+
String executable = "echo";
97103
Commandline cmd = new Commandline();
98104
cmd.setWorkingDirectory( baseDir );
99-
cmd.setExecutable( "echo" );
100-
assertEquals( "echo", cmd.getShell().getOriginalExecutable() );
101-
cmd.createArgument().setValue( "Hello" );
105+
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
106+
{
107+
cmd.createArg().setValue( "Hello" );
108+
executable = "cmd";
109+
cmd.createArg().setValue("/X");
110+
cmd.createArg().setValue("/C");
111+
cmd.createArg().setValue("echo");
112+
}
113+
cmd.setExecutable( executable );
114+
assertEquals( executable, cmd.getShell().getOriginalExecutable() );
115+
cmd.createArg().setValue( "Hello" );
102116

103117
Process process = cmd.execute();
104118
assertEquals( "Hello", IOUtil.toString( process.getInputStream() ).trim() );
@@ -460,7 +474,10 @@ public void testDollarSignInArgumentPath()
460474
cmd.setExecutable( "cat" );
461475
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
462476
{
463-
cmd.setExecutable( "dir" );
477+
cmd.setExecutable( "cmd" );
478+
cmd.createArg().setLine( "/X" );
479+
cmd.createArg().setLine( "/C" );
480+
cmd.createArg().setLine( "dir" );
464481
}
465482
cmd.setWorkingDirectory( dir );
466483
cmd.createArg().setLine( "test$1.txt" );

0 commit comments

Comments
 (0)