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

Sle*_*led 43 windows powershell command-line command maven

我试图从命令行构建我们的Web项目,但跳过测试.我正在使用该命令mvn clean install -Dmaven.test.skip=true.

当我从传统的黑白命令提示符(也称为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上运行的.

Kei*_*ill 105

当您遇到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>
Run Code Online (Sandbox Code Playgroud)

简短回答 - 使用引用'-Dmaven.test.skip=true'

  • 哇,*很棒*回答,希望我能两次投票. (2认同)
  • 刚碰到这个.在'='和'.'上分割参数有什么可能的原因?对我来说似乎完全是愚蠢的. (2认同)

kat*_*ash 9

根据这封电子邮件主题:

通常,如果你使用Powershell for Maven,svn等,你最终必须转义任何以短划线( - )开头的参数.Powershell中的逃脱角色是一个反击.所以你最终得到了mvn archetype:create` -DgroupId = blah` -DartifactId = blah.,' - '是一个特殊的角色,当你从Powershell控制台运行maven时需要使用后退.


Hai*_*man 6

以下适用于我.

$mvnArgs1 ="mvn test -X -Dmaven.test.skip=true".replace('-D','`-D')
Invoke-Expression $mvnArgs1
Run Code Online (Sandbox Code Playgroud)

它看起来像-D是一种特殊的角色,需要转义.
另请注意-X工作正常,不需要转义.注意在replace命令中使用单引号,如果你使用双引号(即")",你需要使用``.

我在Windows 7上使用Powershell 2.0(2.0.1.1)