bash 从shell脚本执行Maven任务并获取错误代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13377436/
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
Executing Maven task from shell script and getting error codes
提问by cduggan
I'm executing a Maven deploy task from a bash script however even if the Maven task fails the script will continue and complete without errors.
我正在从 bash 脚本执行 Maven 部署任务,但是即使 Maven 任务失败,脚本也会继续并完成而不会出错。
I have tried the -e flag but that causes the deploy to fail. I also tried the following (pseudo code)
我试过 -e 标志,但这会导致部署失败。我也尝试了以下(伪代码)
result_code= mvn deploy
if [$result_code -gt 0];then
exit 1
Any suggestions how i can identify if the deploy was successful?
我有什么建议可以确定部署是否成功?
回答by Kent
result_code=mvn deployis not the way to get return status
result_code=mvn deploy不是获得退货状态的方法
you can try e.g. :
你可以尝试例如:
#!/bin/bash
mvn deploy
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "Deployment Successful"
else
echo "Deployment Failed"
fi
回答by Harun
In addition if anyone using Windows 10, here is the example which I use:
此外,如果有人使用 Windows 10,这里是我使用的示例:
mvn deploy
if not %ERROR_CODE%==0 goto error
echo SUCCESS
goto end
:error
echo FAILED
:end
回答by Sridhar Sarnobat
Just one other possible reason a person's mvn task may return 0 despite failing: be careful about piping the output of maven to other programs. For example, I'm using grcat(which grcis build on top of), which will always return exit code 0;
一个人的 mvn 任务尽管失败也可能返回 0 的另一个可能原因是:小心将 maven 的输出传送到其他程序。例如,我正在使用grcat(grc构建在其之上),它将始终返回退出代码 0;
\mvn compile | grcat ~/conf.mvn
I'm not sure how to retain my nice color coding of the output. There is color_maven.sh out there but that has other issues.
我不确定如何保留我漂亮的输出颜色编码。有 color_maven.sh ,但还有其他问题。

