ant exec task when command is fail - ant

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>

Related

Ant exec, How to arg

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

build.xml ignoring <echo> tags and if options

I have a sencha Ext.js project and I am trying to get user input and take action based on that user input in my build.xml file.
Here's my build.xml:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" default=".help">
<import file="${basedir}/.sencha/app/build-impl.xml"/>
<target name="-after-build">
<echo>57 ...</echo>
<property name="realperl" value="/usr/bin/perl"/>
<echo>59 ...</echo>
<exec executable="/bin/hostname" outputproperty="myhost">
<arg value="-s"/>
</exec>
<echo>64 ...</echo>
<exec executable="${realperl}" outputproperty="env">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="ENV"/>
</exec>
<echo>70 ...</echo>
<input message="Install ${env} version on ${myhost}?" addproperty="install" validargs="y,n" />
<echo>72 ...</echo>
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>Hello ...</echo>
<exec executable="${realperl}" outputproperty="htdocspath">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="HTDOCSPATH"/>
</exec>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync to ${htdocspath}. Output logged to /tmp/rsync.log.</echo>
<echo if:false="${doinstall}">Skipping rsync to ${htdocspath}.</echo>
<exec executable="/usr/local/bin/rsync" if:true="${doinstall}">
<arg value="--exclude='*.log'" />
<arg value="--chmod=ugo=rwX" />
<arg value="-rltDq" />
<arg value="../info"/>
<arg value="../sbin"/>
<arg value="../bin"/>
<arg value="../cec"/>
<arg value="../cec.list.html"/>
<arg value="../cgi"/>
<arg value="../common"/>
<arg value="../dblib"/>
<arg value="../etc"/>
<arg value="../ext-2.3.0"/>
<arg value="../framework"/>
<arg value="../help"/>
<arg value="../icon-loading-animated.gif"/>
<arg value="../images"/>
<arg value="../img"/>
<arg value="../include"/>
<arg value="../index.shtml"/>
<arg value="../kicker"/>
<arg value="../lib"/>
<arg value="../msgs"/>
<arg value="../src"/>
<arg value="../thingold"/>
<arg value="../thing"/>
<arg value="${htdocspath}"/>
<redirector output="/tmp/rsync.log" alwayslog="true"/>
</exec>
</target>
</project>
When I run sencha app build which invokes ant I only get this output from by build.xml:
Install Dev version on tools-dev1? (y, n)
There is no output from my <echo... tags and if I answer 'n' I can that the rsync command executes. What am I doing wrong here?
I simplified your build.xml
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" >
<input message="Install ?" addproperty="install" validargs="y,n" />
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync</echo>
<echo unless:true="${doinstall}">Skipping rsync </echo>
<exec executable="echo" if:true="${doinstall}">
<arg value="ECHO IS EXECUTED" />
<redirector output="./rsync.log" alwayslog="true"/>
</exec>
</project>
and I replaced if:false with unless:true
<echo unless:true="${doinstall}">Skipping rsync </echo>
Everything works fine:
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
y
[echo] do install is true
[echo] Please wait. Executing rsync
[exec] ECHO IS EXECUTED
BUILD SUCCESSFUL
Total time: 2 seconds
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
n
[echo] do install is ${doinstall}
[echo] Skipping rsync
BUILD SUCCESSFUL
Total time: 2 seconds

execute Ant task if TWO conditions are met

