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

查看:88
本文介绍了将退出代码从已执行的批处理文件传播回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不起作用.建议的解决方法是从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)不终止调用方的外壳程序,并且(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 off echo [batch] exit /b 2 还有您上面的ant脚本和exec,并返回了我批次中的错误代码.一切正常.
exec returned: 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

对此进行了测试:
Windows 7 64-bit上的Apache Ant(TM) version 1.9.3 compiled on December 23 2013

This was tested on:
Apache Ant(TM) version 1.9.3 compiled on December 23 2013 on Windows 7 64-bit

您应该粘贴批处理文件和运行蚂蚁得到的实际结果.您正在使用的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

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

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