Define two param in for loop in ant script - ant

I am working on ant script and I am defining two param but script says you have already defined the param.Can yopu define how could I define two param in for loop in ant script.
<project name="tomcat_win_deploy" basedir="." default="usage">
<!--taskdef resource="net/sf/antcontrib/antcontrib.properties"/-->
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="env.copy.local.props">
<for param="host" list="${deploy.host}" param="path" list="${deploy.path}">
<sequential>
<copy todir="\\#{host}\${deploy.path}\conf\"
file="Properties/${deploy.env}/local_for_test.properties"
overwrite="true"/>
</sequential>
</for>
</target>
</project>

One way might be to define a list of delimited property pairs and process that in a loop, splitting each property into its two values.
So instead of having separate host and path lists:
hosts=a,b,c
paths=/a/,/b/,/c/
you might have a single host_path list:
host_path=a /a/, b /b/, c /c/
The trick then would be how to split variables and use them in the loop (and not running into the ant "properties are immutable" feature).
This answer shows how you could use the Ant Flaka addon to achieve exactly that.

Related

Call a <macrodef> for each <pathelement> in a <path> in Ant

I have a path as follows:
<path refid="myjarlocation"/>
Now I want to iterate over this path and for each value present in the path I want to call a macrodef with that value as one of the property to be used inside the marcodef.
In case of target we can easily do it as follows:
<foreach target="mytarget" param="jarloc">
<path refid="myjarlocation"/>
</foreach>
I cannot use for each because I need to pass multiple parameters , so I am using macrodef. So, therefore the question how to iterate over a path and call a macrodef instead of a target.
I've made something similar work by using ant-contrib's for task to iterate a path and passing the path element along to a macrodef.
First get ant-contrib in your project - see http://ant-contrib.sourceforge.net/
Next, define your macrodef in your ant build however you want including some attribute that will take your path element. eg:
<macrodef name="awesome-macro">
<attribute name="path-to-deal-with"/>
<attribute name="unrelated-attribute"/>
<sequential>
...
</sequential>
</macrodef>
Then, use the for task to iterate the path into pathelements and invoke the macro:
<for param="path.element">
<fileset dir="${jars.dir}">
<include name="*.jar"/>
</fileset>
<sequential>
<awesome-macro path-to-deal-with="#{path.element}" unrelated-attribute="whatever"/>
</sequential>
</for>
Note the use of #{path.element} as opposed to ${path.element} inside the for loop to refer to the looping parameter!

How to pass nested arguments from one ant target to another

How to pass nested arguments from one ant target to another?
I need to pass a variable number of nested elements from one target to another.
I have a common file with all of my standard build tasks that's included in all of my projects.
I am adding a new custom task that takes a variable number of nested arguments
As a standard, all ant calls are made through the common file to ensure consistency of build style and logging.
Thus the new custom task and its nested child will be defined in the common script.
The project build script looks like this
<target name="projectBuild">
...
<ant target="_newFooTaskWrapper" antfile="commonFile">
<property name="_arg1" value="hello"/>
<property name="_arg2" value="world"/>
<nestedArg value="qux"/>
<nestedArg value="baaz"/>
...
<nestedArg value="AAAAA"/>
</ant>
...
</target>
The common script looks like this:
<target name ="_newFooTaskWrapper">
<echo message="Target _newFooTaskWrapper in project ${ant.project.name} from base directory ${basedir}"/>
<echo message="arg1 = ${_arg1}"/>
<echo message="arg2 = ${_arg2}"/>
<taskdef name="newFooTask" classname="org.foo.NewFooTask"/>
<typedef name="nestedArg" classname="org.foo.NewFooTask$NestedArg"/>
<newFooTask arg1="${_arg1}" arg2="${_arg2}">
<nestedArg value="qux"/>
<nestedArg value="baaz"/>
...
<nestedArg value="AAAAA"/>
</newFooTask>
Obviously, this isn't right. My question is, what's the right way to do this?
I need to pass a variable number of nested elements from one target to another.
For "varible", I assume you mean you don't know the exact number of the nested elements you want to pass to the task, so what you want is something like method(Object param...) in java, is it?
It's not a good idea to try such a way. Ant is not a scripting language but a build tool. It provides limited "scripting" possibilities.
However, you can try it in the following two ways:
1, If your nested element is just in the form of <elementName value="xx" />, you don't need anything complicated. Just pass another property containing a comma seperated list of the values, and process the list in your custom ant task. It's easy for Java to split the property into a list and process it.
2, If your nested element may be more complicated... maybe you can try reference:
Make a type fooTaskParams which can be referenced via an id:
<fooTaskParams id="_foo_task_params">
<nestedArg value="qux"/>
<nestedArg value="baaz"/>
...
<nestedArg value="AAAAA"/>
</fooTaskParams>
and pass the reference to the other build file:
<ant target="_newFooTaskWrapper" antfile="commonFile">
<property name="_arg1" value="hello"/>
<property name="_arg2" value="world"/>
<reference refid="_foo_task_params"/>
</ant>
and then make your task to be able to process the reference:
<newFooTask arg1="${_arg1}" arg2="${_arg2}" paramRefId="_foo_task_params" />
You may need to take care of reference override, or make your task able to process the ref as well as taking nested elements.
Read ant's manual about <ant> and <typedef> for more about this approach, and refer to SO Q&As like this when you encount any problem.

