How to use attribute URI in ANT MacroDef task? - ant

I read doc page on http://ant.apache.org/manual/Tasks/macrodef.html, but I can understand.
There are no examples.
How to use attribute URI in ANT MacroDef task?

I found solution here:
http://mail-archives.apache.org/mod_mbox/ant-user/200606.mbox/%3C4489507F.5050503#mindspring.com%3E
<project name="test" xmlns:me="x.y.z">
<macrodef name="sample" uri="x.y.z">
<sequential>
<echo>Hello world</echo>
</sequential>
</macrodef>
<me:sample/>
</project>

Related

Structure ant projects

I am currently writing an ant project xml file and I am looking for some hints and tips to improve the structure and readability of the project.
<target name="eatnutsOnClient" >
<monkey.eatnuts clientName="${clientName}" label="${nutLabel}" />
<if><not> <equals arg1="${returnCode}" arg2="0"/> </not><then>
<echo message="eatnuts-[${nutlabel}]_[${returnCode}]${line.separator}" file="${reachedFile}" append="true" />
</then></if>
</target>
<target name="eatnuts" depends="createClient,eatnutsOnClient,destroyClient"/>
In order to manage the return codes I would like to have the possibility to replace the full if section that I need to replicate over quite some targets by a sort of function which I can call to handle the returncode logic. I guess one option would be to create a target which only contains the if section and add it to the depend list of each task? Are there better ways?
An Ant <macrodef> provides a function-like way to share code:
<project name="ant-macrodef-echo" default="run">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<macrodef name="echo-macrodef">
<attribute name="returnCode"/>
<sequential>
<if>
<not>
<equals arg1="#{returnCode}" arg2="0"/>
</not>
<then>
<echo message="#{returnCode}" />
</then>
</if>
</sequential>
</macrodef>
<target name="run">
<echo-macrodef returnCode="42"/>
<echo-macrodef returnCode="0"/>
<echo-macrodef returnCode="-9"/>
</target>
</project>
Results:
run:
[echo] 42
[echo] -9

How to <foreach> in a <macrodef>?

I have a xml just like below:
<data>
<foo>value1</foo>
<foo>value2</foo>
<foo>value3</foo>
</data>
I want to create macrodef which implements below function:
<?xml version="1.0"?>
<project name="OATS" default="execute" basedir=".">
<xmlproperty file="data.xml" collapseAttributes="true"/>
<target name="execute">
<foreach list="${data.foo}" target="runScript" param="script"/>
</target>
<target name="runScript">
<echo>Doing things with ${script}</echo>
</target>
</project>
Anybody knows how to ? Thanks in advance.
xmltask is the best choice in the Ant community for this purpose, and you don't have to define your own macrodef.
So for instance:
<tools:xmltask source="data.xml" report="false" >
<tools:call path="data/foo">
<param name="value" path="text()"/>
<actions>
<echo>Doing things with #{value}</echo>
</actions>
</tools:call>
</tools:xmltask>
I encourage you to read the user manual, for xmltask has lots of options. It basically supports XPath to extract and iterate any portion of your xml. It also supports calls to existing targets in addition to anonymous code blocks (as in the example).
It's just hard to beat.
The following example uses the groovy ANT task
<project name="OATS" default="execute" basedir=".">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
<classpath>
<pathelement location="lib/groovy-all-2.1.0-rc-2.jar"/>
</classpath>
</taskdef>
<target name="execute">
<groovy>
def data = new XmlSlurper().parse(new File("data.xml"))
data.foo.each {
properties["script"] = it
ant.project.executeTarget("runScript")
}
</groovy>
</target>
<target name="runScript">
<echo>Doing things with ${script}</echo>
</target>
</project>
This is my macrodef.
<?xml version="1.0" encoding="UTF-8"?>
<project name="OATS" default="test" basedir=".">
<property environment = "env"/>
<path id = "antcontrib.path">
<fileset file = "${env.ANT_HOME}/../net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar"/>
</path>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="antcontrib.path"/>
<macrodef name="runOATS">
<attribute name="suite"/>
<attribute name="toDir"/>
<sequential>
<delete dir="#{toDir}"/>
<mkdir dir="#{toDir}"/>
<xmlproperty file="#{suite}" collapseAttributes="true"/>
<for list="${data.foo}" param="script">
<sequential>
<runScript script="#{script}"/>
</sequential>
</for>
</sequential>
</macrodef>
<macrodef name="runScript">
<attribute name="script"/>
<sequential>
<echo>Doing things with #{script}</echo>
</sequential>
</macrodef>
<target name="test">
<runOATS toDir="/OATS/results" suite="data.xml"/>
</target>
</project>

How to pass multiple parameters to a target in Ant?