The above ant script implements if dir_is_empty then git-clone else git-fetch using Ant-1.7.1 core statements:
<target name="update" depends="git.clone, git.fetch" />
<target name="check.dir">
<fileset dir="${dir}" id="fileset"/>
<pathconvert refid="fileset" property="dir.contains-files" setonempty="false"/>
</target>
<target name="git.clone" depends="check.dir" unless="dir.contains-files">
<exec executable="git">
<arg value="clone"/>
<arg value="${repo}"/>
<arg value="${dir}"/>
</exec>
</target>
<target name="git.fetch" depends="check.dir" if="dir.contains-files" >
<exec executable="git" dir="${dir}">
<arg value="fetch"/>
</exec>
</target>
(see my other post)
But how to implement a target enabled by two conditions?
if dir_does_not_exist or dir_is_empty then git-clone else git-fetch
my current attempt:
<target name="git.clone"
depends="chk.exist, chk.empty"
unless="!dir.exist || dir.noempty" >
[...]
</target>
<target name="chk.exist">
<condition property="dir.exist">
<available file="${dir}/.git" type="dir"/>
</condition>
</target>
[...]
I would prefer Ant-1.7.1 core statements. But I am open about other possibilities as Ant contrib, or embedded script... Feel free to post your ideas...
(See also question Execute ANT task just if a condition is met)
Even when bound to Ant 1.7.1 you may combine your 3 chk targets into one, see the condition part in the snippet.
Since Ant 1.9.1 (better use Ant 1.9.3 because of bugs in Ant 1.9.1 see this answer for details) it is possible to add if and unless attributes on all tasks and nested elements, so no extra target needed, f.e. :
<project xmlns:if="ant:if" xmlns:unless="ant:unless">
<condition property="cloned" else="false">
<and>
<available file="${dir}/.git" type="dir" />
<resourcecount when="gt" count="0">
<fileset dir="${dir}/.git" />
</resourcecount>
</and>
</condition>
<exec executable="git" unless:true="${cloned}">
<arg value="clone" />
<arg value="${repo}" />
<arg value="${dir}" />
</exec>
<exec executable="git" dir="${dir}" if:true="${cloned}">
<arg value="fetch" />
</exec>
</project>
From the ant documentation on targets:
Only one propertyname can be specified in the if/unless clause.
If you want to check multiple conditions, you can use a dependend target for computing the result for the check:
<target name="myTarget" depends="myTarget.check" if="myTarget.run">
<echo>Files foo.txt and bar.txt are present.</echo>
</target>
<target name="myTarget.check">
<condition property="myTarget.run">
<and>
<available file="foo.txt"/>
<available file="bar.txt"/>
</and>
</condition>
</target>
Moreover, there were some discussions on dev#ant.apache.org and user#ant.apache.org mailing-lists:
Using multiple properties in the 'if' and 'unless' conditions (June 2006)
Support mutliple if and unless (August 2008)
Multiple conditions satisfying in an ant target (October 2008)
For example, the following target combines two properties (dir.exist and dir.noempty) to create another one (cloned) using operators <and> and <istrue> (many other operators are documented as <or>, <xor>, <not>, <isfalse>, <equals>, <length>).
<target name="chk" depends="chk.exist, chk.empty" >
<condition property="cloned">
<and>
<istrue value="dir.exist" />
<istrue value="dir.noempty" />
</and>
</condition>
</target>
The above property "cloned" is used by targets git.clone and git.fetch as follows:
<target name="update" depends="git.clone, git.fetch" />
<target name="git.clone" depends="chk" unless="cloned" >
<exec executable="git" >
<arg value="clone" />
<arg value="${repo}" />
<arg value="${dir}" />
</exec>
</target>
<target name="git.fetch" depends="chk" if="cloned" >
<exec executable="git" dir="${dir}">
<arg value="fetch"/>
</exec>
</target>
<target name="chk.exist" >
<condition property="dir.exist" >
<available file="${dir}" type="dir" />
</condition>
</target>
<target name="chk.empty" >
<fileset dir="${dir}" id="fileset" />
<pathconvert refid="fileset" property="dir.noempty" setonempty="false" />
</target>

Ant build.xml phpunit