Why does ANT update the contents of a fileset after it was created, and can I override this?

I think this may be easiest explained by an example, so here goes:
<target name="test">
<fileset id="fileset" dir="target">
<include name="*"/>
</fileset>
<echo>${toString:fileset}</echo>
<touch file="target/test"/>
<echo>${toString:fileset}</echo>
</target>
Outputs:
test:
[echo]
[touch] Creating target/test
[echo] test
What I ideally want is to have the fileset stay the same so I can have a before/after set (in order to get a changed set using <difference>, so if you know of a way to skip right to that...).
I've tried using <filelist> instead, but I can't get this correctly populated and compared in the <difference> task (they're also hard to debug since I can't seem to output their contents). I also tried using <modified/> to select files in the fileset, but it doesn't seem to work at all and always returns nothing.
Even if there is an alternative approach I would appreciate a better understanding of what ANT is doing in the example above and why.
The path selector is evaluated on the fly. When a file is added, it will reflect in the set when you use it.
You may able to evaluate and keep it in variable using pathconvert. Then this can be converted back to filest using pathtofilest
A fileset is something like a selector. It's a set of "instructions" (inclusions, exclusions, patterns) allowing to get a set of files.
Each time you actually do something with the fileset (like printing the files it "references"), the actual set of files is computed based on the "instructions" contained in the fileset.
As Jayan pointed out it might be worth posting the final outcome as an answer, so here's a simplified version with the key parts:
<fileset id="files" dir="${target.dir}"/>
<pathconvert property="before.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<!-- Other Ant code changes the file-system. -->
<pathconvert property="after.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<filelist id="before.files" files="${before.files}"/>
<filelist id="after.files" files="${after.files}"/>
<difference id="changed.files">
<filelist refid="before.files"/>
<filelist refid="after.files"/>
</difference>

Passing optional path arguments to ant

I have an ant task that uses an apply task to run a script on a group of files.
I have a directory structure resultant of something like this:
mkdir -p a/{b,c,d,e}/f
Normally (if I pass no arguments), I would like ant to run on all fs.
That is, if I called ant mytask, it should process: a/b/f, a/c/f, a/d/f, a/e/f. This already works using apply and patternsets.
However, when I pass it an optional argument called foo, it should only call the script on a/foo/f.
So if I called ant mytask -foo b, it should process a/b/f only, and not the others.
I have read this SO post, which explains ways of passing arguments, and I have looked at the ant documentation regarding properties, and conditionals. But I am still unable to piece them together in a way that works.
Also, I do not want to use one of the suggestions from the SO above which called for arguments like this:
<arg value="${arg0}"/>
<arg value="${arg1}"/>
I want to be able to call it as ant mytask -foo valueoffoo for any arbitrary foo.
Thanks.
I tried martin clayton's suggestion below and have code like:
<target name="mytask">
<property name="foo" value="*" />
<apply executable="perl">
<arg value="somescript"/>
<dirset id="them" dir="a">
<include name="${foo}/*/f" />
</dirset>
</apply>
</target>
The above does what I want.
Note 1: In my actual code I use a patternset instead of dirset but it should work the same.
Note 2: In my original question I said the directory structure was a/{b,c,d,e}/f. It is in fact a bit more complicated, hence the * in the include above. I omitted that the first time around because it did not seem relevant.
You can do this - albeit with a slightly different command-line syntax -
using a property 'override'.
First, in the buildfile, construct your fileset or dirset from a property foo,
something like this:
<property name="foo" value="*" />
<dirset id="them" dir="a">
<include name="${foo}/f" />
</dirset>
This will give you your default behaviour - processing all
subdirectories of a that themselves have a subdirectory f.
Now, if you run Ant like this:
ant -Dfoo=d
Only directory a/d/f will be processed.
This works because Ant properties are not mutable - well, not normally anyway -
so the command-line definition of foo prevents the one within the buildfile from being used.

