ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cyril Sagan <Cyril.Sa...@sas.com>
Subject RE: Having problem using property to in macrodef if invoke for more than one time
Date Thu, 27 Mar 2008 20:14:52 GMT
You could also use beanshell.  Not sure if this works due to a bug or feature.
We use it very sparingly in our build process (analagous to like goto in C).

<?xml version="1.0" encoding="utf-8"?>

<project name="ant_dev_problem" default="build">
    <scriptdef name="beanSetProperty" language="beanshell">
        <!-- setProperty appears *not* to respect Ant's property,
             immutability.  Appears to be a change in 1.7.0.
             Note that setNewProperty *does* respect immutability -->
        <attribute name="name"/>
        <attribute name="value"/>
        <![CDATA[
                 String n = attributes.get("name");
                 String v = attributes.get("value");
                 project.setProperty(n, v);
        ]]>
    </scriptdef>


    <macrodef name="a">
        <attribute name="module"/>
        <sequential>
            <beanSetProperty name="myvalue" value="@{module}" />
            <echo>inside macro: ${myvalue}</echo>
        </sequential>
    </macrodef>

    <target name="build">
        <a module="venus"/>
        <echo>outside macro: ${myvalue}</echo>
        <a module="mars"/>
        <echo>outside macro: ${myvalue}</echo>
    </target>
</project>

$ ant -q
     [echo] inside macro: venus
     [echo] outside macro: venus
     [echo] inside macro: mars
     [echo] outside macro: mars

BUILD SUCCESSFUL
Total time: 0 seconds

Hope this helps.

--Cyril


-----Original Message-----
From: CheeYang Chau [mailto:cychau@gmail.com]
Sent: Thursday, March 27, 2008 1:51 AM
To: user@ant.apache.org
Subject: Having problem using property to in macrodef if invoke for more than one time

Hi,

I love the macrodef task. However, it has a weakness.  We must be very
careful if using property within the macrodef.  I face problem with a
macrodef that is more complicated than the following example.  I have
to use dirname and condition in my macrodef, but it cause problem if I
invoke macrodef for more than one time as the property has been set
for the first time.  Here is an example that trying to replay the
situation I face:

        <macrodef name="a">
                <attribute name="module"/>
                <sequential>
                        <property name="myvalue" value="@{module}"/>
                        <echo>${myvalue}</echo>
                </sequential>
        </macrodef>

        <target name="build">
                <a module="venus"/>
                <a module="mars"/>
        </target>

I expect the output should be

[echo] venus
[echo] mars

but it turn out:

[echo] venus
[echo] venus

I think this is a common issue using macrodef and there are some
temporary solution to pass a temporary property name to the macrodef.
This is not feasible in my situation as I need to pass more than 5 to
10 properties to my macrodef.

Some other solution is using local property but I couldn't find this
task.  I am using ANT 1.7.0.

Any solution?  Please advice.  Thank you very much.

--
Best regards,
Chau Chee Yang

E Stream Software Sdn Bhd
URL: www.sql.com.my
SQL Financial Accounting

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


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


Mime
View raw message