首页 > 解决方案 > 如何使用多行脚本执行 mvn?

问题描述

我在一个 Maven 项目中使用 JMeter 编写测试用例。我想为每个测试用例提供 shell 和批处理脚本示例,以便其他人可以轻松地尝试这些测试。另一方面,这些脚本是关于如何通过参数配置测试用例的示例。多行格式的操作系统能够轻松理解脚本参数的重要性,尤其是当我将拥有具有 10 多个参数的脚本时。

以批处理格式执行脚本的 mvn 命令如下所示。批处理格式就像魅力一样。

mvn clean verify -DjMeterScript=JMXFileName.jmx ^
  -Djmeter.url=test.environment.url ^
  -Djmeter.port=6080 ^
  -Djmeter.protocol=http ^
  -Djmeter.debugMode=1 ^
  -Djmeter.viaFiddler=1 ^

但是,shell 格式不起作用。根据互联网终极知识,多行 shell 脚本的格式应如下所示。

mvn clean verify -DjMeterScript=JMXFileName.jmx \
  -Djmeter.url=test.environment.url \
  -Djmeter.port=6080 \
  -Djmeter.protocol=http \
  -Djmeter.debugMode=1 \
  -Djmeter.viaFiddler=1 \

./shellScriptName.sh当我使用包含上述示例的脚本执行脚本时,出现以下错误。

问题是如何正确格式化调用 maven 的 shell 脚本?

更新 - 它只发生在 Windows 10 - 带有 Hyper 的 WSL 上

错误信息

[ERROR] Unknown lifecycle phase "
[ERROR] ". 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, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

标签: shellmaven

解决方案


我遇到了同样的问题,您应该使用 ^ 符号来换行,但也要包含所有带引号的参数。我认为您不必在最后一行添加 ^ 符号。

因此,在您的情况下,它应该如下所示:

mvn clean verify -DjMeterScript="JMXFileName.jmx" ^
  -D"jmeter.url"="test.environment.url ^
  -D"jmeter.port"=6080 ^
  -D"jmeter.protocol"=http ^
  -D"jmeter.debugMode"=1 ^
  -D"jmeter.viaFiddler"=1 

推荐阅读