-1

How to invoke same target by passing different values to the parameter? I wanted to invoke target using antcall. Based on some falg i wanted to invoke a target by passing different values to the parameter using param.

user3492304
  • 153
  • 2
  • 4
  • 11

1 Answers1

2

Don't use antcall, better use macrodef (introduced with Ant 1.6) instead !!
I won't go into details, just search the web for 'antcall vs. macrodef' and similar..
See also Writing Better Ant Scripts: Techniques, Patterns and Antipatterns
Some snippet :

<project xmlns:if="ant:if">

 <macrodef name="foobar">
  <attribute name="foo"/>
  <attribute name="verbose"/>
  <sequential>
   <echo>@{foo}</echo>
   <echoproperties prefix="ant" if:true="@{verbose}"/>
  </sequential>
 </macrodef>

 <!-- use foobar macrodef with different parameters (attribute values) -->
 <foobar verbose="yes" foo="1. yada,yada"/>
 <foobar verbose="no" foo="2. blabla.."/>

</project>

output :

[echo] 1. yada,yada
[echoproperties] #Ant properties
[echoproperties] #Thu Sep 18 09:31:05 CEST 2014
[echoproperties] ant.core.lib=C\:\\ant194\\lib\\ant.jar
[echoproperties] ant.file=C\:\\area51\\ant\\tryme.xml
[echoproperties] ant.home=C\:\\ant194
[echoproperties] ant.java.version=1.7
[echoproperties] ant.library.dir=C\:\\ant194\\lib
[echoproperties] ant.version=Apache Ant(TM) version 1.9.4 compiled on April 29 2014
[echo] 2. blabla..
Rebse
  • 10,307
  • 2
  • 38
  • 66
  • In my first test (ant 1.7), the properties were always printed. It seems that we need to upgrade ant at least to 1.9.3 for `if:true` to behave correctly. From http://stackoverflow.com/questions/20102601/ant-iftrue-setting-property-to-true-wont-cause-the-task-to-executed] – Seki Sep 18 '14 at 11:44
  • @Seki yes you need to use ant 1.9.3 for `if:true` feature. Nevertheless the snippet is only for demonstration of reuse with different parameters(attribute values), which is independent from that new ant feature. – Rebse Sep 18 '14 at 12:37
  • In this example how to invoke based on some condition? – user3492304 Sep 19 '14 at 06:17
  • @user3492304 with Ant >= 1.9.3 use new if/unless feature with Ant <= 1.9.3 use the old way.Create a checking target with a condition, afterwards use a second target holding your macrodef calls like that – Rebse Sep 19 '14 at 15:24