You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Wolfgang Häfelinger <wh...@epo.org> on 2006/06/06 15:02:26 UTC

how to call macros dynamically?

Hi,

I wonder how  to  implement a task (in Java) allowing me to execute
a macro. I tried to understand how it works looking into Ant's source
code, but I'm bit lost. Would be kind if someone could give me a
"roadmap" how to do it ..

What I'm looking for is something like ..

Assume I have a macro:

<macrodef name="x">
  <attribute name="a" default="hello" />
  ..
 </macrodef>

Then I want to be able to say

   <run-macro name="x" />

instead of 

 <x/>

For the beginning I'm happy to call a macro without any parameters like
shown. Even better would be to be able to provide parameters as well:

 <run-macro name="x" a="hello, world" />    <!-- utopia -->

 <run-macro name="x">
   <param name="a" value="hello, world" />
 </run-macro>

Any hints?

Cheers,
Wolfgang.


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


Re: how to call macros dynamically?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
>> For example, the above could be replaced by a single empty-target
>> depending on the 4 possible package-* targets, each with an
>> appropriate 'if' attribute. Or with a <antcall> (limited use of
>> <antcall> is usually not that expensive).

Sure you agree that "run-macro" is by far more elegant than working with 
such a target?

Also, I do not need to care about this hidden conditional if I'm going to 
extend the list
of known project types.

>> Or with a <antcall> (limited use of <antcall> is usually not that 
>> expensive).

Hmm, I thought that there's no need for antcall any longer - now as
we have macros.

>> That's because you think of them in procedural term. 

Still I can't see the general problem treating a target like a
special kind of macro .. (target = public macro + dependencies).


>> That said, feel free to implement a <macrocall> or <runmacro> 
>> task ;-)

I'm stuck in NullPointerExceptions :-)



 






"Dominique Devienne" <dd...@gmail.com> 
06-06-2006 16:55
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to call macros dynamically?






>  <switch value="${project.type}">
>  <case value="jar">
>    <package-jar />
>  </case>
>  ..
>  <case value="ear">
>    <package-ear />
>  </case>
>  </switch>
>
> Having a  "run-macro" around I could shorten things to simply write
>
>  <run-macro name="package-${project.type}" />

Indeed, that's what I had in mind by hidden conditional.

That's one way to do it, but probably not one of the Ant ways.

For example, the above could be replaced by a single empty-target
depending on the 4 possible package-* targets, each with an
appropriate 'if' attribute. Or with a <antcall> (limited use of
<antcall> is usually not that expensive).

I so think you are working a bit "against the grain" with Ant here ;-)

> Another thing that strikes me is the distinction between a target and a
> task and macrodef.
> It's rather easy to implement a task executing an arbitrary target using
> getProject().executeTarget().

Sure, that's the safe <antcall> in Ant, or the less safe <runtarget>
in Ant-Contrib.

> Why not providing the same functionality for a macro as well? Honestly, 
I
> regard a target
> nothing more than a "public" macro, i.e. a macro able to call from the
> command line.

That's because you think of them in procedural term. When a target as
dependencies, it's not the same ball game. In theory, the dependencies
of a target should be only the pre-requisite of the target, and not an
ordered list of steps to fun before, and then the Ant engine would be
allowed to order the execution of theses target anyway it likes
(possibly parallelizing them) as long the the pre-requisites are met.
There's even an Executor proposal to behave that way.

Sure, it's not the way Ant works, or will ever work in all likelyhood,
but that's way I think it's wrong to think of targets as "public"
macros.

It's similar to how an XSLT engine processes it's templates. Most
processors are still mostly procedural right now, but the design
allows the engine to work in a non-procedural way (Read Dr. Kay's
excellent books for more details).

That said, feel free to implement a <macrocall> or <runmacro> task ;-)

--DD

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




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


Re: how to call macros dynamically?

Posted by Dominique Devienne <dd...@gmail.com>.
>  <switch value="${project.type}">
>  <case value="jar">
>    <package-jar />
>  </case>
>  ..
>  <case value="ear">
>    <package-ear />
>  </case>
>  </switch>
>
> Having a  "run-macro" around I could shorten things to simply write
>
>  <run-macro name="package-${project.type}" />

Indeed, that's what I had in mind by hidden conditional.

That's one way to do it, but probably not one of the Ant ways.

For example, the above could be replaced by a single empty-target
depending on the 4 possible package-* targets, each with an
appropriate 'if' attribute. Or with a <antcall> (limited use of
<antcall> is usually not that expensive).

I so think you are working a bit "against the grain" with Ant here ;-)

> Another thing that strikes me is the distinction between a target and a
> task and macrodef.
> It's rather easy to implement a task executing an arbitrary target using
> getProject().executeTarget().

