Cannot run Maven using `mvn -D` argument within Microsoft Powershell, but works in Command Prompt
Solution 1:
When you run into problems with PowerShell's interpretation of arguments to be passed to a console EXE, try using the echoargs.exe
utility that comes with the PowerShell Community Extensions. With this tool you can see how PowerShell supplies the arguments to the EXE e.g.:
PS> echoargs mvn clean install -Dmaven.test.skip=true
Arg 0 is <mvn>
Arg 1 is <clean>
Arg 2 is <install>
Arg 3 is <-Dmaven>
Arg 4 is <.test.skip=true>
PS> echoargs mvn clean install '-Dmaven.test.skip=true'
Arg 0 is <mvn>
Arg 1 is <clean>
Arg 2 is <install>
Arg 3 is <-Dmaven.test.skip=true>
Short answer - use quoting '-Dmaven.test.skip=true'
Solution 2:
According to this email thread:
Generally if you are using Powershell for Maven, svn etc you end up having to escape any arguments which start with a dash (-). The escape character in Powershell is a backtick. So you end up with mvn archetype:create `-DgroupId=blah `-DartifactId=blah. , the '-' is a special character that needs escaping with a back-tick when you run maven from Powershell console.
Solution 3:
The following works for me.
$mvnArgs1 ="mvn test -X -Dmaven.test.skip=true".replace('-D','`-D')
Invoke-Expression $mvnArgs1
It looks like the -D is a kind of a special character and requires escaping.
Note also that –X works fine and does not require escaping.
Note the use of single quote in the replace command, if you use double quotes (i.e. ") you need to use ``.
I am using Powershell 2.0 (2.0.1.1) on windows 7