简体   繁体   English

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

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

I am trying to build our web project from the commandline but skipping the testing.我正在尝试从命令行构建我们的 web 项目,但跳过了测试。 I am using the command mvn clean install -Dmaven.test.skip=true .我正在使用命令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]

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 上运行。

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 .当您遇到 PowerShell 对要传递给控制台 EXE 的 arguments 的解释出现问题时,请尝试使用PowerShell 社区扩展附带的echoargs.exe实用程序。 With this tool you can see how PowerShell supplies the arguments to the EXE eg:使用此工具,您可以看到 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>

Short answer - use quoting '-Dmaven.test.skip=true'简短的回答- 使用引用'-Dmaven.test.skip=true'

According to this email thread :根据这个 email 线程

Generally if you are using Powershell for Maven, svn etc you end up having to escape any arguments which start with a dash (-).通常,如果您将 Powershell 用于 Maven、svn 等,您最终将不得不逃避任何 ZDBC11CAABD8BDA99F77E6 以 DAdash882E 开头的 ZDBC11CAABD8BDA99F77E6 FB4E The escape character in Powershell is a backtick. Powershell 中的转义字符是反引号。 So you end up with mvn archetype:create `-DgroupId=blah `-DartifactId=blah.所以你最终得到 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 时,需要带反引号的 escaping。

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.看起来 -D 是一种特殊字符,需要 escaping。
Note also that –X works fine and does not require escaping.另请注意,–X 工作正常,不需要 escaping。 Note the use of single quote in the replace command, if you use double quotes (ie ") you need to use ``.注意替换命令中使用单引号,如果使用双引号(即“),则需要使用``。

I am using Powershell 2.0 (2.0.1.1) on windows 7我在 windows 7 上使用 Powershell 2.0 (2.0.1.1)

In the PowerShell the hyphen (-) will not be consider as linux terminals, so you need to escape it using this (`) before each hyphen.在 PowerShell 中,连字符 (-) 不会被视为 linux 终端,因此您需要在每个连字符前使用此 (`) 对其进行转义。

For example:-例如:-

Below command will not work in PowerShell以下命令在 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 

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

相关问题 无法通过命令提示符运行 powershell 命令 - Cannot run powershell command via command prompt exec-maven-plugin无法在Windows计算机上运行“ mvn”命令 - exec-maven-plugin cannot run “mvn” command on Windows machine Maven在Powershell中工作,但在Cmd Prompt中不工作 - Maven works in Powershell but not Cmd Prompt Maven:未找到 mvn 命令 - Maven: mvn command not found mvn在cmd提示符下工作,但不能批量工作 - mvn works in cmd prompt but not in batch 为什么Windows命令提示符说“更多? “当我运行一个带有d ^参数的程序时? - Why does the Windows command prompt say “More? ” when I run a program with an argument of d^? 当我从CreateProcess运行NETSH时,“系统找不到指定的文件”,但它在命令提示符下运行正常吗? - Got “The system cannot find the file specified” when I run NETSH from CreateProcess but it works ok on Command Prompt? PowerShell:如何从PowerShell以管理员身份运行远程命令提示符命令? - PowerShell : How to run remote command prompt commands as admin from PowerShell? 符号链接无法在 Powershell 5.1 中创建,但可以通过 Powershell 7 和命令提示符创建 - Symlinks cannot be created in Powershell 5.1 but can be created by Powershell 7 and Command Prompt 如果在命令提示符下运行但不在脚本中运行,则命令有效。 - Command works if run through the command prompt but not in a script..?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM