Optional Ant arg - ant

I would like to have an ant arg value optionally included without having to make 2 targets which would be basically the same except for the extra arg. For example:
<target name="A" depends="C">...</target>
<target name="B" depends="C">...</target>
<target name="C">
<java fork="true" ...>
<jvmarg .../>
<arg .../>
<arg .../>
...
# now, if the dependency is from A, no more args
# if from B
<arg value="xxx"/>
</java>
</target>

Rather than depending on task C, you could use the Antcall task to pass the B argument as a param.
<target name="A" >
<antcall target="C" />
....
</target>
<target name="B" >
<antcall target="C" >
<param name="extra_arg" value="xxx" />
</antcall>
...
</target>
<target name="C">
<java fork="true" ...>
<jvmarg .../>
<arg .../>
<arg .../>
<arg value="${extra_arg}"/>
</java>
</target>
EDIT: As Nico points out in the comment, this doesn't work if the value is unset from A. The answer can be extended to use the condition task to set the argument to a null string.
<condition property="argToUseIfFromB" else="">
<isset property="extra_arg" />
</condition>
<java fork="true" ...>
<jvmarg .../>
<arg .../>
<arg .../>
<arg value="${argToUseIfFromB}"/>
</java>
FURTHER EDIT:
Since we can't get the arguments to be recognised as optional, we can pass in the whole command line from each parent task. Target A would only pass the common arguments; B would pass through an extra argument. The Ant manual on arguments explains it better than me.
<target name="A" >
<antcall target="C">
<param name="java_args" value="arg_a arg_b" />
</antcall>
....
</target>
<target name="B" >
<antcall target="C" >
<param name="java_args" value="arg_a arg_b extra_arg" />
</antcall>
...
</target>
<target name="C">
<java fork="true" ...>
<jvmarg .../>
<arg line="${java_args}"/>
</java>
</target>

Related

Read Ant properties file generated by Apache FOP

I am new to Ant. I am trying to read two property files, The first is static and second is created during the build process. Please see below.
There is one static property file which I read in at the top: ./cfg/build.properties.
Then again reading another property file inside a target tag. The flow is described below.
There are two targets which will get executed in sequences.
First I am trying to create a property file using FOP in target GENERATE_PROPERTYFILE.
Then on the second target, READ_PROPERTY_FILE_GENERATE_XML, I am reading the property file created in the first step.
But the issue is, it is not picking a value for ${IssueObjects.ID} from the second property file.
Below is the snapshot of script.
<project name="fop4ant" default="run" basedir=".">
<property file="./cfg/build.properties" prefix="System"/>
<property environment="env"/>
<tstamp>
<format property="param_TouchTimeStamp" pattern="yyyyMMddHHmmssSS"/>
</tstamp>
<property name="param_TouchTimeStamp" value="" />
<property name="prop_File_XSL_FetchProbNSolObjects" value="${System.prop_Dir_stylesheet}/${System.prop_File_Stylesheet_FetchProbSol_CNReport}" />
<property name="prop_File_XML" value="${System.prop_Dir_temp}/Object-${param_TouchTimeStamp}.xml" />
<property name="prop_File_XML_Prob" value="${System.prop_Dir_temp}/Prob-${param_TouchTimeStamp}.xml" />
<property name="prop_File_COIDs" value="${System.prop_Dir_temp}/probCO-${param_TouchTimeStamp}.properties" />
<property name="prop_Dir_FOP" value="${env.FOP_HOME}"/>
<property name="prop_Dir_GS" value="${env.GS_HOME}"/>
<taskdef name="fop"
classname="org.apache.fop.tools.anttasks.Fop">
<classpath>
<fileset dir="${prop_Dir_FOP}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${prop_Dir_FOP}/build">
<include name="fop.jar"/>
</fileset>
</classpath>
</taskdef>
<!-- #SECTION_BEGIN :: READ_PROPERTY_FILE_GENERATE_XML -->
<target name="generate-problem-item-productview">
<echo message="prop_XML_FILE_PROB :: ${prop_File_XML_Prob}" level="info" />
<!--Reading dynamically created property file-->
<property file="$prop_File_COIDs" prefix="IssueObjects"/>
<exec executable="export.exe">
<arg line="-xml_file=${prop_File_XML_Prob} -transfermode=${IssueObjects.ID}">
</exec>
</target>
<!-- #SECTION_END :: READ_PROPERTY_FILE_GENERATE_XML -->
<!-- #SECTION_BEGIN :: GENERATE_PROPERTYFILE-->
<target name="fetch-prob-sol-items">
<echo message="prop_File_XML :: ${prop_File_XML}" level="info" />
<echo message="prop_File_XSL_FetchProbNSolObjects :: ${prop_File_XSL_FetchProbNSolObjects}" level="info" />
<echo message="prop_File_COIDs :: ${prop_File_COIDs}" level="info" />
<echo message="prop_Dir_FOP :: ${prop_Dir_FOP}/fop.bat" level="info" />
<exec executable="${prop_Dir_FOP}/fop.bat">
<arg value="-xml"/>
<arg value="${prop_File_XML}"/>
<arg value="-xsl"/>
<arg value="${prop_File_XSL_FetchProbNSolObjects}"/>
<arg value="-foout"/>
<arg value="${prop_File_COIDs}"/>
</exec>
</target>
<!-- #SECTION_END :: GENERATE_PROPERTYFILE -->
<target name="run" depends="">
<echo message="start :: run" level="info" />
<antcall target="fetch-prob-sol-items" />
<antcall target="generate-problem-item-productview" />
<echo message="end :: run" level="info" />
</target>
</project>
In the READ_PROPERTY_FILE_GENERATE_XML section, replace the following...
<property file="$prop_File_COIDs" prefix="IssueObjects"/>
...with...
<property file="${prop_File_COIDs}" prefix="IssueObjects"/>
In the above example, curly braces have been added around the prop_File_COIDs reference.

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 - 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.

