You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Andres Valenciano <av...@artinsoft.com> on 2002/08/30 23:28:14 UTC

Modifying properties on the fly in custom task

Hi.

I am writing a Task that has as a child an exec task. In my execute method I
am changing properties and calling the perform method on the exectask added,
this done 1 or more times before my execute finishes.

Now, the problem I am having is that the exectask is not getting the
"dynamically" created properties that I am using in the arg element.

For example:
<mytask name="MYTASK"  >
	<exec executable="notepad.exe"  >
		<arg line=" ${MYVAR}"/>
	</exec>
</mytask>

I am executing several times the exec task changing the MYVAR variable, BUT
the verbose output when running the script is:
	Executing 'notepad.exe' with arguments:
	'${MYVAR}'

Any idea about how to do it? Is the exec task building the arguments before
the perform method?


Thanks for your help!


A.V.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Modifying properties on the fly in custom task

Posted by Andres Valenciano <av...@artinsoft.com>.
Easy! I used the <antcall> approach inside my task, it works just fine.

Thanks for your help!

A.V.


-----Original Message-----
From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com]
Sent: Monday, September 02, 2002 1:05 PM
To: Ant Users List
Subject: Re: Modifying properties on the fly in custom task


Andres Valenciano wrote:
> My motivation is this, I want to "execute" one or more tasks (exec right
> now) but many times based on one or more defined fileset. The idea was
that
> this "foreach-file" (or whatever this task could be called) could generate
> an implicit (documented,etc...) property (like "${theloop.filename}") that
> changes in every iteration (again) according to each file obtained from
the
> evaluation of the fileset(s).

Makes sense.


> Now, I tried to find info about this with the already defined tasks and I
> found nothing, am I wrong?

Ant-contrib's <foreach> task is the closest one you'll find that does
this, that I know of.  And it does its work by doing an <antcall>, which
gets around the property immutability issue you're encountering.

Perhaps this is the right model to do what you want?

> I wrote a class extending Task with an addExec method and in my execute
> method I call execTask.perform(). The problem is I need to "control" the
> execution moment of the children tasks in order to change for each call
the
> defined "properties" or "environmental variables", whatever. I know I am
> making a mistake in this part: I have tried calling project.setProperty,
> project.setUserProperty, adding an Environment.Variable to the ExecTask
> instance, now is see why it is not working... (from your post!) "...you
are
> bypassing Ant's property expansion mechanism...".

Its not the property expansion issue that is getting you - its the
property immutability.  Once properties are set, their value does not
change (unless you get into an <antcall> situation where you can
override properties).


> So, what are my options here for implementing this kind of task? Or, am I
> trying to do something that should be done under a <script> element?

Either you implement your own property-like substitution on strings
you're handing to <exec>, or use the <foreach> way.  For maximum
flexibility, I'd recommend the <foreach> model, but if the tasks you
want to <exec> are pretty fixed, you could embed that information into
your Java task code directly.

	Erik


> -----Original Message-----
> From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com]
> Sent: Friday, August 30, 2002 6:15 PM
> To: Ant Users List
> Subject: Re: Modifying properties on the fly in custom task
>
>
> Since you are embedding the <exec> task within your class, you are
> bypassing Ant's property expansion mechanism.
>
> I actually referred back to my book for the answer (why memorize the
> details when you can look them up easily?! :).... p. 485:
>
> getProject().replaceProperties(someString)
>
> Use the results of that to pass to the arg. method.  But, I'm a bit
> confused on exactly how you're doing what you say you're doing.  How are
> you *changing* the value of MYVAR?  You're mutating immutable
> properties?!  (which can be done, but not necessarily a good thing to do).
>
> Is <mytask> a TaskContainer?  Or are you reimplementing the <exec>
> interface yourself?  There may be better ways to accomplish what you're
> after - elaborate a bit and lets work through it.
>
> 	Erik
>
>
> Andres Valenciano wrote:
>
>>Hi.
>>
>>I am writing a Task that has as a child an exec task. In my execute method
>
> I
>
>>am changing properties and calling the perform method on the exectask
>
> added,
>
>>this done 1 or more times before my execute finishes.
>>
>>Now, the problem I am having is that the exectask is not getting the
>>"dynamically" created properties that I am using in the arg element.
>>
>>For example:
>><mytask name="MYTASK"  >
>>	<exec executable="notepad.exe"  >
>>		<arg line=" ${MYVAR}"/>
>>	</exec>
>></mytask>
>>
>>I am executing several times the exec task changing the MYVAR variable,
>
> BUT
>
>>the verbose output when running the script is:
>>	Executing 'notepad.exe' with arguments:
>>	'${MYVAR}'
>>
>>Any idea about how to do it? Is the exec task building the arguments
>
> before
>
>>the perform method?
>>
>>
>>Thanks for your help!
>>
>>
>>A.V.
>>
>>
>>--
>>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>>
>>
>>
>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Modifying properties on the fly in custom task

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Andres Valenciano wrote:
> My motivation is this, I want to "execute" one or more tasks (exec right
> now) but many times based on one or more defined fileset. The idea was that
> this "foreach-file" (or whatever this task could be called) could generate
> an implicit (documented,etc...) property (like "${theloop.filename}") that
> changes in every iteration (again) according to each file obtained from the
> evaluation of the fileset(s).