I have a ant build.xml (below). I am able to run phpunit fine from the command line as follows:
D:> phpunit --verbose --testdox-html logs\today.html runtest
This runs all my phpunit tests within the folder d:\runtest.
My problem is when I run my build.xml as 'ant build' it tries to execute a file called runtest.php the output from ant is below:
D:\>ant build
Buildfile: D:\build.xml
check_os:
if_windows:
if_unix:
prepare:
phpunit:
[exec] PHPUnit 3.6.11 by Sebastian Bergmann.
[exec]
[exec] Cannot open file "runtest.php".
BUILD FAILED
D:\build.xml:48: exec returned: 1
Total time: 2 seconds
My Build.xml is as follows:
<!-- This project launches the test generator and execute all phpunit selenium tests -->
<project name="proj" default="build" basedir="">
<!--Get environment variables -->
<property environment="env" />
<property name="logFolder" value="${basedir}\logs"/>
<property name="testFolder" value="${basedir}\runtest"/>
<property name="test" value="**" />
<condition property="pattern" value="runtest/*.php">
<os family="windows" />
</condition>
<tstamp/>
<!-- Check Operating system to set phpunit path-->
<target name="check_os">
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isLinux">
<os family="unix" />
</condition>
</target>
<target name="if_windows" depends="check_os" if="isWindows">
<property name="exe.phpunit" value="C:\\Program Files\\PHP\\phpunit.bat"/>
</target>
<target name="if_unix" depends="check_os" if="isLinux">
<property name="exe.phpunit" value="${env.PHP_HOME}/includes/PHPUnit-3.2.0/PHPUnit" />
</target>
<target name="prepare" depends="if_windows, if_unix">
<mkdir dir="${logFolder}"/>
</target>
<target name="phpunit">
<!-- Check if folder empty -->
<fileset id="fileset.test" dir="${testFolder}">
<include name="*.*"/>
</fileset>
<fail message="Files not found">
<condition>
<resourcecount refid="fileset.test" when="less" count="1"></resourcecount>
</condition>
</fail>
<!-- Execute phpunit tests -->
<exec executable="${exe.phpunit}" failonerror="true" dir="runtest">
<arg line="--verbose --testdox-html '${logFolder}\phpunit-report-${TODAY}.html' runtest" />
</exec>
</target>
<target name="build" depends="prepare,phpunit"/>
</project>
The problem was I specified dir="runtest" once I removed this from the Execute line it worked.
<target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" failonerror="true" dir="${basedir}/classes/tests/" resultproperty="result.phpunit">
${phpunit} - needs Path of phpunit installation, you can get it from where is phpunit command in linux
dir="${basedir}/classes/tests/" - Need only the folder path where your php application is present
<arg line="UserTest ${basedir}/classes/tests/userTest.php" />
line="UserTest ${basedir}/classes/tests/userTest.php" - Here UserTest is class name and ${basedir}/classes/tests/userTest.php it is path of test class file
<arg value="--configuration"/>
<arg path="${basedir}/classes/tests/UnitTest.xml"/>
path="${basedir}/classes/tests/UnitTest.xml" - Path of xml file
</exec>
<property name="phpunit.done" value="true"/>
</target>

Any analog of rsync for Ant?

I need some analog of rsync for ant. The issue is to copy files from source directory to the set of subdirectories that was previously accomplished with script
rsync -r --ignore-existing $BASE_DIR/common/config/* $BASE_DIR/config
Thanks for any help
You can use exec to call rsync from Ant, you could use the java task to call Jarsync or java-sync or you could create a custom ant task to call either of those libraries.
Zombie question but posting https://gist.github.com/garethr/878364 in case I ever search for it myself again. Pasting Gist content in case something something.
<project name="{{ name }}" default="help" basedir=".">
<property name="username" value="{{ username }}"/>
<property name="host" value="{{ host }}"/>
<property name="dir" value="/srv/{{ path }}/"/>
<tstamp>
<format property="TODAY_UK" pattern="yyyyMMddhhmmss" locale="en,UK"/>
</tstamp>
<target name="help" description="show available commands" >
<exec executable="ant" dir="." failonerror="true">
<arg value="-p"/>
</exec>
</target>
<target name="deploy-to" description="show where we are deploying to" >
<echo>${username}#${host}:${dir}</echo>
</target>
<target name="deploy" description="deploy usng rsync" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="-v"/>
</exec>
</target>
<target name="deploy-test" description="test deploy usng rsync with the dry run flag set" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="--dry-run"/>
<arg value="-v"/>
</exec>
</target>
<target name="backup" description="backup site" >
<exec executable="scp" dir="." failonerror="true">
<arg value="-r"/>
<arg value="${username}#${host}:${dir}"/>
<arg value="backups/${TODAY_UK}"/>
</exec>
</target>
</project>

Resources