Can a macrodef Task be Called Dynamically

I have a number of macrodef tasks that I would like to call but would like to wrap the calls with some timestamping code. This could be easily done if the tasks were targets instead of macrodefs by using antcall.
Is it possible to do the same thing with macrodef?
Example:
The "macrocall" line is the hypothetical kind of task that I would like to use (akin to "antcall")
<target name="run.tests">
<run.named.test name="macro1" />
<run.named.test name="macro2" />
</target>
<macrodef name="run.named.test">
<attribute name="name" />
<sequential>
<echoTime />
<macrocall name="#{name}" />
<echoTime />
</sequential>
</macrodef>
<macrodef name="macro1">
</macrodef>
<macrodef name="macro2">
</macrodef>
Why not wrap your macro1 macro2 with targets and use an antcall for the "macrocall". the new macro1, macro2 targets will each run in their own project (properties and ref passed in, but not back out) which may or may not be a good thing depending on what you're trying to accomplish.
There no pure ant way to accomplish what you want as far as i know -- dynamically calling a task. You'd probably need to find some outside package to even come close, but it would probably be uglier.
This is a very late reply, but I had been struggling with the exact same issue for a while and just now came up with a solution, so I figured I'd contribute.
First of all, I think it's safe to definitively say that there is no way to do this simply with Ant alone, outside of doing something extremely hacky like echoing Ant code to a new file and then calling a macrodef from the file.
Anyway, I decided to use the Groovy Ant task to run some Groovy code. Here's what I came up with:
<groovy>
ant."${properties["macrodef.name"]}"("dir":properties["dir"])
</groovy>
Explanation:
-ant. is simply the prefix for telling Groovy to run an Ant task. For example, ant.echo() runs the <echo> task.
-"${properties["macrodef.name"]}" pulls the property named "macrodef.name" from my Ant project.
-With the two above combined together like this, I'm telling Groovy to run an Ant task with the same name as the value of the property "macrodef.name". For example, if ${macrodef.name} in my Ant project currently holds the value of "compile", Groovy will read this line as ant.compile.
-("dir":properties["dir"]) tells Groovy to run the macrodef with the attribute "dir" using the value of the Ant property also named "dir". To be clear, this is because my macrodef requires this attribute. In Ant, it would look like this: <compile dir="${dir}" />
I hope this helps anyone who comes across it! For the record, I wanted to avoid using the more generic <script> task, because apparently it runs noticeably slower than basic Ant or the Groovy task. The ideal solution would probably be to actually write a custom Ant task, but unfortunately I don't quite have to knowledge to do that yet.
There are two ways to resolve macrodef name dynamically
1) Macrodef names are not constant and resolved during load time depending on "what-to-say" variable. As a result, only one macrodef gets "say-something" name, there other one's name is not resolved (so not available to call)
<property name="what-to-say" value="bye"/>
<property name="say-${what-to-say}" value="say-something"/>
<macrodef name="${say-hi}">
<sequential>
<echo>hi!</echo>
</sequential>
</macrodef>
<macrodef name="${say-bye}">
<sequential>
<echo>bye!</echo>
</sequential>
</macrodef>
<target name="test">
<say-something/>
</target>
2) Create two additional files with macrodef definitions, e.g.
<project name="macrodefs-hi.xml>
<macrodef name="say-something">
<sequential>
<echo>hi!</echo>
</sequential>
</macrodef>
</project>
<project name="macrodefs-bye.xml>
<macrodef name="say-something">
<sequential>
<echo>bye!</echo>
</sequential>
</macrodef>
</project>
And include just one of them to your main project
<property name="what-to-say" value="bye"/>
<import file="macrodefs-${what-to-say}.xml"/>
<target name="test">
<say-something/>
</target>

Resources