I have an ant build file that executes a makefile
<target name="jni">
<exec executable="make">
<arg line="-f jni/Makefile"/>
</exec>
</target>
however if the make fails the other rules that depend on this rule will execute
how can I stop ant if the jni rule fails?
use the failonerror attribute, which is false by default:
<target name="jni">
<exec executable="make" failonerror="true">
<arg line="-f jni/Makefile"/>
</exec>
</target>
I never stop wondering why it is not true by default...
See the docs.
Related
So I have the below build file:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\Abc\Desktop\cmd.bat" >
<arg value="exit"/>
<arg value="mkdir C:\Users\Abc\Desktop\ant\test"/>
</exec>
</target>
I thought it would create a test folder there but its not. It pops out another command prompt window, I thought the exit would close that but it doesn't. I'm not sure what I am doing wrong.
The content of bath script test1.bat:
#echo off
echo %1
And ant's build.xml:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\AAA\Desktop\ant\test1.cmd">
<arg value="hello"/>
</exec>
</target>
As per the official 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
Follow this format instead:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
Source: https://ant.apache.org/manual/Tasks/exec.html
I have some code:
<exec executable="src/main/webapp/bin/webdriver.bat" failonerror="true" resultproperty="return.code">
<arg line="${ccc}/eeee--report-format JSON --report-file testResultserer/resultere-data/wwwww.json"/>
</exec> (1)
So, now I would like to exec command , when (1) are going fail.
How can I do this.
You can set property with the value of the return code and then execute the other command conditionally on the property's value:
<project>
<exec executable="${cmd}" resultproperty="ret1"/>
<condition property="cmd1failed" value="true">
<not>
<equals arg1="0" arg2="${ret1}"/>
</not>
</condition>
<exec executable="echo" xmlns:if="ant:if" if:true="${cmd1failed}">
<arg value="${cmd} failed"/>
</exec>
<exec executable="echo" xmlns:unless="ant:unless" unless:true="${cmd1failed}">
<arg value="${cmd} didn't fail"/>
</exec>
</project>
For example
$ ant -f exec.xml -Dcmd=/bin/true
Buildfile: /tmp/exec.xml
[exec] /bin/true didn't fail
BUILD SUCCESSFUL
Total time: 0 seconds
$ ant -f exec.xml -Dcmd=/bin/false
Buildfile: /tmp/exec.xml
[exec] Result: 1
[exec] /bin/false failed
BUILD SUCCESSFUL
Total time: 0 seconds
This uses the if/unless attributes introduced with Ant 1.9.1.
If you are using an older version of Ant, you'll have to use separate targets, something like
<project default="both">
<target name="cmd1">
<exec executable="${cmd}" resultproperty="ret1"/>
<condition property="cmd1failed" value="true">
<not>
<equals arg1="0" arg2="${ret1}"/>
</not>
</condition>
</target>
<target name="cmd1-fail" depends="cmd1" if="cmd1failed">
<exec executable="echo">
<arg value="${cmd} failed"/>
</exec>
</target>
<target name="cmd1-pass" depends="cmd1" unless="cmd1failed">
<exec executable="echo">
<arg value="${cmd} didn't fail"/>
</exec>
</target>
<target name="both" depends="cmd1-fail,cmd1-pass"/>
</project>
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
Is there a way to call Ant targets in 'Invoke Ant' build step dynamically? Say you have a build.xml with:
<target name="1" description="1">
<exec executable="${RES_DIR}/1.sh" failonerror="true">
</exec>
</target>
<target name="2" description="2">
<exec executable="${RES_DIR}/2.sh" failonerror="true">
</exec>
</target>
<target name="3" description="3">
<exec executable="${RES_DIR}/3.sh" failonerror="true">
</exec>
</target>
And want your Jenkins builds to all use this same build.xml, but to be able to call certain targets for each build. I don't want to hardcode these targets everytime a project needs to be created because I want it to be done automatically. Is this possible or do you have to create a project beforehand and manually set your Ant targets?
A simple trick can be to use the if attribute on targets.
For instance:
<target name="run" depends="1,2,3" />
<target name="1" description="1" if="run1">
<exec executable="${RES_DIR}/1.sh" failonerror="true">
</exec>
</target>
<target name="2" description="2" if="run2">
<exec executable="${RES_DIR}/2.sh" failonerror="true">
</exec>
</target>
<target name="3" description="3" if="run3">
<exec executable="${RES_DIR}/3.sh" failonerror="true">
</exec>
</target>
That way, you call the target run and set the property runX to an arbitrary value (true is generally a good choice). Only the target X will actually run.
That would probably work OK but it is a bit of an inconvenient to change your build just to satisfy a Jenkins build interface requirement.
The way I usually do it is to set a build parameter say a 'choice' and give the user the possibility to choose which target they want to invoke from that drop down list.
Say that choice parameter is called target then all you need to do in the 'invoke ant' build section is to refer to %target% and Jenkins will make the substitution for you.
Whether you go with a 'choice' or other types of build parameters (e.g. text) would depend on your specific needs.
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>