Ant build to fail but always execute a target in Jenkins

[Solved] - The correct ant contrib jar was not getting picked up from the default location on my system. Have to give path manually in the build xml like this:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/usr/share/ant/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
-------------------------------------------
Q: I have a python script that is run through ant/Jenkins like this:
<project name="prjName">
<target name="preRun" description="do something">
....
</target>
<target name="Run" description="Run the python script">
<exec executable="python" failonerror="true">
<arg value="${basedir}/run.py" />
<arg value="something" />
</exec>
</target>
<target name="other1" description="do something">
....
</target>
<target name="other2" description="do something">
....
</target>
</project>
Now this python script runs an external tool (web-inject which produces some result files) and keeps on scanning for the word FAIL in the logs. As soon as it finds FAIL, it does sys.exit("Error")
Thus the build fails but I still want to execute the target - other1. Is it possible through try-catch? I am doing it like this but it isn't working
<macrodef name="test-case">
<sequential>
<trycatch>
<try>
<exec executable="python" failonerror="true">
<arg value="${basedir}/read.py" />
</exec>
</try>
<catch>
<echo>Investigate exceptions in the run!</echo>
</catch>
<finally>
<antcall target="other1" />
</finally>
</trycatch>
</sequential>
</macrodef>
<target name="other1" description="do something">
....
</target>

problem with ant script properties

The following is my ant script:
<project name="nightly_build" default="main" basedir="C:\Work\6.70_Extensions\NightlyBuild">
<target name="init">
<sequential>
<exec executable="C:/Work/Searchlatestversion.exe">
<arg line='"/SASE Lab Tools" "6.70_Extensions/6.70.102/ANT_SASE_RELEASE_"'/>
</exec>
<property file="C:/Work/latestbuild.properties"/>
<sleep seconds="10"/>
<echo message="The product version is ${Product_Version}"/>
<exec executable="C:/Work/checksnapshot.exe">
<arg line='"ANT_SASE_RELEASE_${Product_Version}_SASE Lab Tools-NightlyBuild" ANT_SASE_RELEASE_${Product_Version}_AnalyzerCommon-NightlyBuild ${Product_Version}-AppsMerge' />
</exec>
<property file="C:/Work/checksnapshot.properties"/>
<tstamp>
<format property="suffix" pattern="ddMMyyyyHHmm"/>
</tstamp>
</sequential>
</target>
<target name="main" depends="init">
<echo message="loading properties files.." />
<sleep seconds="10"/>
<echo message="Backing up folder" />
<move file="C:\NightlyBuild\NightlyBuild" tofile="C:\NightlyBuild\NightlyBuild.${suffix}" failonerror="false" />
<parallel>
<exec executable="C:/Work/sortfolder.exe">
<arg line="6" />
</exec>
<exec executable="C:/Work/6.70_Extensions/NightlyBuild/antc.bat">
</exec>
</parallel>
</target>
</project>
Basically the sequence goes something like this:
I will run Searchlatestversion.exe and write latestbuild.properties
Using the latestbuild.properties i will obtain ${Product_Version} and would like to allow checksnapshot.exe access to latestbuild.properties and obtain ${Product_Version}
checksnapshot.exe will then generate checksnapshot.properties which will then be used by the target in main antc.bat
am i doing something wrong over here? seems like ${Product_Version} is not being received well by checksnapshot.exe
You appear to have a hard coded wait period of 10 seconds for Searchlatestversion to write out your file. If the executable does not complete inside that time, ${Product_Version} cannot be read from file.
Have you considered using the Waitfor Ant Task? As the name implies, this will wait for a certain condition before it will allow the rest of the task to progress. You could do something like
<property name="props.file" value="C:/Work/latestbuild.properties"/>
<waitfor maxwait="10" maxwaitunit="second">
<available file="${props.file}"/>
</waitfor>
<property file="${props.file}"/>
Does Searchlatestversion.exe produce the file C:/Work/latestbuild.properties?
If so, should you not sleep/wait before you load that properties file?
You have this:
<exec .../>
<property file="C:/Work/latestbuild.properties"/>
<sleep seconds="10"/>
Should you not have this:
<exec ... />
<sleep seconds="10"/>
<property file="C:/Work/latestbuild.properties"/>

Resources