Makes sense.


> Now, I tried to find info about this with the already defined tasks and I
> found nothing, am I wrong?

Ant-contrib's <foreach> task is the closest one you'll find that does 
this, that I know of.  And it does its work by doing an <antcall>, which 
gets around the property immutability issue you're encountering.

Perhaps this is the right model to do what you want?

> I wrote a class extending Task with an addExec method and in my execute
> method I call execTask.perform(). The problem is I need to "control" the
> execution moment of the children tasks in order to change for each call the
> defined "properties" or "environmental variables", whatever. I know I am
> making a mistake in this part: I have tried calling project.setProperty,
> project.setUserProperty, adding an Environment.Variable to the ExecTask
> instance, now is see why it is not working... (from your post!) "...you are
> bypassing Ant's property expansion mechanism...".

Its not the property expansion issue that is getting you - its the 
property immutability.  Once properties are set, their value does not 
change (unless you get into an <antcall> situation where you can 
override properties).


> So, what are my options here for implementing this kind of task? Or, am I
> trying to do something that should be done under a <script> element?

Either you implement your own property-like substitution on strings 
you're handing to <exec>, or use the <foreach> way.  For maximum 
flexibility, I'd recommend the <foreach> model, but if the tasks you 
want to <exec> are pretty fixed, you could embed that information into 
your Java task code directly.

	Erik


> -----Original Message-----
> From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com]
> Sent: Friday, August 30, 2002 6:15 PM
> To: Ant Users List
> Subject: Re: Modifying properties on the fly in custom task
> 
> 
> Since you are embedding the <exec> task within your class, you are
> bypassing Ant's property expansion mechanism.
> 
> I actually referred back to my book for the answer (why memorize the
> details when you can look them up easily?! :).... p. 485:
> 
> getProject().replaceProperties(someString)
> 
> Use the results of that to pass to the arg. method.  But, I'm a bit
> confused on exactly how you're doing what you say you're doing.  How are
> you *changing* the value of MYVAR?  You're mutating immutable
> properties?!  (which can be done, but not necessarily a good thing to do).
> 
> Is <mytask> a TaskContainer?  Or are you reimplementing the <exec>
> interface yourself?  There may be better ways to accomplish what you're
> after - elaborate a bit and lets work through it.
> 
> 	Erik
> 
> 
> Andres Valenciano wrote:
> 
>>Hi.
>>
>>I am writing a Task that has as a child an exec task. In my execute method
> 
> I
> 
>>am changing properties and calling the perform method on the exectask
> 
> added,
> 
>>this done 1 or more times before my execute finishes.
>>
>>Now, the problem I am having is that the exectask is not getting the
>>"dynamically" created properties that I am using in the arg element.
>>
>>For example:
>><mytask name="MYTASK"  >
>>	<exec executable="notepad.exe"  >
>>		<arg line=" ${MYVAR}"/>
>>	</exec>
>></mytask>
>>
>>I am executing several times the exec task changing the MYVAR variable,
> 
> BUT
> 
>>the verbose output when running the script is:
>>	Executing 'notepad.exe' with arguments:
>>	'${MYVAR}'
>>
>>Any idea about how to do it? Is the exec task building the arguments
> 
> before
> 
>>the perform method?
>>
>>
>>Thanks for your help!
>>
>>
>>A.V.
>>
>>
>>--
>>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>>
>>
>>
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Modifying properties on the fly in custom task

