Cannot run Maven using `mvn -D` argument within Microsoft Powershell, but works in Command Prompt
Asked Answered
L

4

73

I am trying to build our web project from the commandline but skipping the testing. I am using the command mvn clean install -Dmaven.test.skip=true.

When I run the command from the traditional black & white Command Prompt (aka DOS shell) the command works, but when I run it from the command from "Windows PowerShell" I get the following error:

[ERROR] Unknown lifecycle phase ".test.skip=true". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepar e-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy, pre-clean, clean, post-clean. -> [Help 1]`

What is causing this discrepancy and how do I get PowerShell to behave like the traditional Command Prompt?

This is running on Windows 7.

Lampley answered 14/6, 2011 at 18:7 Comment(0)
B
157

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'

Bianco answered 15/6, 2011 at 0:32 Comment(6)
Just ran into this. What possible reason is there for splitting parameters on '=' and '.'? Seems utterly stupid to me.Johanna
Good answer. Any idea why powershell splits -Dmaven.test.skip=true on the first period but not subsequent ones?Gaye
Is it necessary to use the backtick for all occurrances of dash sign for args, or is it Maven specific? Would this apply to git dash commands also?Ticknor
It is killing me when I end up with Windows-specific problemsPerpend
Another way to do this is to use the --% operator which causes PowerShell on Windows to parse more like cmd.exe. Note that you don't get PS features like $var substitution. If you need var substitution you can do it using env vars e.g.: $env:SKIPTEST='true'; mvn --% clean install -Dmaven.test.skip=%SKIPTEST%Bianco
I wasted several hours debugging. Thank you Microsoft 👍Affianced
I
13

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.

Implode answered 16/2, 2015 at 19:9 Comment(0)
W
8

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

Wiggler answered 14/11, 2012 at 7:56 Comment(0)
C
4

In the PowerShell the hyphen (-) will not be consider as linux terminals, so you need to escape it using this (`) before each hyphen.

For example:-

Below command will not work in PowerShell

mvn clean install -Dmaven.test.skip=true 

Instead of this you need to add this (`) before hyphen (-) like below

mvn clean install `-Dmaven.test.skip=true 
Ceremonious answered 26/12, 2022 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.