将退出代码从 exec'd 批处理文件传播回 ant [英] Propagating exit code from exec'd batch file back to ant

查看:21
本文介绍了将退出代码从 exec'd 批处理文件传播回 ant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 ant 调用 sbt.我正在使用exec"任务执行此操作,如下所示:

I need to call sbt from ant. I'm doing this with the "exec" task as follows:

  <target name="publish-jar">
    <exec executable="sbt.bat" failonerror="true">
      <arg value="publish"/>
    </exec>
  </target>

如果 sbt 任务失败,我需要 ant 任务失败",这就是使用 failonerror="true" 的原因.但是,这不起作用.当 sbt 任务失败时,ant 不会报告失败的构建.

I need the ant task to "fail" if the sbt task fails, which is why failonerror="true" is being used. However, this does not work. When the sbt task fails, ant does not report a failed build.

这看起来像这里讨论的问题:Ant exec resultproperty is not working.建议的解决方法是从 sbt.bat 中删除/B".换句话说,改变:

This looks like the problem discussed here: Ant exec resultproperty is not working. The suggested workaround is to remove "/B" from the sbt.bat. In other words, change:

exit /B %ERROR_CODE%

exit %ERROR_CODE%

然而,正如一位评论者所说:

However, as one commenter states:

这样做的缺点是,如果您直接运行批处理文件进行测试,它将终止您的 shell.您可以在批处理文件中使用 if 和 arg 在 ant 调用它时选择 \b 并在不调用时正常退出.

The downside of this is that if you run the batch file directly for testing it will terminate your shell. you could use an if and an arg in the batch file to select \b when ant calls it and normal exit when not.

问题:是否有修复方法,当失败发生时:(1) 不终止调用者的 shell 并且 (2) 将退出代码传播给 ant?

Question: Is there a fix which, when a failure happens: (1) does not terminate the caller's shell AND (2) propagates the exit code to ant?

这是运行我的 ant 任务的输出.实际错误在这里并不重要(我故意不配置要发布到的存储库,以强制错误):

Here is the output of running my ant task. The actual error is not important here (I'm purposely not configuring a repository to publish to, to force an error):

C:\dev\la\sdf3\modules\test>ant publish-jar
Buildfile: C:\dev\la\sdf3\modules\test\build.xml

publish-jar:
     [exec] [info] Loading global plugins from C:\Users\jn\.sbt\0.13\plugins
     [exec] [info] Set current project to test (in build file:/C:/dev/la/sdf3/modules/test/)
     [exec] :: loading settings :: file = C:\dev\la\sdf3\modules\ivysettings.xml

     [exec] [info] :: delivering :: com.jn#test;SNAPSHOT ::
SNAPSHOT :: integration :: Fri Mar 14 08:45:58 HST 2014
     [exec] [info]      delivering ivy file to C:\dev\la\sdf3\modules\com.jn\target\scala-2.10\ivy-SNAPSHOT.xml
     [exec] java.lang.RuntimeException: Repository for publishing is not specified.
     [exec]     at scala.sys.package$.error(package.scala:27)
     [exec]     at sbt.Classpaths$$anonfun$getPublishTo$1.apply(Defaults.scala:1203)
     [exec]     at sbt.Classpaths$$anonfun$getPublishTo$1.apply(Defaults.scala:1203)
     [exec]     at scala.Option.getOrElse(Option.scala:120)
     [exec]     at sbt.Classpaths$.getPublishTo(Defaults.scala:1203)
     [exec]     at sbt.Classpaths$$anonfun$57.apply(Defaults.scala:1037)
     [exec]     at sbt.Classpaths$$anonfun$57.apply(Defaults.scala:1037)
     [exec]     at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
     [exec]     at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
     [exec]     at sbt.std.Transform$$anon$4.work(System.scala:64)
     [exec]     at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
     [exec]     at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
     [exec]     at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
     [exec]     at sbt.Execute.work(Execute.scala:244)
     [exec]     at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
     [exec]     at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
     [exec]     at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
     [exec]     at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
     [exec]     at java.util.concurrent.FutureTask.run(FutureTask.java:262)
     [exec]     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
     [exec]     at java.util.concurrent.FutureTask.run(FutureTask.java:262)
     [exec]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
     [exec]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
     [exec]     at java.lang.Thread.run(Thread.java:744)
     [exec] [error] (*:publishConfiguration) Repository for publishing is not specified.
     [exec] [error] Total time: 0 s, completed Mar 14, 2014 8:45:59 AM

BUILD SUCCESSFUL
Total time: 4 seconds

推荐答案

我只是做了一个简单的批处理:
@echo 关闭回声 [批处理]退出/b 2上面的 ant 脚本和 exec 返回了我的批处理中的错误代码.一切正常.
exec 返回:2

I just did a simple batch:
@echo off echo [batch] exit /b 2 And your ant script from above, and exec returned with the error code from my batch. Everything worked fine.
exec returned: 2

这是在:
Apache Ant(TM) version 1.9.3 于 2013 年 12 月 23 日编译Windows 7 64 位

您应该粘贴批处理文件和运行 ant 的实际结果.您使用的 Ant 版本有问题,或者(很可能)批处理文件有问题.

You should paste your batch file and the actual result you get from running your ant. Either there is something wrong with the version of Ant you are using, or (most likely) something wrong with the batch file.

执行以下操作

  • 从命令行运行 sbt.bat publish
  • 立即运行 echo %ERRORLEVEL% 并记录结果
  • Run the sbt.bat publish from the command line
  • Immediately after that run echo %ERRORLEVEL% and note the result

如果您得到 0,那么您的批次就是问题所在.

If you are getting 0, your batch is the problem.

或者,有一篇文章在这里 描述了使用宏定义的解决方法

Alternatively, there is an article here that describes a workaround with a macrodef

这篇关于将退出代码从 exec'd 批处理文件传播回 ant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