Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ant, how do I get the return value from an exec?

Tags:

ant

<target name="CheckState">      <exec executable="${App.path}"/> </target> 

In this task, the executable will return a value which will indicate the state of my app. How could I get the value returned in the Ant build file. I will use this value to determine some behaviour.

like image 460
Hyden Avatar asked Dec 03 '10 09:12

Hyden


People also ask

How do I call ant target from command line?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

How do ant builds work?

Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets. A target can be directly invoked via Ant.

What are Ant scripts?

Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets.


1 Answers

Use the resultproperty and failonerror attributes of the exec task, e.g.:

<target name="CheckState">      <exec executable="${App.path}"            resultproperty="App.state"            failonerror="false"/>      <echo message="App state was: ${App.state}" /> </target> 

Quoting from the exec task docs Errors and return codes:

By default the return code of an exec is ignored; when you set failonerror="true" then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can set resultproperty to the name of a property and have it assigned to the result code (barring immutability, of course).

If the attempt to start the program fails with an OS dependent error code, then halts the build unless failifexecutionfails is set to false. You can use that to run a program if it exists, but otherwise do nothing.

What do those error codes mean? Well, they are OS dependent. On Windows boxes you have to look at the documentation; error code 2 means 'no such program', which usually means it is not on the path. Any time you see such an error from any Ant task, it is usually not an Ant bug, but some configuration problem on your machine.

like image 148
martin clayton Avatar answered Nov 06 '22 16:11

martin clayton