Ant - Execute specific target from one target and not from other - ant

I can run my build file from two targets as below
<target name="makeMinificationTarget" depends="inheritedTarget" />
<target name="skipMinificationTarget" depends="inheritedTarget" />
However, these both targets internally calls "inheritedTarget" and this "inheritedTarget" internally calls "makeMinification" target.
Following is "inheritedTarget" and "makeMinification"
<target name="makeMinification">
<echo message="makingMinification...">
</target>
<target name="inheritedTarget" depends="makeMinification"/>
<echo message="compileFiles">
<echo message="ExecuteFiles">
</target>
My question is i want to skip execution of "makeMinification" target from
this call <target name="skipMinificationTarget" depends="inheritedTarget" />
And
let it happen if we call <target name="makeMinificationTarget" depends="inheritedTarget" />
How can I achieve this ???

Does this work? Using "antcall" and passing the parameter. Instead of using "depends"
<target name="makeMinificationTarget" >
<antcall target="inheritedTarget">
<param name="Minification" value="true"/>
</antcall>
</target>
<target name="skipMinificationTarget" >
<antcall target="inheritedTarget">
<param name="Minification" value="false"/>
</antcall>
</target>
<target name="inheritedTarget" >
<if>
<equals arg1="${Minification}" arg2="true" />
<then>
<antcall target="makeMinification"/>
</then>
<else>
<!-- Do something else-->
</else>
</if>
<!-- Do other stuff -->
</target>
<target name="makeMinification" >
<!-- Do something -->
</target>

Related

How to read properties file in ant

Hi all these are the contents of my file.properties file and i want to print those platforms which have true valu and their installer location,it is sure that the platform which has true value will only have installer:
platform.win-x86=true
platform.win-x86-client=false
platform.win-x64=true
platform.linux-x86=false
installer-zip.win-x86=E:\\abc\\abc.jar
installer-zip.win-x64=E:\\def\\def.jar
tried many thing but could not one of the things i used was as
<propertyselector property="platform.list"
delim`enter code here`iter=","
match="platform\.([^\.]*)"
select="\1"
casesensitive="false" />
<propertyselector property="zip.list"
delimiter=","
match="installer-zip\.([^\.]*)"
select="\1"
casesensitive="false" />
<target name="print.name" >
<propertycopy name="platform.name" from="platform.${platform.id}" />
<if> <equals arg1="${platform.name}" arg2="true" />
<then>
<echo>PlatForm.Id====>${platform.id}</echo>
<echo message="${platform.name}" />
<echo file="platform.properties" append="yes">${new.platform-name}=Yes${line.separator}</echo>
</then>
</if>
</target>
<target name="print.zipname" >
<propertycopy name="zip.name" from="installer-zip.${zip.id}" />
<echo>zip.Id====>${zip.id}</echo>
<echo message="${zip.name}" />
</target>
<target name="first">
<foreach list="${platform.list}"
delimiter=","
target="print.name"
param="platform.id" />
<foreach list="${zip.list}"
delimiter=","
target="print.zipname"
param="zip.id" />
</target>
although it prints value for all true platforms but for installer it prints only 1,and then breaks..can you help please
This is not directly answering your question, but rather showing you how you could use the same properties to perform conditional targets in your build.
<project default="build">
<property file="platform.properties"/>
<condition property="build.win-x86">
<and>
<istrue value="${platform.win-x86}"/>
<isset property="installer-zip.win-x86"/>
</and>
</condition>
<condition property="build.win-x86-client">
<and>
<istrue value="${platform.win-x86-client}"/>
<isset property="installer-zip.win-x86-client"/>
</and>
</condition>
<condition property="build.win-x64">
<and>
<istrue value="${platform.win-x64}"/>
<isset property="installer-zip.win-x64"/>
</and>
</condition>
<condition property="build.linux-x86">
<and>
<istrue value="${platform.linux-x86}"/>
<isset property="installer-zip.linux-x86"/>
</and>
</condition>
<target name="build" depends="win-x86, win-x86-client, win-x64, linux-x86"/>
<target name="win-x86" depends="win-x86-build, win-x86-installer"/>
<target name="win-x86-client" depends="win-x86-client-build, win-x86-client-installer"/>
<target name="win-x64" depends="win-x64-build, win-x64-installer"/>
<target name="linux-x86" depends="linux-x86-build, linux-x86-installer"/>
<target name="win-x86-build" if="${build.win-x86}">
<echo message="executing win-x86-build"/>
</target>
<target name="win-x86-installer" if="${build.win-x86}">
<echo message="installer: ${installer-zip.win-x86}"/>
</target>
<target name="win-x86-client-build" if="${build.win-x86-client}">
<echo message="executing win-x86-client-build"/>
</target>
<target name="win-x86-client-installer" if="${build.win-x86-client}">
<echo message="installer: ${installer-zip.win-x86-client}"/>
</target>
<target name="win-x64-build" if="${build.win-x64}">
<echo message="executing win-x64-build"/>
</target>
<target name="win-x64-installer" if="${build.win-x64}">
<echo message="installer: ${installer-zip.win-x64}"/>
</target>
<target name="linux-x86-build" if="${build.linux-x86}">
<echo message="executing linux-x86-build"/>
</target>
<target name="linux-x86-installer" if="${build.linux-x86}">
<echo message="installer: ${installer-zip.linux-x86}"/>
</target>
</project>
The output based on your properties file is below. Note that only the win-x86 and win-64 targets were actually executed. The others were skipped because the required conditions were not satisfied:
win-x86-build:
[echo] executing win-x86-build
win-x86-installer:
[echo] installer: E:\abc\abc.jar
win-x86:
win-x86-client-build:
win-x86-client-installer:
win-x86-client:
win-x64-build:
[echo] executing win-x64-build
win-x64-installer:
[echo] installer: E:\def\def.jar
win-x64:
linux-x86-build:
linux-x86-installer:
linux-x86:
build:
BUILD SUCCESSFUL
Total time: 0 seconds

