Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant antcall a target that defines a property

Tags:

In Ant I want to define a target (called A) that define a property and antcall it from another target (called B). I want that the target B, after antcalling the target A, can access the property defined in the target A.

For example:

<target name="B">
    <antcall target="A" inheritAll="true" inheritRefs="true" />
    <echo>${myprop}</echo>
</target>
<target name="A">
    <property name="myprop" value="myvalue" />
</target>

However it doesn't work and <echo>${myprop}</echo> doesn't print myvalue (I think because the property myprop isn't defined in B).

Is there any way to do that?

like image 929
alem0lars Avatar asked Mar 11 '11 16:03

alem0lars


People also ask

What is Antcall target?

When a target is invoked by antcall , all of its dependent targets will also be called within the context of any new parameters. For example. if the target doSomethingElse ; depended on the target init , then the antcall of doSomethingElse will call init during the call.

What are Ant properties?

Properties are key-value pairs where each value is associated to a key. The property is used to set value which can be accessed anywhere in the buildfile. Once a property is set, it cannot be changed. Apache Ant provides <property> tag which can be used to set property.

How do I run a specific target in Ant?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

How do I override property value in Ant?

Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.


2 Answers

According to the Apache Ant FAQ:

    <target name="cond" depends="cond-if"/>

    <target name="cond-if" if="prop1">
      <antcall target="cond-if-2"/>
    </target>

    <target name="cond-if-2" if="prop2">
      <antcall target="cond-if-3"/>
    </target>

    <target name="cond-if-3" unless="prop3">
      <echo message="yes"/>
    </target>

Note: <antcall> tasks do not pass property changes back up to the environment they were called from, so you wouldn't be able to, for example, set a result property in the cond-if-3 target, then do <echo message="result is ${result}"/> in the cond target.

In this respect, it is impossible to do what you want using antcall.

========== edit ===========

Try antcallback: AntCallBack is identical to the standard 'antcall' task, except that it allows properties set in the called target to be available in the calling target.
http://antelope.tigris.org/nonav/docs/manual/bk03ch20.html

Sample code pasted from the above page:

    <target name="testCallback" description="Test CallBack">
        <taskdef name="antcallback" classname="ise.antelope.tasks.AntCallBack" classpath="${antelope.home}/build" />
        <antcallback target="-testcb" return="a, b"/>
        <echo>a = ${a}</echo>
        <echo>b = ${b}</echo>
    </target>

    <target name="-testcb">
        <property name="a" value="A"/>
        <property name="b" value="B"/>
    </target>
like image 52
SOUser Avatar answered Sep 28 '22 21:09

SOUser


Another approach is to refactor your targets into macros. You are trying to use targets like functions and they are just not intended to be used that way. I typically write the bulk of my logic as macros, so that I can compose it more easily into more complicated macros. Then I write simple wrapper targets for the command-line entry points that I need.

like image 21
Konstantin Komissarchik Avatar answered Sep 28 '22 23:09

Konstantin Komissarchik