Posted by Andres Valenciano <av...@artinsoft.com>.
MM... ok.. I will try to clarify this... (BTW: thx for the reply) and sorry
if I am making a silly mistake or wrong assumptions (I'm sure I am...), I
can't say I am an Ant expert...  ;-)

My motivation is this, I want to "execute" one or more tasks (exec right
now) but many times based on one or more defined fileset. The idea was that
this "foreach-file" (or whatever this task could be called) could generate
an implicit (documented,etc...) property (like "${theloop.filename}") that
changes in every iteration (again) according to each file obtained from the
evaluation of the fileset(s).

Now, I tried to find info about this with the already defined tasks and I
found nothing, am I wrong?

I wrote a class extending Task with an addExec method and in my execute
method I call execTask.perform(). The problem is I need to "control" the
execution moment of the children tasks in order to change for each call the
defined "properties" or "environmental variables", whatever. I know I am
making a mistake in this part: I have tried calling project.setProperty,
project.setUserProperty, adding an Environment.Variable to the ExecTask
instance, now is see why it is not working... (from your post!) "...you are
bypassing Ant's property expansion mechanism...".

So, what are my options here for implementing this kind of task? Or, am I
trying to do something that should be done under a <script> element?


Thanks for your help.

A.V.




-----Original Message-----
From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com]
Sent: Friday, August 30, 2002 6:15 PM
To: Ant Users List
Subject: Re: Modifying properties on the fly in custom task


Since you are embedding the <exec> task within your class, you are
bypassing Ant's property expansion mechanism.

I actually referred back to my book for the answer (why memorize the
details when you can look them up easily?! :).... p. 485:

getProject().replaceProperties(someString)

Use the results of that to pass to the arg. method.  But, I'm a bit
confused on exactly how you're doing what you say you're doing.  How are
you *changing* the value of MYVAR?  You're mutating immutable
properties?!  (which can be done, but not necessarily a good thing to do).

Is <mytask> a TaskContainer?  Or are you reimplementing the <exec>
interface yourself?  There may be better ways to accomplish what you're
after - elaborate a bit and lets work through it.

	Erik


Andres Valenciano wrote:
> Hi.
>
> I am writing a Task that has as a child an exec task. In my execute method
I
> am changing properties and calling the perform method on the exectask
added,
> this done 1 or more times before my execute finishes.
>
> Now, the problem I am having is that the exectask is not getting the
> "dynamically" created properties that I am using in the arg element.
>
> For example:
> <mytask name="MYTASK"  >
> 	<exec executable="notepad.exe"  >
> 		<arg line=" ${MYVAR}"/>
> 	</exec>
> </mytask>
>
> I am executing several times the exec task changing the MYVAR variable,
BUT
> the verbose output when running the script is:
> 	Executing 'notepad.exe' with arguments:
> 	'${MYVAR}'
>
> Any idea about how to do it? Is the exec task building the arguments
before
> the perform method?
>
>
> Thanks for your help!
>
>
> A.V.
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Modifying properties on the fly in custom task

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Since you are embedding the <exec> task within your class, you are 
bypassing Ant's property expansion mechanism.

I actually referred back to my book for the answer (why memorize the 
details when you can look them up easily?! :).... p. 485:

getProject().replaceProperties(someString)

Use the results of that to pass to the arg. method.  But, I'm a bit 
confused on exactly how you're doing what you say you're doing.  How are 
you *changing* the value of MYVAR?  You're mutating immutable 
properties?!  (which can be done, but not necessarily a good thing to do).

Is <mytask> a TaskContainer?  Or are you reimplementing the <exec> 
interface yourself?  There may be better ways to accomplish what you're 
after - elaborate a bit and lets work through it.

	Erik


Andres Valenciano wrote:
> Hi.
> 
> I am writing a Task that has as a child an exec task. In my execute method I
> am changing properties and calling the perform method on the exectask added,
> this done 1 or more times before my execute finishes.
> 
> Now, the problem I am having is that the exectask is not getting the
> "dynamically" created properties that I am using in the arg element.
> 
> For example:
> <mytask name="MYTASK"  >
> 	<exec executable="notepad.exe"  >
> 		<arg line=" ${MYVAR}"/>
> 	</exec>
> </mytask>
> 
> I am executing several times the exec task changing the MYVAR variable, BUT
> the verbose output when running the script is:
> 	Executing 'notepad.exe' with arguments:
> 	'${MYVAR}'
> 
> Any idea about how to do it? Is the exec task building the arguments before
> the perform method?
> 
> 
> Thanks for your help!
> 
> 
> A.V.
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>