Selenium Grid with Ant sequential + parallel execution

I am trying to run the selenium tests with selenium grid 2 set up. My tests are more dependent . I have to well-define my sequential run & parallel run. Attaching my build.xml file for ref. Inside sequential node I have many parallel nodes with different targets. I am facing inconsistency issues in running this build.xml.
Some times it picks up the targets of 2nd parallel node & sometimes its not. It is not giving error also. I tried running ant command in verbose mode, still not getting ant exceptions.
Will be glad if someone helps in this regard.
<target name="startServerRC" depends="startServerhub">
<echo>Starting Selenium Server...</echo>
<java jar="${lib.dir}/selenium-server-standalone.jar" fork="true" spawn="true">
<arg line="-port 5555"/>
<arg line="-log log.txt"/>
<arg line="-firefoxProfileTemplate"/>
<arg value="${lib.dir}/ff_profile"/>
<arg line="-userExtensions"/>
<arg value="${lib.dir}/user-extensions.js"/>
<arg line="-role node"/>
<arg line="-hub http://localhost:4444/grid/register "/>
<arg line="-maxSession 10"/>
<arg line="-maxInstances=10"/>
</java>
</target>
<!-- Initialization -->
<target name="init" depends="startServerRC" >
<echo>Initlizing...</echo>
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}"/>
</target>
<!-- Complies the java files -->
<target name="compile" depends="init">
<echo>Compiling...</echo>
<javac
debug="true"
srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="classpath" />
</target>
<target name="CItarget">
<sequential>
<antcall target="compile"/>
<parallel>
<antcall target="run"/>
<antcall target="run_PSDATA"/>
</parallel>
<parallel>
<antcall target="run_PreData"/>
<antcall target="run_DFPPulls"/>
<antcall target="run_AdTechPulls"/>
<antcall target="run_AppnexusPulls"/>
<antcall target="run_FTPPulls"/>
<antcall target="run_OASPulls"/>
<antcall target="run_GDFPPulls"/>
<antcall target="run_FreewheelPulls"/>
<antcall target="run_ThirdPartyPulls"/>
</parallel>
<parallel>
<antcall target="run_PostData"/>
<antcall target="run_Sales"/>
</parallel>
<parallel>
<antcall target="run_Administration"/>
<antcall target="run_E2EPartner360"/>
<antcall target="run_Sales"/>
<antcall target="run_Finance"/>
<antcall target="run_Loaders"/>
<antcall target="run_Accounts"/>
<antcall target="run_Adops"/>
</parallel>
<parallel>
<antcall target="run_Alerts"/>
<antcall target="run_CustomFields"/>
</parallel>
<antcall target="stop-selenium"/>
</sequential>
</target>
Thanks in advance
Anjana
Try QAF (formerly ISFW) where you can run test parallel with/without use of grid.
In Your case following config file can fulfill your requirement:
<suite name="Sample Test Automation" verbose="0" parallel="tests">
<test name="Set1" >
<parameter name="selenium.server" value="server1"/>
<parameter name="selenium.port" value="port"/>
<!-- group or class or package entry as per testNG standard
-->
<classes>
<class name="qualified name of class"></class>
</classes>>
</test>
<test name="set2" >
<parameter name="selenium.server" value="server2"/>
<parameter name="selenium.port" value="port"/>
<!-- <packages>
<package name="package name" />
</packages>
-->
<classes>
<class name="qualified name of class"></class>
</classes>
</test>
</suite>
It's much better to use tesng or junit to manager parallel testing instead of doing it directly in Build.xml.
here is a good tutorial on configuring testng, Ant and selenium grid :
http://technologyandleadership.com/six-steps-for-complete-test-automation-with-selenium-grid/

