Bug 47035 - Property expansion in macro attributes and elements
Summary: Property expansion in macro attributes and elements
Status: REOPENED
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.7.1
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-15 05:20 UTC by Juhos Csaba-Zsolt
Modified: 2009-04-16 20:56 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Juhos Csaba-Zsolt 2009-04-15 05:20:37 UTC
Hi,

I'm using Launch4j to wrap a JAR into a windows executable:

<launch4j>
    <config>
        <jre>
            <opt>-Xms${java.initial.memory}m</opt>
        </jre>
    </config>
</launch4j>

The problem is that the "opt" element doesn't expand properties.
It seems that the developers didn't take care to expand them explicitly.
(http://ant.apache.org/ant_task_guidelines.html)

I wrapped the task into a macro like this:

<macrodef name="create-exe">
    <attribute name="java-initial-memory"/>
    <sequential>
        <launch4j>
            <config>
                <jre>
                    <opt>-Xms@{java.initial.memory}m</opt>
                </jre>
            </config>
        </launch4j>
    </sequential>
</macrodef>

And called it like:

<create-exe java-initial-memory="${java.initial.memory}"/>

in hope that the property will be expanded before being passed to the template.
It wasn't :(



I know that this is the expected behavior. What I'm asking for here is an option to expand macro arguments before passing them to the template.



After some consideration I came to the conclusion that the best solution would be to add an optional attribute "expand" to the "element" and "attribute" child elements of macrodef.

Modified example:

<macrodef name="create-exe">
    <attribute name="java-initial-memory" expand="true"/>
    <attribute name="java-maximum-memory" expand="true"/>
    <element name="vm-options" optional="true" expand="true"/>
    <sequential>
        <launch4j>
            <config>
                <jre>
                    <opt>-Xms@{java.initial.memory}m</opt>
                </jre>
            </config>
        </launch4j>
    </sequential>
</macrodef>

<create-exe java-initial-memory="${java.initial.memory}"
            java-maximum-memory="${java.maximum.memory}">

    <vm-options>
        <opt>${another.vm.option}</opt>
    </vm-options>
</create-exe>

"expand" should be a boolean variable with a default value of "false". This wouldn't break any existing code.

Another use case I'm seeing is the following:

<macrodef name="create-exe">
    <attribute name="expand-options" default="true"/>
    <attribute name="java-initial-memory" expand="@{expand-options}"/>
    <attribute name="java-maximum-memory" expand="@{expand-options}"/>
    <element name="vm-options" optional="true" expand="@{expand-options}"/>
...

This would make the macro more flexible.



In case this isn't possible to implement, I'm sorry for my ignorance, I'm not familiar with Ant's internals at all.

Thank you,
Csabi

P.S.: I filed a bug against Launch4j:

    https://sourceforge.net/tracker/?func=detail&aid=2764888&group_id=95944&atid=613100
Comment 1 Matt Benson 2009-04-16 08:04:08 UTC
Uh... Red herring alert:  I think the reason your attribute is not expanded is because you're not using it:

<macrodef name="create-exe">
    <attribute name="java-initial-memory"/>
    <sequential>
        <launch4j>
            <config>
                <jre>
                    <opt>-Xms@{java.initial.memory}m</opt>
                </jre>
            </config>
        </launch4j>
    </sequential>
</macrodef>

This should be:

<macrodef name="create-exe">
    <attribute name="java-initial-memory"/>
    <sequential>
        <launch4j>
            <config>
                <jre>
                    <opt>-Xms@{java-initial-memory}m</opt>
                </jre>
            </config>
        </launch4j>
    </sequential>
</macrodef>
Comment 2 Juhos Csaba-Zsolt 2009-04-16 20:56:20 UTC
Hi,

It's a typo. In my real code I have used the attribute correctly.

What happens is that "@{java-initial-memory}" gets expanded to "${java.initial.memory}", which doesn't get expanded by the "opt" element, because it doesn't expand properties in its text.

What I'm asking for is an option for the expansion of "${java.initial.memory}" when calling the macro, like this:

<property name="java.initial.memory" value="128"/>
<create-exe java-initial-memory="${java.initial.memory}"/>
...
@{java-initial-memory} should expand to "-Xms128m"

Cheers,
Csabi