bash exec maven 插件:退出代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38738560/
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-18 14:57:41  来源:igfitidea点击:

exec maven plugin: exit code

bashmavenexec-maven-plugin

提问by Schütze

I am having the following plugin to run a .shscript:

我有以下插件来运行.sh脚本:

<plugin>
  <artifactId>exec-maven-plugin</artifactId>
  <groupId>org.codehaus.mojo</groupId>
  <executions>
    <execution>
      <id>deploy-bundles</id>
      <phase>install</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>${basedir}/deploy.sh</executable>
        <successCodes>
            <successCode>0</successCode> <-- Not working
        </successCodes>
      </configuration>
    </execution>
  </executions>
</plugin>

which copies some folders and files to certain locations. It works. However, just in case, I want to have a fail-on-error mechanism. I already have set -ecommand in my .shscript, but I want a maven solution too. I heard that there is a tagcalled successCodes, I try to incorporate it. But no luck so far. Could someone point out the correct way of doing it?

它将一些文件夹和文件复制到某些位置。有用。但是,为了以防万一,我希望有一个失败错误机制。set -e我的.sh脚本中已经有了命令,但我也想要一个 Maven 解决方案。我听说有一个名为的标签successCodes,我尝试合并它。但到目前为止没有运气。有人能指出正确的做法吗?

Edit:My .shscript looks like this:

编辑:我的.sh脚本如下所示:

cp ../template/config.properties $component/conf
cp ../../runtime/group1/group1.mw/conf/log4j.xml $component/conf
# if the component is planning, create an additional folder called plans
if [[ $component == *".planning"* ]] 
then
mkdir -p $component/plans
    # copy all the plans here
    cp ../../mission.planning/plans/* $component/plans
fi  

where it is expected to fail in case these folders/files are not there. So, as a test, I manually change the paths above and expect it to fail. It does fail the execution process and tells me the error (since I have set -ecommand in the .shscript), however maven reports is as "success".

如果这些文件夹/文件不存在,预计会失败。因此,作为测试,我手动更改了上面的路径并预计它会失败。它确实使执行过程失败并告诉我错误(因为我set -e.sh脚本中有命令),但是 maven 报告为“成功”。

回答by Tunaki

That's not an issue with the Exec Maven Plugin, but an issue with the handling of exit codes in the Shell script.

这不是 Exec Maven 插件的问题,而是在 Shell 脚本中处理退出代码的问题。

The successCodesparameter is useful in situation where an executable has an exit code different than 0 for "successful execution":

successCodes参数在可执行文件的退出代码不同于“成功执行”的 0 的情况下很有用:

Exit codes to be resolved as successful execution for non-compliant applications (applications not returning 0 for success).

对于不合规的应用程序(应用程序不返回 0 表示成功),退出代码将被解析为成功执行。

The default behaviouris to consider the exit code 0 as a successful execution, and all the others as a failure,and the plugin will fail the build in that case.

默认行为是将退出代码 0 视为成功执行,将所有其他代码视为失败,在这种情况下插件将无法构建。

In your Shell script, you have multiple commands, each of which has its own exit code. Without any extra handling, the exit code of the script itself, as a whole, is the exit code of the last command. Therefore, even if one of the command failed (so its exit code is not zero) a successful command after that will reset the script exit code to 0. You can test that by invoking the script on the command line outside of Maven, and echo the $?variable, which contains the exit code.

在您的 Shell 脚本中,您有多个命令,每个命令都有自己的退出代码。没有任何额外的处理,脚本本身的退出代码,作为一个整体,就是最后一个命令的退出代码。因此,即使其中一个命令失败(因此其退出代码不为零),之后的成功命令也会将脚本退出代码重置为 0。您可以通过在 Maven 外部的命令行上调用脚本来测试这一点,并回显包含退出代码$?变量。

As such, you need to test for the exit code of each invoked command that might fail inside your Shell. (You can also use a bit of arithmeticto accumulate each exit codes.)

因此,您需要测试每个调用命令的退出代码,这些命令可能会在您的 Shell 中失败。(您也可以使用一些算术来累积每个退出代码。)