Ant - continue the target execution ,even if one target completes for selenium automation

Is there a way to cause Ant to not quit even if one of a target completes?
For instance, several targets may execute, and if the first one stops,selenium will freeze.All the other testcases which are running parallel in other target stops.
How to make ant to continue executing other targets,even if one completes.
I tried giving -k at target level , but no use. We have failonerror set to true .Does that matter?
Here is my build file :
<target name="startServerRC" depends="startServerhub">
<echo>Starting Selenium Server...</echo>
<java jar="${lib.dir}/selenium-server-standalone.jar" fork="true" spawn="true">
<arg line="-port 5555"/>
<arg line="-log log.txt"/>
<arg line="-firefoxProfileTemplate"/>
<arg value="${lib.dir}/ff_profile"/>
<arg line="-userExtensions"/>
<arg value="${lib.dir}/user-extensions.js"/>
<arg line="-role node"/>
<arg line="-hub http://localhost:4444/grid/register "/>
<arg line="-maxSession 10"/>
<arg line="-maxInstances=10"/>
</java>
</target>
<!-- Initialization -->
<target name="init" depends="startServerRC" >
<echo>Initlizing...</echo>
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}"/>
</target>
<!-- Complies the java files -->
<target name="compile" depends="init">
<echo>Compiling...</echo>
<javac
debug="true"
srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="classpath" />
</target>
<target name="CItarget">
<sequential>
<antcall target="compile"/>
<parallel>
<antcall target="run"/>
<antcall target="run_PSDATA"/>
</parallel>
<parallel>
<antcall target="run_PreData"/>
<antcall target="run_DFPPulls"/>
<antcall target="run_AdTechPulls"/>
<antcall target="run_AppnexusPulls"/>
<antcall target="run_FTPPulls"/>
<antcall target="run_OASPulls"/>
<antcall target="run_GDFPPulls"/>
<antcall target="run_FreewheelPulls"/>
<antcall target="run_ThirdPartyPulls"/>
</parallel>
<parallel>
<antcall target="run_PostData"/>
<antcall target="run_Sales"/>
</parallel>
<parallel>
<antcall target="run_Administration"/>
<antcall target="run_E2EPartner360"/>
<antcall target="run_Sales"/>
<antcall target="run_Finance"/>
<antcall target="run_Loaders"/>
<antcall target="run_Accounts"/>
<antcall target="run_Adops"/>
</parallel>
<parallel>
<antcall target="run_Alerts"/>
<antcall target="run_CustomFields"/>
</parallel>
<antcall target="stop-selenium"/>
</sequential>
</target>
Thanks in advance
You could try using try-catch from ant-contrib.
Example from the link:
<trycatch property="foo" reference="bar">
<try>
<fail>Tada!</fail>
</try>
<catch>
<echo>In <catch>.</echo>
</catch>
<finally>
<echo>In <finally>.</echo>
</finally>
</trycatch>
If someting fails, you would just echo something (or do noting). The finally part works great if you need to make sure, that servers are shut down at the end, even if something fails hard in between.
Also setting failonerror="false" should make ant not to fail the build on errors.