Sure, that's the safe <antcall> in Ant, or the less safe <runtarget>
in Ant-Contrib.

> Why not providing the same functionality for a macro as well? Honestly, I
> regard a target
> nothing more than a "public" macro, i.e. a macro able to call from the
> command line.

That's because you think of them in procedural term. When a target as
dependencies, it's not the same ball game. In theory, the dependencies
of a target should be only the pre-requisite of the target, and not an
ordered list of steps to fun before, and then the Ant engine would be
allowed to order the execution of theses target anyway it likes
(possibly parallelizing them) as long the the pre-requisites are met.
There's even an Executor proposal to behave that way.

Sure, it's not the way Ant works, or will ever work in all likelyhood,
but that's way I think it's wrong to think of targets as "public"
macros.

It's similar to how an XSLT engine processes it's templates. Most
processors are still mostly procedural right now, but the design
allows the engine to work in a non-procedural way (Read Dr. Kay's
excellent books for more details).

That said, feel free to implement a <macrocall> or <runmacro> task ;-)

--DD

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


Re: how to call macros dynamically?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
> Is your motivation being able to have conditionals in disguise, i.e.
> be able to write

I believe that my deeper motivation is to be able to do "scripting" 
without falling back
to Javascript, Python et cetera. I'm not using them because they are not 
part of a
standard Ant installation.

Here's my motivation for having "run-macro" - it would simplify my Ant 
scripts 
considerably. On loading my build script, a macro gets executed which 
guesses the 
project's type. The result of this guess is property '"project.type" which 
(currently) 
holds on of the values 

 jar
 jsf
 war
 ear

Further target "package" exists which calls a specific macro depending on 
project.type's value.
So my package macro contains something like

 <switch value="${project.type}">
  <case value="jar">
    <package-jar />
  </case>
  ..
  <case value="ear">
    <package-ear />
 </case>
 </switch>

Having a  "run-macro" around I could shorten things to simply write 

  <run-macro name="package-${project.type}" />

Another thing that strikes me is the distinction between a target and a 
task and macrodef.
It's rather easy to implement a task executing an arbitrary target using
getProject().executeTarget().

Why not providing the same functionality for a macro as well? Honestly, I 
regard a target 
nothing more than a "public" macro, i.e. a macro able to call from the 
command line.







"Dominique Devienne" <dd...@gmail.com> 
06-06-2006 15:18
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to call macros dynamically?






Is your motivation being able to have conditionals in disguise, i.e.
be able to write

<run-macro name="${do-this}" />

? Otherwise I can't see the use for this. Just curious ;-) Thanks, --DD

On 6/6/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> Hi,
>
> I wonder how  to  implement a task (in Java) allowing me to execute
> a macro. I tried to understand how it works looking into Ant's source
> code, but I'm bit lost. Would be kind if someone could give me a
> "roadmap" how to do it ..
>
> What I'm looking for is something like ..
>
> Assume I have a macro:
>
> <macrodef name="x">
>  <attribute name="a" default="hello" />
>  ..
>  </macrodef>
>
> Then I want to be able to say
>
>   <run-macro name="x" />
>
> instead of
>
>  <x/>
>
> For the beginning I'm happy to call a macro without any parameters like
> shown. Even better would be to be able to provide parameters as well:
>
>  <run-macro name="x" a="hello, world" />    <!-- utopia -->
>
>  <run-macro name="x">
>   <param name="a" value="hello, world" />
>  </run-macro>
>
> Any hints?
>
> Cheers,
> Wolfgang.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

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




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


Re: how to call macros dynamically?

Posted by Dominique Devienne <dd...@gmail.com>.
Is your motivation being able to have conditionals in disguise, i.e.
be able to write

<run-macro name="${do-this}" />

? Otherwise I can't see the use for this. Just curious ;-) Thanks, --DD

On 6/6/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> Hi,
>
> I wonder how  to  implement a task (in Java) allowing me to execute
> a macro. I tried to understand how it works looking into Ant's source
> code, but I'm bit lost. Would be kind if someone could give me a
> "roadmap" how to do it ..
>
> What I'm looking for is something like ..
>
> Assume I have a macro:
>
> <macrodef name="x">
>  <attribute name="a" default="hello" />
>  ..
>  </macrodef>
>
> Then I want to be able to say
>
>   <run-macro name="x" />
>
> instead of
>
>  <x/>
>
> For the beginning I'm happy to call a macro without any parameters like
> shown. Even better would be to be able to provide parameters as well:
>
>  <run-macro name="x" a="hello, world" />    <!-- utopia -->
>
>  <run-macro name="x">
>   <param name="a" value="hello, world" />
>  </run-macro>
>
> Any hints?
>
> Cheers,
> Wolfgang.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

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