无法在Microsoft Powershell中使用`mvn -D`参数运行Maven,但可以在命令提示符下使用

问题描述

| 我正在尝试从命令行构建我们的Web项目,但是跳过了测试。我正在使用命令“ 0”。 当我从传统的黑白命令提示符(又称为DOS shell)运行命令时,该命令有效,但是当我从“ Windows PowerShell”的命令运行该命令时,出现以下错误
[ERROR] UnkNown lifecycle phase \".test.skip=true\". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-
artifact-id>[:<plugin-version>]:<goal>. 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,po
st-clean. -> [Help 1]
是什么导致这种差异,如何使PowerShell的行为类似于传统的命令提示符? 它在Windows 7上运行。     

解决方法

        当您遇到将PowerShell传递给控制台EXE的参数的解释问题时,请尝试使用PowerShell社区扩展随附的
echoargs.exe
实用程序。使用此工具,您可以看到PowerShell如何将参数提供给EXE,例如:
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>
简短答案-使用引号
\'-Dmaven.test.skip=true\'
    ,        根据此电子邮件主题:   通常,如果您将Powershell用于Maven,svn等,则最终   必须转义以破折号(-)开头的所有参数。的   Powershell中的转义字符是反引号。所以你最终得到了MVN   原型:创建-DgroupId = blah -DartifactId = blah。 ,\'-\'是一个   特殊字符,在运行时需要用反斜杠转义   Powershell控制台中的专家。     ,        以下对我有用。
$mvnArgs1 =\"mvn test -X -Dmaven.test.skip=true\".replace(\'-D\',\'`-D\')
Invoke-Expression $mvnArgs1
看起来-D是一种特殊字符,需要转义。 还请注意,-X可以正常工作,不需要转义。 注意replace命令中使用了单引号,如果使用双引号(即\“),则需要使用``。 我在Windows 7上使用Powershell 2.0(2.0.1.1)     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...