Is there is a way to check Ant Target condition before target dependencies get executed

I am trying to avoid antcall in my program, so trying to move antcall targets to target dependencies. Now I have a situation:
<target name="test" depends="a,b,c" />
<target name="a" depends="a1,a2,a3" />
<target name="b" depends="b1,b2,b3" />
<target name="c" depends="c1,c2,c3" />
Till now every thing work fine. But if i just want to skip target 'c' and its dependencies from execution,
<target name="c" depends="c1,c2,c3" if="skip.c" /> (considering the property "skip.c" is already set)
Now the dependencies of target 'c' will be executed and then it checks for condition "skip.c".
Is there a better solution, where both target and its dependencies wont execute based on condition.
I can always go for antcall with if conditions. But looking for any another alternative.
I cant check for "skip.c" conditions in c1,c2,c3 targets as i have more conditions to check in those targets.
All dependencies of a target are processed before the "if/unless" test is looked at. There is no built-in method in Ant to avoid this.
Instead, a good approach is to separate the dependencies from the actions. Try the following:
<target name="test" depends="a,b,c" />
<target name="a" depends="a1,a2,a3" />
<target name="b" depends="b1,b2,b3" />
<target name="c">
<condition property="skip.c.and.dependents">
<or>
<isset property="prop1"/>
<isset property="prop2"/>
</or>
</condition>
<antcall target="do-c-conditionally"/>
</target>
<target name="do-c-conditionally" unless="skip.c.and.dependents">
<antcall target="do-c"/>
</target>
<target name="do-c" depends="c1,c2,c3">
<!-- former contents of target c -->
</target>
To avoid using the antcall, you would need to put the condition in each of the sub-tasks.
Looking at the name "skip.c", it is probably an "unless" condition, like this:
<target name="test" depends="a,b,c" />
<target name="a" depends="a1,a2,a3" />
<target name="b" depends="b1,b2,b3" />
<target name="c" depends="c1,c2,c3" />
<target name="c1" unless="skip.c">
<!-- contents of target c1 -->
</target>
<target name="c2" unless="skip.c">
<!-- contents of target c2 -->
</target>
<target name="c3" unless="skip.c">
<!-- contents of target c3 -->
</target>
If you need to do the processing of the conditions at the moment you run task "c", you could do it in a target "check_run_c" like this:
<target name="test" depends="a,b,c" />
<target name="a" depends="a1,a2,a3" />
<target name="b" depends="b1,b2,b3" />
<target name="c" depends="check_run_c,c1,c2,c3" />
<target name="check_run_c">
<condition property="run.c">
<!-- set this property "run.c" if the "c*" targets should run... -->
<or>
<isset property="prop1"/>
<isset property="prop2"/>
</or>
</condition>
</target>
<target name="c1" if="run.c">
<!-- contents of target c1 -->
</target>
<target name="c2" if="run.c">
<!-- contents of target c2 -->
</target>
<target name="c3" if="run.c">
<!-- contents of target c3 -->
</target>
If there are also instructions in the task "c" that you only want to run conditionally:
<target name="test" depends="a,b,c" />
<target name="a" depends="a1,a2,a3" />
<target name="b" depends="b1,b2,b3" />
<target name="c" depends="check_run_c,c1,c2,c3" if="run.c" >
<!-- contents of target c -->
</target>
<target name="check_run_c">
<condition property="run.c">
<!-- set this property "run.c" if the "c*" targets should run... -->
<or>
<isset property="prop1"/>
<isset property="prop2"/>
</or>
</condition>
</target>
<target name="c1" if="run.c">
<!-- contents of target c1 -->
</target>
<target name="c2" if="run.c">
<!-- contents of target c2 -->
</target>
<target name="c3" if="run.c">
<!-- contents of target c3 -->
</target>

