How to macro-ify ant targets? - ant

I want to be able to have different targets doing nearly the same thing, as so:
ant build <- this would be a normal (default) build
ant safari <- building the safari target.
The targets look like this:
<target name="build" depends="javac" description="GWT compile to JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="${lhs.target}"/>
</java>
</target>
<target name="safari" depends="javac" description="GWT compile to Safari/JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="${lhs.safari.target}"/>
</java>
</target>
(Nevermind the first thought that strikes: throw out ant! That's not an option just yet.) I tried using macrodef, but got a strange error message (even though the message didn't imply it, it think it had to do with putting a target in sequential). I don't want write a cmdline as so: ant -Dwhatever=nevermind. Any ideas?

My first try (without being able to test it at the moment):
<target name="build" depends="javac, create.mymacro" description="GWT compile to JavaScript">
<mymacro target="${lhs.target}"/>
</target>
<target name="safari" depends="javac, create.mymacro" description="GWT compile to Safari/JavaScript">
<mymacro target="${lhs.safari.target}"/>
</target
<target name="create.mymacro">
<macrodef name="mymacro">
<attribute name="target" default="${lhs.target}"/>
<sequential>
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="#{target}"/>
</java>
</sequential>
</macrodef>
</target>

Related

How can I zip each file separately instead of putting in one archive?

I have 3 files (eg: m1.txt, m2.txt and m3.txt) in my /tmp/ folder. How can I zip each files as m1.zip, m2.zip and m3.zip?
I used the following coding, but didn't work:
<target name="xzipit">
<foreach target="zipone" param="Files">
<path>
<fileset dir="/tmp/" excludes="*.zip"
includes="*.txt" casesensitive="no" />
</path>
</foreach>
</target>
<target name="zipone">
<zip destfile="${Files}" basedir="/tmp/">
</zip>
</target>
Note use of fileset id, refid usage below and also on specifying includesfile to zip ant task
<fileset dir="/tmp" excludes="*.zip" includes="*.txt" id="zip.fileset.id" casesensitive="no" />
<pathconvert property="zipFileSetProp" refid="zip.fileset.id">
<mapper>
<flattenmapper/>
</mapper>
</pathconvert>
<for list="${zipFileSetProp}" param="fileToZip" delimiter=";">
<sequential>
<echo>fileToZip #{fileToZip}</echo>
<zip destfile="/tmp/#{fileToZip}.zip" basedir="/tmp/" includesfile="/tmp/#{fileToZip}"/>
</sequential>
</for>
I got one more answer to solve this.
<project name="texzip" basedir="." default="zipit">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="/tmp/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="zipit">
<foreach target="zipone" param="Files">
<path>
<fileset dir="/tmp/" excludes="*.zip" includes="*.txt" casesensitive="no" />
</path>
</foreach>
</target>
<target name="zipone">
<basename property="basename" file="${Files}" suffix="txt"/>
<zip destfile="${basename}.zip">
<zipfileset dir="." includes="${basename}.txt"/>
</zip>
</target>
</project>

Passing data file as generic file

I have ANT build file having these two tasks-
<target name="ldm-validation">
<property name="graphFile" value="${tools.dir}/build-config/SPARQL/*.ttl"/>
<record name="${tools.dir}/build-config/SPARQL/BuildLog.txt" action="start"/>
<foreach target="jena-sparql-validation" param="queryFile">
<path>
<fileset dir="${tools.dir}/build-config/SPARQL/Queries">
<include name="*.rq"/>
</fileset>
</path>
</foreach>
<record name="${tools.dir}/build-config/SPARQL/BuildLog.txt" action="stop"/>
</target>
<target name="jena-sparql-validation">
<java classname="arq.sparql" fork="true" outputproperty="javaresult" errorproperty="javaerror1">
<arg value="--data=${graphFile}"/>
<arg value="--query=${queryFile}"/>
<jvmarg value="-Xmx1024M"/>
<classpath>
<path>
<fileset dir="${jena.dir}/lib">
<include name="*.jar"/>
</fileset>
</path>
</classpath>
</java>
<fail message="Error at: ${javaerror1} in ${queryFile}">
<condition>
<not>
<equals arg1="${javaerror1}" arg2=""/>
</not>
</condition>
</fail>
<echo message="Result for ${queryFile} is: ${javaresult}"/>
</target>
But when I am running this it is always failing saying that -
C:\CI-POC\tools\build-config\validate.all.xml:41: Error at: Failed to load data
It is unable to get the Data file using the Property name 'graphFile'. I am not sure what is going wrong. Can any one help.
Try calling the build as follows:
ant ldm-validation jena-sparql-validation
so that the values of the properties graphFile and queryFile are set.
Another option is to create a dependency between the two targets.
<target name="jena-sparql-validation" depends="ldm-validation">

