Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagating exit code from exec'd batch file back to ant

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>

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.

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%

to

exit %ERROR_CODE%

However, as one commenter states:

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.

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?

Update

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
like image 323
JimN Avatar asked Nov 02 '22 03:11

JimN


1 Answers

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

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

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.

Do the following

  • Run the sbt.bat publish from the command line
  • Immediately after that run echo %ERRORLEVEL% and note the result

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

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

like image 159
Slav Avatar answered Nov 05 '22 14:11

Slav