Multiple renaming using ant script

Let me explain the scenario:
D:\project\src\one.txt
D:\project\src\two.txt
D:\project\src\three.txt
D:\project\src\four.txt
The above files should be copied as :
D:\project\dst\one.xls
D:\project\dst\two.xls
D:\project\dst\three.xls
D:\project\dst\four.xls
I need to change the extension without using the mapper and move task. I need to rename as above using a for loop with fte:filecopy function inside. Is this possible ???
For anyone arriving here without the negative requirement afflicting the OP, the much simpler answer is to use a mapper.
<project default="move_files">
<target name="move_files">
<copy todir="dst">
<fileset dir="src">
<include name="*.txt"/>
</fileset>
<globmapper from="*.txt" to="*.xls"/>
</copy>
</target>
</project>
This works for me :
<?xml version="1.0"?>
<project name="so-copy-rename" default="build2">
<property name="ant-contrib-jar" value="${user.home}/.ant/lib/ant-contrib-1.0b3.jar"/>
<target name="setup" unless="ant-contrib.present">
<echo>Getting ant-contrib</echo>
<mkdir dir="${user.home}/.ant/lib"/>
<!--
Note: change this to a locally hosted maven repository manager such as nexus http://nexus.sonatype.org/
-->
<get dest="${ant-contrib-jar}"
src="http://repo1.maven.org/maven2/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
</target>
<target name="taskdefs">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${ant-contrib-jar}"/>
</classpath>
</taskdef>
</target>
<target name="build" depends="taskdefs">
<property name="srcdir" value="src"/>
<property name="targetdir" value="target"/>
<property name="files" value="file1,file2,file3,file4"/>
<mkdir dir="${targetdir}"/>
<foreach list="${files}" target="copy-rename" param="srcfile" trim="true">
<param name="srcdir" value="${srcdir}" />
<param name="targetdir" value="${targetdir}" />
</foreach>
</target>
<target name="copy-rename">
<var name="src-suffix" value="txt"/>
<var name="tgt-suffix" value="xls"/>
<copy file="${srcdir}/${srcfile}.${src-suffix}" tofile="${targetdir}/${srcfile}.${tgt-suffix}" />
</target>
<target name="build2" depends="taskdefs">
<property name="srcdir" value="src"/>
<property name="targetdir" value="target"/>
<mkdir dir="${targetdir}"/>
<foreach target="copy-rename2" param="srcfile">
<path id="srcfilepath">
<fileset dir="${srcdir}" casesensitive="yes">
<include name="*.txt"/>
</fileset>
</path>
<param name="targetdir" value="${targetdir}" />
</foreach>
</target>
<target name="copy-rename2">
<var name="basefile" value="" unset="true"/>
<basename property="basefile" file="${srcfile}" suffix=".txt"/>
<var name="tgt-suffix" value="xls"/>
<copy file="${srcfile}" tofile="${targetdir}/${basefile}.${tgt-suffix}" />
</target>
</project>
Can you slice it the other way and perform the renaming inside the fte:filecopy command? Looking at the IBM documentation, you can specify tasks to be carried out at the source or destination agents either before or after the copy, using presrc, postdst etc. This task could be an Ant task that does the renaming?

Resources