I am calling a batch file using an Ant exec task and setting the result in resultpropery. But the return value never comes to Ant. Below is my code
<property name="BuildErrorCode" value="abc"/>
<exec executable="cmd" resultproperty="BuildErrorCode" failonerror="false"
dir="C:\workspace\build\">
<arg value="/c"/>
<arg value="cmake_cross_compile.bat"/>
</exec>
<echo message="Error Code:=${BuildErrorCode}" />
I exit my batch script by:
if %errorlevel% neq 0 exit /b %errorlevel%
When the script runs, i always get abc as value instead of return value from batch file. My batch file returns 2 for now and I have to stop the build
I want to do the following:
If the return value is <> 0 then i have to make the build fail which is not happening now.
Any idea how I can get he return value and make the ant build fail?
If you run the build script in verbose mode (ant -v), you will notice the line
Override ignored for property "BuildErrorCode"
Essentially once an ant property has been set its value cannot be changed. This SO question has details.
A possible workaround is to not declare the property.
...
<!--property name="BuildErrorCode" value="abc"/-->
<exec executable = "cmd" resultproperty="BuildErrorCode" failonerror="false" dir="D:\work">
<arg value="/c"/>
<arg value="cmake_cross_compile.bat"/>
</exec>
...
The exec task resultproperty will capture the exit code of the cmd interpreter. The way you are calling exit in the batch file though is not terminating cmd, it is only exiting the script. The exit code from cmd will be unaffected, and stay zero. If you simply remove the \b option of the exit command you will terminate the interpreter as well and see the exit code you supply propagated.
if %errorlevel% neq 0 exit %errorlevel%
To fail, you could use a fail task, perhaps something like this:
<fail message="cmake_cross_compile.bat exited non-zero">
<condition>
<not>
<equals arg1="${BuildErrorCode}" arg2="0"/>
</not>
</condition>
</fail>
Or you could set failonerror="true" in the exec task to fail immediately.
Related
I am trying to execute an Ant script through jenkins but it fails when it attempts to use webpack...
The ant script's execution task is the following:
<exec executable="webpack.cmd" failonerror="true">
<env key="PROD_ENV" value="true"/>
</exec>
where webpack.cmd has been installed as a global npm module. The module directory has been included in the Path variable and I have even made sure that Jenkins is actually able to see it by doing a trial run inside a Execute windows batch command build step. However when it attempts to execute it via Ant it fails. Could it be a problem with the Ant plugin and its version?
The error i'm getting is the following:
Execute failed: java.io.IOException: Cannot run program "webpack.cmd": CreateProcess error=2, The system cannot find the file specified
webpack.cmd needs to run in a command shell. Try...
<exec executable="cmd" failonerror="true">
<env key="PROD_ENV" value="true"/>
<arg value="/c"/>
<arg value="webpack.cmd"/>
</exec>
You may need to provide the full absolute path to webpack.cmd so cmd.exe can find it.
These are two ant tasks:
<target name="hg.add" >
<exec dir="." executable="hg">
<arg line="add ${reports-summary}" />
</exec>
</target>
<target name="hg.add">
<exec executable="/bin/sh">
<arg value="-c"/>
<arg value="hg add ${reports-summary}"/>
</exec>
</target>
This two tasks seem to have the same function. But why the second one needs to write"/bin/sh" and "-c"?
The -c is just an argument to the executable it does not mean anything special to the ant exec task.
The first target is running the hg executable directly.
The second target is running the Linux/Unix shell command (/bin/sh) and passing it the -c argument which tells the shell to execute a command given in the next argument. So this will run the same command as the first target, but the shell command may set up things like environment variables before running the command.
I have an exiting ant build that is called via ant -lib lib -f test_build.xml
I wanted to add one more <target> section to be run after the existing one finishes. I did some research and found ant manual for exec and even a question here on SO. After some reading I added new target to this existing build but it didn't work.
I tried to create new build file only with my target. It doesn't work either. Although the ant run finishes with message BUILD SUCCESSFUL
Total time: 0 seconds
If I run my ruby script from a command line it works. I tried to create bat file that would call my ruby script with the same result. If I call the bat file from dos window it works.
My ant run build file looks like
<project name="RunRubyExample">
<target name="calling ruby " >
<exec executable="ruby.exe">
<arg value="C:\EduTester\others\afterant.rb 1 2 tri four"/>
</exec>
</target>
<target name="calling batach">
<exec executable="cmd">
<arg value="/c"/>
<arg value="C:\EduTester\others\rubruby.bat 1 2 tri four"/>
</exec>
</target>
</project>
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
on Windows XP
It looks like you're passing a single arg value with embedded spaces
<arg value="C:\EduTester\others\afterant.rb 1 2 tri four"/>
Is that right? Or should it be either an arg line:
<arg line="C:\EduTester\others\afterant.rb 1 2 tri four"/>
or multiple arg values:
<arg value="C:\EduTester\others\afterant.rb"/>
<arg value="1"/>
<arg value="2"/>
<arg value="tri"/>
<arg value="four"/>
There needs to be specified target that needs to be run
either by <project name="RunRubyExample" default="callingruby">
or when calling the ant build file. Where we pass the target name we want to be run as an argument. ant -lib lib callingruby
Writing a Simple Buildfile
Thank you #Steve
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}" />
I'm attempting to execute a program, and if that fails I have a fallback method to get the information required. Not everyone who uses this build script will have the program installed. My task has the following:
<exec executable="runMe"
failonerror="false"
failifexecutionfails="false"
outputproperty="my.answer"
errorproperty="my.error"
error="myerror.txt" />
Apparently I misunderstand the Ant manual because I thought by setting error or errorproperty the error would be redirected and not shown on the screen.
Is it possible to hide the message "Execute failed: java.io.IOException: Cannot run program "runMe"..."?
Alternately, is there a way to determine if that program can be run without checking for its existence? If the program is on the user's system it won't be in the same place from user to user.
Thank you,
Paul
Try ant-contrib's try/catch/finally commands.
http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html
The goal for doing this was to get the Subversion revision for my project in Ant. Some developers have command line Subversion installed and others don't so I needed a way to test for the presence of svnversion.
In the end I used a batch file to test the command:
REM File checkCommand.bat
#echo off
%1 >NUL 2 >NUL
if errorlevel 1 goto fail
REM command succeeded
exit 0
:fail
REM command failed
exit 1
In my Ant target I run this like so:
<target name="checkForSvnversion">
<local name="cmdresult" />
<exec dir="." executable="com"
resultproperty="cmdresult"
failonerror="false"
failifexecutionfails="false">
<arg line="/c checkCommand.bat svnversion" />
</exec>
<condition property="exec.failed">
<equals arg1="${cmdresult}" arg2="1" trim="true" />
</condition>
</target>
I have two targets that depend on this result:
<target name="getRevisionFromSvnversion" depends="checkForSvnversion"
unless="exec.failed">
etc etc
</target>
and
<target name="getRevisionFromEntries" depends="checkForSvnversion"
if="exec.failed">
etc etc
</target>
Finally, the task I call to get the revision is:
<target name="getRevision"
depends="getRevisionFromSvnversion,getRevisionFromEntries">
<echo>My rev is ${svn.revision}</echo>
</target>