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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6347985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:03:30  来源:igfitidea点击:

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

windowscommand-linepowershellmavencommand

提问by Sled

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.

我正在尝试从命令行构建我们的 Web 项目,但跳过了测试。我正在使用命令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:

当我从传统的黑白命令提示符(又名 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]

[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]

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

是什么导致了这种差异,我如何让 PowerShell 像传统的命令提示符一样运行?

This is running on Windows 7.

这是在 Windows 7 上运行的。

回答by Keith Hill

When you run into problems with PowerShell's interpretation of arguments to be passed to a console EXE, try using the echoargs.exeutility that comes with the PowerShell Community Extensions. With this tool you can see how PowerShell supplies the arguments to the EXE e.g.:

当您遇到 PowerShell 对要传递到控制台 EXE 的参数的解释的问题时,请尝试使用PowerShell Community Extensionsechoargs.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>

Short answer- use quoting '-Dmaven.test.skip=true'

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

回答by katrash

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.

通常,如果您将 Powershell 用于 Maven、svn 等,您最终必须转义任何以破折号 (-) 开头的参数。Powershell 中的转义字符是反引号。所以你最终得到 mvn archetype:create `-DgroupId=blah `-DartifactId=blah。, '-' 是一个特殊字符,当您从 Powershell 控制台运行 maven 时,需要使用反引号进行转义。

回答by Haim Raman

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 ``.

看起来 -D 是一种特殊字符,需要转义。
还要注意 -X 工作正常,不需要转义。注意replace命令中单引号的使用,如果使用双引号(即")则需要使用``。

I am using Powershell 2.0 (2.0.1.1) on windows 7

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