Customised ANT task

I have a ANT build.xml file which goes like this-
<?xml version="1.0"?>
<project name="apache-jena-2.10.0" basedir="." default="notifyme">
<target name="notifyme">
<java classname-"arq.sparql" fork="true">
<arg value="--data=C:\apache-jena-2.10.0\test.ttl"/>
<arg value="--query=C:\apache-jena-2.10.0\ASKTest.rq"/>
<jvmarg value="-Xmx1024M"/>
<classpath>
<path>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
</classpath>
</java>
</target>
</project>
This build.xml basically run a query and return a specefic result.
The result comes like this-
notifyme:
[java] Ask =>No
BUILD SUCCESSFUL
Total time : 1second
Now my question is there any way I can make the build fail if Ask => No, if yes can any one please help me to customise the ANT build file.
Kind regards
Som
Use the resultproperty attribute for java task. It will store standard out in the given property. Then us the fail task, with conditions task:
<?xml version="1.0"?>
<project name="apache-jena-2.10.0" basedir="." default="notifyme">
<target name="notifyme">
<java classname-"arq.sparql" fork="true" failonerror="false" outputproperty="javaresult">
<arg value="--data=C:\apache-jena-2.10.0\test.ttl"/>
<arg value="--query=C:\apache-jena-2.10.0\ASKTest.rq"/>
<jvmarg value="-Xmx1024M"/>
<classpath>
<path>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
</classpath>
</java>
<fail>
<condition>
<matches string="${javaresult} pattern="No"/>
</condition>
</fail>
</target>
</project>
Did not test it. But you can get the idea.

Build.xml issue

Selenium - ANT -TestNG
I have written a build.xml, where it produces a error stating " classname attribute of taskdef element is undefined "
Here is my build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name ="AutomationScripts" default="test" basedir=".">
<echo message ="Testing selenium server... Plz wait"/>
<target name="startServer">
<echo message ="Start selenium server... Plz wait"/>
<java jar="..\lib\selenium-server-standalone-2.19.0.jar" fork="true">
<jvmarg value="-Dhttp.proxyHost=192.168.0.200"/>
<jvmarg value="-Dhttp.proxyPort=3128"/> </java>
<echo message ="Started selenium server"/>
</target>
<target name="test" depends="startServer">
<echo message="Test run. Please wait"/>
<mkdir dir="out" />
<java classname="RosettastoneMain" classpath="..\AutomationScripts\bin"
dir="C:\Program Files\Java\jdk1.6.0_11\bin">
<classpath>
<fileset dir="..\AutomationScripts\lib" includes="*.*"/>
</classpatha></java>
<taskdef name="testng" classpath="org.testng.TestNG"> ---------> It produces
error in this stmt
<classpath>
<pathelement location="../lib/testng-6.2.jar"/>
</classpath>
</taskdef>
<property name="testng.output.dir" value="testngOutput"/>
<path id="classes">
<fileset dir="../lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${bin.dir}"/>
</path>
<mkdir dir="${testng.output.dir}"/>
<testng outputdir="${testng.output.dir}" classpathref="classes">
<xmlfileset dir="." includes="testng.xml"/>
</testng> </target>
<target name="stopServer">
<echo message="stop selenium server. Plz wait"/>
<get taskname="selenium-shutdown"
src="http://localhost:4444/selenium-server-standalone-2.0rc2/driver/?cmd=shutDown"
dest="./out/sever.stop.status.txt" ignoreerrors="true"/>
</target>
</project>
Can any one help me out
thanks in advance
You need to specify the class implementing the data type in the 'classname' attribute.
According to the Ant taskdef documentation (and more specifically typedef), this task has two required attributes - 'name' and 'classname', unless 'file' or 'resource' have been specified. The attribute 'classpath' only defines the locations where the class specified in 'classname' can be found.

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>

Resources