繁体   English   中英

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

[英]Cannot run Maven using `mvn -D` argument within Microsoft Powershell, but works in Command Prompt

我正在尝试从命令行构建我们的 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 上运行。

当您遇到 PowerShell 对要传递给控制台 EXE 的 arguments 的解释出现问题时,请尝试使用PowerShell 社区扩展附带的echoargs.exe实用程序。 使用此工具,您可以看到 PowerShell 如何将 arguments 提供给 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'

根据这个 email 线程

通常,如果您将 Powershell 用于 Maven、svn 等,您最终将不得不逃避任何 ZDBC11CAABD8BDA99F77E6 以 DAdash882E 开头的 ZDBC11CAABD8BDA99F77E6 FB4E Powershell 中的转义字符是反引号。 所以你最终得到 mvn archetype:create `-DgroupId=blah `-DartifactId=blah。 , '-' 是一个特殊字符,当您从 Powershell 控制台运行 maven 时,需要带反引号的 escaping。

以下对我有用。

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

看起来 -D 是一种特殊字符,需要 escaping。
另请注意,–X 工作正常,不需要 escaping。 注意替换命令中使用单引号,如果使用双引号(即“),则需要使用``。

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

在 PowerShell 中,连字符 (-) 不会被视为 linux 终端,因此您需要在每个连字符前使用此 (`) 对其进行转义。

例如:-

以下命令在 PowerShell 中不起作用

mvn clean install -Dmaven.test.skip=true 

取而代之的是,您需要在连字符 (-) 之前添加此 (`),如下所示

mvn clean install `-Dmaven.test.skip=true 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM