如何通过 bash 脚本检测来自 ant/maven 的构建错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375133/
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
how to detect a build error from ant/maven via a bash script?
提问by fei
I am writing a bash script to automate the build process. There are two major build blocks, one is an ant task and one is a plain old mvn clean install
. I want to do something when there is build error coming from either of this two build processes.
我正在编写一个 bash 脚本来自动化构建过程。有两个主要的构建块,一个是 ant 任务,一个是普通的mvn clean install
. 当这两个构建过程中的任何一个出现构建错误时,我想做一些事情。
The problem is, these builds will contain test failures or errors from time to time, but the end result is successful. And I believe that the status code ($?) return by these processes should be 0 no matter the build fail or succeed, I could be wrong.
问题是,这些构建会不时包含测试失败或错误,但最终结果是成功的。而且我相信无论构建失败还是成功,这些进程返回的状态代码 ($?) 都应该是 0,我可能是错的。
So what is the best way for my script to detect the end result (build fail/succeed) without catching the false info during the mid build (test errors, etc) from them?
那么我的脚本检测最终结果(构建失败/成功)的最佳方法是什么,而不会在构建中期(测试错误等)期间捕获错误信息?
回答by Renaud
mvn clean test
if [[ "$?" -ne 0 ]] ; then
echo 'could not perform tests'; exit $rc
fi
$?
is a special shell variable that contains the exit code (whether it terminated successfully, or not) of the most immediate recently executed command.-ne
stands for "not equal". So here we are testing if the exit code frommvn clean
is not equal to zero.
$?
是一个特殊的 shell 变量,它包含最近执行的命令的退出代码(无论是否成功终止)。-ne
代表“不相等”。所以在这里我们测试退出代码mvn clean
是否不等于零。
回答by Rich Seller
There are a few issues against Maven 2 returning incorrect return codes (i.e. always returning 0). Notably MNG-3651that was fixed in Maven 2.0.9.
Maven 2 返回不正确的返回代码(即始终返回 0)存在一些问题。特别是在 Maven 2.0.9 中修复的MNG-3651。
In older versions, mvn.bat ended with this line:
在旧版本中, mvn.bat 以这一行结尾:
exit /B %ERROR_CODE%
From Maven 2.0.9 onwards, the last line was changed to this:
从 Maven 2.0.9 开始,最后一行改为:
cmd /C exit /B %ERROR_CODE%
So a non-0 return code is returned if the build fails. In the case of a build ERROR the return code is 1. If you are unable to upgrade to 2.0.9+, you could consider modifying mvn.bat as above to return the correct code.
因此,如果构建失败,则返回非 0 返回码。在构建错误的情况下,返回代码为 1。如果您无法升级到 2.0.9+,您可以考虑修改 mvn.bat 以返回正确的代码。
回答by ire_and_curses
According to the Ant manual:
根据蚂蚁手册:
the ant start up scripts (in their Windows and Unix version) return the return code of the java program. So a successful build returns 0, failed builds return other values.
ant 启动脚本(在它们的 Windows 和 Unix 版本中)返回 java 程序的返回码。所以一个成功的构建返回 0,失败的构建返回其他值。
Maven also returns a non-zero exit code on error. Here's a linkshowing how to interrogate this status using the Maven Invocation API.
Maven 还会在出错时返回一个非零退出代码。这是一个链接,显示了如何使用 Maven Invocation API 询问此状态。
So it seems to me that you should be able to explicitly handle the return codes in your script . Presumably you can ignore error codes relating to tests etc. if those are not a concern to you.
所以在我看来,您应该能够明确处理脚本中的返回代码。如果您不关心这些,大概您可以忽略与测试等相关的错误代码。
exec
error codes in Ant are operating system-specific. These may help you:
exec
Ant 中的错误代码是特定于操作系统的。这些可能会帮助您:
回答by codefinger
Here is exactly what I do to get the result you want.
这正是我为获得您想要的结果所做的工作。
<exec executable="${env.M2_HOME}/bin/mvn" dir="${basedir}"
failonerror="true" osfamily="unix">
<arg value="-DskipTests=${argSkipTests}"/>
<arg value="-Doffline=${argOffline}"/>
<arg line="${projectsLine}"/>
<arg line="${resumeFromLine}"/>
<arg line="${alsoMakeLine}"/>
<arg line="${alsoMakeDependentsLine}"/>
<arg line="${commandsLine}"/>
</exec>
回答by Māris Rubenis
Correct solution for unix/linux:
unix/linux 的正确解决方案:
mvn clean install
rc=$?
if [ $rc -ne 0 ] ; then
echo Could not perform mvn clean install, exit code [$rc]; exit $rc
fi
The "if" statement itself is a command and if it is successful, it will reset the $? variable to 0. Same goes for echo. So, you have to use an intermediary local var, for example $rc to store the return code from "mvn clean install", then it can be passed to the "exit" command as well.
“if”语句本身是一个命令,如果成功,它将重置 $? 变量为 0。回声也是如此。因此,您必须使用一个中间本地变量,例如 $rc 来存储来自“mvn clean install”的返回代码,然后它也可以传递给“exit”命令。
回答by Will
There's a command built-in to bash which performs exactly this.
bash 内置了一个命令,它完全执行此操作。
# exit when any command fails
set -e
I put this at the top of my bash scripts, which I copied from this excellent resource.
我把它放在我的 bash 脚本的顶部,我从这个优秀的资源中复制了它。
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT