You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Michael Sunde <ms...@actional.com> on 2004/01/16 22:11:23 UTC

is it possible to create targets dynamically?

Hi,
 
When looking at my build.xml file, there are several targets that are almost identical. The main difference between them is the name of the target and a path. Is it possible to create a target dynamically. I tried using macrodef in ant 1.6 but that did not work:
 
<project name="test" default="all" basedir=".">
<macrodef name="echoTarget">
   <attribute name="name" />
   <sequential>
      <target name="@{name}">
         <echo>@{name} target executed.</echo>
         <!-- the real work goes here. -->
      </target>
   </sequential>
</macrodef>
<echoTarget name="dynamic"/>
<target name="all" depends="dynamic"/>
</project>

Error:
E:\builds\build.xml:11: Following error occured while executing this line
E:\builds\build.xml:5: Could not create task or type of type: target.
 
>From the command line, I would like to be able to do:
   ant dynamic
 
The only way I can think of doing it is to run an xslt on the build file to generate all the targets. Is there another cleaner/simpler solution?
 
Thanks,
Michael
 

Re: is it possible to create targets dynamically?

Posted by Claas Thiele <ct...@ct42.de>.
Michael Sunde wrote:
> Hi,
>  
> When looking at my build.xml file, there are several targets that are almost identical. The main difference between them is the name of the target and a path. Is it possible to create a target dynamically. I tried using macrodef in ant 1.6 but that did not work:
>  
> <project name="test" default="all" basedir=".">
> <macrodef name="echoTarget">
>    <attribute name="name" />
>    <sequential>
>       <target name="@{name}">
>          <echo>@{name} target executed.</echo>
>          <!-- the real work goes here. -->
>       </target>
>    </sequential>
> </macrodef>
> <echoTarget name="dynamic"/>
> <target name="all" depends="dynamic"/>
> </project>
> 
> Error:
> E:\builds\build.xml:11: Following error occured while executing this line
> E:\builds\build.xml:5: Could not create task or type of type: target.
>  
>>>From the command line, I would like to be able to do:
>    ant dynamic
>  
> The only way I can think of doing it is to run an xslt on the build file to generate all the targets. Is there another cleaner/simpler solution?
>  
> Thanks,
> Michael
>  
> 
Migrating several identical targets using macrodef means:

Targets have to be tasks.

Explanation:

old style:
<target name="foo1">
   <property name="path" value="bar1"/>
   <!-- doing something with property 'path' -->
</target>

<target name="foo2">
   <property name="path" value="bar2"/>
   <!-- doing the same as above -->
</target>

<target name="all" depends="foo1, foo2"/>

It is possible to remove code duplication using antcall. But antcall is 
slow. Since ant1.6 in most cases macrodef is the solution. But macrodef 
defines tasks, not targets.

<macrodef name="foo">
   <attribute name="path"/>
   <sequential>
     <!-- doing something with attribute 'path' -->
   </sequential>
</macrodef>

<target name="all">
   <foo path="bar1"/>
   <foo path="bar2"/>
</target>

Thinking about further nice features in ant1.6 macrodef is very powerful:

- all tasks can be executed by default placing them directly in project 
root (like it was possible for property before)
- macrodefs attributes can have default values (checking is done if no 
default is given)
- macrodefs can have an uri so using namespaces solves name conflicts
- the <element> element can place xml fragments in macrodefs - it's a 
little bit like aspect oriented programming (setting a cut point) - one 
use case are the inner elements of java task.
- import and antlib can make the build files modular and reusable


Claas
-- 
____________________________________________________________________
Dipl.-Inf. Claas Thiele          	EMail: cthiele@ct42.de
Konradstr. 58                    	Web:   http://ct42.de
04315 Leipzig                    	Tel.: +49 (0)341 68 70 92 29
GERMANY                          	Fax   +49 (0)341 68 70 92 30



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: is it possible to create targets dynamically?

Posted by Matt Benson <gu...@yahoo.com>.
--- Michael Sunde <ms...@actional.com> wrote:
> The only way I can think of doing it is to run an
> xslt on the build file to generate all the targets.
> Is there another cleaner/simpler solution?
>  

I am not clear on what exactly it is you are trying to
do, but chances are good that a dynamic solution
exists.

-Matt

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org