I have this dummy target:
<mkdir dir="${project.stage}/release
<war destfile="${project.stage}/release/sigma.war">
...
...
</war>
What I want to do is provide two parameters say "abc" & "xyz" which will replace the word release with the values of abc and xyz parameters respectively.
For the first parameter say abc="test", the code above will create a test directory and put the war inside it.Similarly for xyz="production" it will create a folder production and put the war file inside it.
I tried this by using
<antcall target="create.war">
<param name="test" value="${test.param.name}"/>
<param name="production" value="${prod.param.name}"/>
</antcall>
in the target which depends on the dummy target provided above.
Is this the right way to do this.I guess there must be some way to pass multiple parameters and then loop through the parameters one at a time.
unfortunately ant doesn't support iteration like for or foreach loops unless you are refering to files. There is however the ant contrib tasks which solve most if not all of your iteration problems.
You will have to install the .jar first by following the instructions here : http://ant-contrib.sourceforge.net/#install
This should take about 10 seconds. After you can simply use the foreach task to iterate through you custom list. As an example you can follow the below build.xml file :
<project name="test" default="build">
<!--Needed for antcontrib-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<target name="build">
<property name="test" value="value_1"/>
<property name="production" value="value_2"/>
<!--Iterate through every token and call target with parameter dir-->
<foreach list="${test},${production}" param="dir" target="create.war"/>
</target>
<target name="create.war">
<echo message="My path is : ${dir}"/>
</target>
</project>
Output :
build:
create.war:
[echo] My path is : value_1
create.war:
[echo] My path is : value_2
BUILD SUCCESSFUL
Total time: 0 seconds
I hope it helps :)
Second solution without using ant contrib. You could encapsulate all your logic into a macrodef and simply call it twice. In any case you would need to write the two parameters at some point in your build file. I don't think there is any way to iterate through properties without using external .jars or BSF languages.
<project name="test" default="build">
<!--Needed for antcontrib-->
<macrodef name="build.war">
<attribute name="dir"/>
<attribute name="target"/>
<sequential>
<antcall target="#{target}">
<param name="path" value="#{dir}"/>
</antcall>
</sequential>
</macrodef>
<target name="build">
<property name="test" value="value_1"/>
<property name="production" value="value_2"/>
<build.war dir="${test}" target="create.war"/>
<build.war dir="${production}" target="create.war"/>
</target>
<target name="create.war">
<echo message="My path is : ${path}"/>
</target>
</project>
I admit that I don't understand the question in detail. Is ${project.stage} the same as the xyz and abc parameters? And why are there two parameters xyz and abc mentioned, when only the word "release" should be replaced?
What I know is, that macrodef (docu) is something very versatile and that it might be of good use here:
<project name="Foo" default="create.wars">
<macrodef name="createwar">
<attribute name="stage" />
<sequential>
<echo message="mkdir dir=#{stage}/release " />
<echo message="war destfile=#{stage}/release/sigma.war" />
</sequential>
</macrodef>
<target name="create.wars">
<createwar stage="test" />
<createwar stage="production" />
</target>
</project>
The output will be:
create.wars:
[echo] mkdir dir=test/release
[echo] war destfile=test/release/sigma.war
[echo] mkdir dir=production/release
[echo] war destfile=production/release/sigma.war
Perhaps we can start from here and adapt this example as required.

Apache Ant: Trying to use map lists to arguments using nested for loops. What should I REALLY be doing?

So, I've got a list of ${locales} and ${externs} and I'm trying to do this. It isn't working. Ant says that java doesn't support the nested "for" element. I suspect my approach is wrong. How should I be trying to do this?
<for list="${locales}" param="locale">
<sequential>
<java jar="whatever.jar" fork="true">
<for list="${externs}" param="extern-file">
<arg line='-f "--externs=${extern_dir}/#{extern-file}"' />
</for>
<arg line="... more stuff" />
</java>
</sequential>
</for>
Ant command-line line args support prefixes and suffixes, which might do the trick for this.
If ${externs} is a comma-separted list you'll need to make it space-separated, perhaps using the ant-contrib propertyregex task.
In outline:
<propertyregex property="externs.arg"
input="${externs}"
regexp="," replace=" " />
<for list="${locales}" param="locale">
<sequential>
<java jar="whatever.jar" fork="true">
<arg line="${externs.arg}"
prefix='-f "--externs=${extern_dir}/'
suffix='"' />
<arg line="... more stuff" />
</java>
</sequential>
</for>
Here are some alternative options you could consider.
Implement a custom Ant task to do what you need.
If you own the Java code you are invoking, update it to be more friendly to your requirements, e.g.
2.1. Passing list of locales and externs.
2.2. Accepting input from a file or standard input (both of which you could write to from Ant)
If you don't own the Java code, implement your own script or Java code to provide an adapter from something easy to pass from Ant to the required args of the target.

How to <copy> in a <macrodef> in ant?

I'm trying to copy files in a macro, like so:
<project name="why" default="go">
<macrodef name="copy-some-stuff">
<attribute name="file.name" />
<copy todir="/var/tmp">
<fileset file="${file.name}" />
</copy>
</macrodef>
<target name="go">
<copy-some-stuff file.name="/etc/hosts" />
</target>
</project>
but I get the following
BUILD FAILED
b.xml:3: macrodef doesn't support the nested "copy" element.
Any ideas, other than "yes, indeeed, macrodef doesn't support the nested "copy" element." I got that much. I'm looking for why this limitation is here and a possible workaround (without using antcall).
Try surrounding the <copy> element with <sequential>:
<macrodef name="copy-some-stuff">
<attribute name="file.name" />
<sequential>
<copy todir="/var/tmp">
<fileset file="#{file.name}" />
</copy>
</sequential>
</macrodef>

Resources