tags:

views:

990

answers:

5

I have a main build script that calls various targets. One of these targets needs to store a value and another target needs to display it. Obviously this is not working so I think it may be related to scope. I've tried var, property, and declaring the property outside of target1. Since var seems to be mutable, it looks like I need to use it instead, but each time my output is empty.

Main script

<antcall target="target1"/>
<antcall target="display"/>

In target1:

<var name="myVar" value="${anotherVar}"/>

In display:

<echo>${myVar}</echo>
+1  A: 

antcall will start the ant target in a new project and will not affect the main project in any way. Try runtarget from antcontrib to run the targets in the same project.

Rob Goodwin
+1: Thank you for this hint. With `runtarget` I was able to clean up my build files easlily.
tangens
A: 

You can call multiple targets with one antcall element. These targets will then share a single project instance including the properties defined. To do this specify the targets as nested elements like this:

<antcall>
  <target name="target1"/>
  <target name="display"/>
</antcall>
Geoff Reedy
Interesting idea. I have other target calls in-between these two. I could add all of them in there, but then I would have the same scope/security problem if I change all of them to runtarget. But either way its a trade-off.
bobtheowl2
This is only supported since Ant 1.6.3, otherwise you'll see: "The <antcall> task doesn't support the nested "target" element."
nbolton
+1  A: 

Another option I found was the antcallback, and it appears to work. This limits what is returned to just a particular list of values, which seems inherently safer than opening up the scope of the whole target (as it sets, creates, modifies many var and properties).

<antcallback target="target1" return="myVar"/>
<antcall target="display"/>

I think all of these are valid solutions, it just depends on what level you want to change the variable scope at.

bobtheowl2
A: 

Do you really need to use <antcall>? Can you use target dependencies instead?

As you suspect, using <antcall> essentially creates a new scope.

We have a dependency mess right now and were trying to componentize our shared targets to remove any inner dependencies and do some cleanup while were there.
bobtheowl2
Often <antcall> is used because of nested <param> tags.
nbolton
A: 

Using antcallback within the groovy task won't work as they have used a java reserved word (return) as the name of one of the attributes!

DFH
This is not groovy
bobtheowl2