Ant SQL task errorproperty example - ant

Does anyone know of an example on how to use and SQL TASK errorproperty?
I am trying to do something like this:
<sql
driver="${jdbc.driver.class}"
url="${jdbc.url}"
userid="${db.admin.user}"
password="${db.admin.password}"
onerror="continue"
autocommit="true"
delimiter="/"
print="true"
failOnConnectionError="false"
errorproperty="dberror">
<classpath>
<pathelement location="${jdbc.driver.location}"/>
</classpath>
${query.drop}
</sql>
<if>
<isset dberror>
do this
<else>
do that
</if>

With Ant < 1.9.3 you go like that :
<project>
<target name="calldb"
<sql .. errorproperty="dberror"/>
<condition property="isdberror">
<isset property="dberror"/>
</condition>
</target>
<target name="ifdberror" if="isdberror">
...
</target>
<target name="nodberror" unless="isdberror">
...
</target>
<!-- create target dependency chain -->
<target name="whatever" depends="calldb,ifdberror,nodberror"/>
</project>
With Ant >= 1.9.3 you go like that :
<project
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>
<target name="calldb">
<sql .. errorproperty="dberror"/>
<!-- example, echo instead of real work .. -->
<echo unless:blank="${dberror}">Houston, we have a problem !!</echo>
<echo if:blank="${dberror}">Strike :-)</echo>
</target>
</project>

Related

How to use macrodef?

Lets assume this is my code. buildJar is my macrodef.
<target name="build">
<buildJar build.dir="module1"/>
<buildJar build.dir="module2"/>
</target>
How to invoke macrodef "buildJar" based on some condition? For example, above code can be:
<target name="build">
<if module="module1" >
<buildJar build.dir="module1"/>
</if>
<if module="module2" >
<buildJar build.dir="module2"/>
</if>
</target>
Ant supports if and unless attributes. These can be combined with a directory check using the available task:
<project name="demo" default="build" xmlns:if="ant:if">
<macrodef name="greeting">
<attribute name="name"/>
<sequential>
<echo message="found #{name}"/>
</sequential>
</macrodef>
<available property="found.module1" file="module1"/>
<available property="found.module2" file="module2"/>
<target name="build">
<greeting name="module1" if:true="${found.module1}"/>
<greeting name="module2" if:true="${found.module2}"/>
</target>
</project>

How to check two properties in an if condition of an Ant task?

It is possible to execute an Ant target conditionally by specifying an if or unless clause. As far as I can see this clause accepts only one property. How can I check for two properties?
This is an example:
<project default="test">
<property name="a" value="true"/>
<property name="b" value="true"/>
<target name="test-a" if="a">
<echo>a</echo>
</target>
<target name="test-b" if="b">
<echo>b</echo>
</target>
<target name="test-ab" if="a,b">
<echo>a and b</echo>
</target>
<target name="test" depends="test-a,test-b,test-ab"/>
</project>
If I run it, the test-ab target generates no output:
$ ant -f target-if.xml
Buildfile: target-if.xml
test-a:
[echo] a
test-b:
[echo] b
test-ab:
test:
BUILD SUCCESSFUL
Total time: 0 seconds
How to specify an and expression for the two properties?
Unfortunately, no. From the ant Targets manual:
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>
This is my example with the use of the condition element:
<project default="test">
<property name="a" value="true"/>
<property name="b" value="true"/>
<target name="test-a" if="a">
<echo>a</echo>
</target>
<target name="test-b" if="b">
<echo>b</echo>
</target>
<condition property="a-and-b">
<and>
<equals arg1="${a}" arg2="true"/>
<equals arg1="${b}" arg2="true"/>
</and>
</condition>
<target name="test-ab" if="a-and-b">
<echo>a and b</echo>
</target>
<target name="test" depends="test-a,test-b,test-ab"/>
</project>

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

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>

Need to set path (cp) in ant script depending on value of a property

I want to set a path variable cp to one of two values depending on the value of an property.
E.g. the following is what I'm trying to achieve, but I'm not sure about how to get it working properly...
<if>
<contains string="${jboss.home}" substring="jboss-4.2.3.GA"></contains>
<then>
<echo message="Using JBoss 4.2.3 classpath"/>
<path id="cp">
...
</path>
</then>
<else>
<echo message="Using JBoss 4.0.5 classpath"/>
<path id="cp">
...
</path>
</else>
</if>
<condition property="usejboss423">
<contains string="${jboss.home}" substring="jboss-4.2.3.GA"/>
</condition>
<target name="build.jboss.cp" depends="build.jboss.cp.423,build.jboss.cp.405"/>
<target name="build.jboss.cp.423" if="usejboss423">
<path id="cp">
... JBoss 4.2.3 classpath
</path>
</target>
<target name="build.jboss.cp.405" unless="usejboss423">
<path id="cp">
... JBoss 4.0.5 classpath
</path>
</target>
If you need to do this kind of thing a lot, install AntXtras. You just add a JAR file to Ant's class path, and you can do this:
<condition property="usejboss423">
<contains string="${jboss.home}" substring="jboss-4.2.3.GA"/>
</condition>
<do if="usejboss423">
...
</do>
<do unless="usejboss423">
...
</do>
It has a lot of other useful features.

Resources