How to get %ERRORLEVEL% from batch file in Ant - ant

I want to know if its possible to get the return value from batch file in Ant build xml.
My batch file returns %ERRORLEVEL% value (batch file returns 2 in my case). I want to know if it's possible to capture this and mark as error in Ant. Below is the code snippet I use:
<exec executable = "cmd">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
Currently after the batch file call, build is reported as success always. It looks like Ant is not processing the %ERRORLEVEL% or I am not sure. How I can make Ant process the %ERRORLEVEL%?

Use the resultproperty and failonerror. By default, the errocode is ignored.
<property name="Batcherrcode" value="0"/>
<exec executable = "cmd" failonerror="true" resultproperty="Batcherrcode">
<arg value="/c"/>
<arg value="C:\workspace\Build\cross_Compile.bat"/>
</exec>
<echo message="Error Code:=${Batcherrorcode}" />

Related

Expanding parameters in Apache Ant exec task

I have a build that needs a task for starting a process, and one for killing it at the end.
I have a file with the process id in it, but cannot figure out how to make ant expand the command substitution in order to pass the contents of that file to the kill command.
I have tried:
<target name="kill process">
<exec executable="kill">
<arg value="`cat process-file`"/>
</exec>
...
And:
<target name="kill process">
<exec executable="kill">
<arg value="$(cat process-file)"/>
</exec>
but both are converted to string litterals, and so result in:
[exec] kill: failed to parse argument: '$(cat process-file)'
Is there a way to make ant expand these? Or an altogether different route to accomplish this?
You can use Ant's loadfile task to read the file's content into a property.
<loadfile srcFile="process-file" property="pid">
<filterchain>
<striplinebreaks/>
</filterchain>
</loadfile>
<exec executable="kill">
<arg value="${pid}"/>
</exec>
EDIT: added filterchain to deal with additional whitespace

ant send yes or no

I am just stuck at thing scenario,
I have a batch file which upon running will ask for confirmation like " press y/n ". Now i am to automate that batch file using ant. so, my code looks something like this
<exec executable="cmd.exe" dir="${base.dir}" >
<arg line="/c run.bat" />
</exec>
but I have no idea how to pass the keyboard value 'y' to it in run time
please help me out
Use the input task?
Use combination of input task and inputstring parameter of exec task.
<input
message="All data is going to be deleted from DB continue (y/n)?"
validargs="y,n"
addproperty="do.delete"
/>
<exec
executable="cmd.exe"
dir="${base.dir}"
inputstring="${do.delete}"
>
<arg line="/c run.bat" />
</exec>
Just provide /y input as you would without ant:
<exec executable="cmd.exe" dir="${base.dir}" >
<arg line="/c run.bat /y "/>
</exec>
Another way is to use inputstring task.
E.g.:
<exec executable= "run.bat" failonerror="true" inputstring="Y">
</exec>
However this proved to be unstable in some scenarios.

Running a .cmd file from Ant

Is it possible to run a command (.cmd file) from Ant? Would I need to write Java code for this?
<exec executable="cmd" os="Windows XP">
<arg value="/C"/>
<arg value="command to run"/>
</exec>
You can do this by using the exec task. From the ant exec documentation:
Note that .bat files cannot in general by executed directly. One normally needs to execute the command shell executable cmd using the /c switch.
So you would need to do something like:
<exec executable="cmd">
<arg value="/c"/>
<arg value="batchfile.cmd"/>
</exec>
Note that by doing this you have created a dependency of running your ant script in windows.
Adding to eradicus answer, you can also execute .bat, .cmd, ... from any directory with argument on your Window machine by
<target name="test">
<exec executable="cmd" dir="C:/Users/mydir/">
<arg value="/C" />
<arg value="myjob.bat arg1 arg2" />
</exec>
</target>

How can I execute a VBScript file from Ant script?

I want to run a VBScript file from an Ant script. How can I do that?
Have a look at the exec task. It allows you to execute a system command from your Ant script.
EDIT:
An example could be:
<target name="RunVbScript">
<exec executable="cscript">
<arg value="MyScript.vbs"/>
<arg value="argument 1" />
<arg value="argument 2" />
<arg value="argument n" />
</exec>
</target>

How can I get current PID from within Ant?

I have an ant task, and within it I'd like to get the current process id (a la echo $PPID from command line).
I'm running ksh on Solaris, so I thought I could just do this:
<property environment="env" />
<target name="targ">
<echo message="PID is ${env.PPID}" />
<echo message="PID is ${env.$$}" />
</target>
But that didn't work; the variables aren't substituted. Turns out PPID, SECONDS, and certain other env variables don't make it into Ant's representation.
Next I try this:
<target name="targ">
<exec executable="${env.pathtomyfiles}/getpid.sh" />
</target>
getpid.sh looks like this:
echo $$
This gets me the PID of the spawned shell script. Closer, but not really what I need.
I just want my current process ID, so I can make a temporary file with that value in the name. Any thoughts?
You can find PID using java process monitoring tool JPS, then output stream can be filtered and if needed process can be killed. check out this tomcat pid kill script:
<target name="tomcat.kill" depends="tomcat.shutdown">
<exec executable="jps">
<arg value="-l"/>
<redirector outputproperty="process.pid">
<outputfilterchain>
<linecontains>
<contains value="C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</linecontains>
<replacestring from=" C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
</outputfilterchain>
</redirector>
</exec>
<exec executable="taskkill" osfamily="winnt">
<arg value="/F"/>
<arg value="/PID"/>
<arg value="${process.pid}"/>
</exec>
<exec executable="kill" osfamily="unix">
<arg value="-9"/>
<arg value="${process.pid}"/>
</exec>
</target>
Why not just use the tempfile Ant task, instead? It does what you really want to do, while hiding all the gory details.
See http://ant.apache.org/manual/Tasks/tempfile.html.
your second method doesn't get ANT's pid. Change the shell script to (I use bash, I don't know if ksh is the same):
echo "$PPID"

Resources