Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant string functions?

Tags:

java

string

ant

Does Ant have any way of doing string uppercase/lowercase/captialize/uncaptialize string manipulations? I looked at PropertyRegex but I don't believe the last two are possible with that. Is that anything else?

like image 631
Mike Q Avatar asked Sep 16 '10 10:09

Mike Q


1 Answers

From this thread, use an Ant <script> task:

<target name="capitalize">
    <property name="foo" value="This is a normal line that doesn't say much"/>

    <!-- Using Javascript functions to convert the string -->
    <script language="javascript"> <![CDATA[

        // getting the value
        sentence = project.getProperty("foo");

        // convert to uppercase
        lowercaseValue = sentence.toLowerCase();
        uppercaseValue = sentence.toUpperCase();

        // store the result in a new property
        project.setProperty("allLowerCase",lowercaseValue);
        project.setProperty("allUpperCase",uppercaseValue);

    ]]> </script>

    <!-- Display the values -->
    <echo>allLowerCase=${allLowerCase}</echo>
    <echo>allUpperCase=${allUpperCase}</echo>
</target>

Output

D:\ant-1.8.0RC1\bin>ant capitalize
Buildfile: D:\ant-1.8.0RC1\bin\build.xml

capitalize:
     [echo] allLowerCase=this is a normal line that doesn't say much
     [echo] allUpperCase=THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH

BUILD SUCCESSFUL

Update for WarrenFaith's comment to separate the script into another target and pass a property from the called target back to the calling target

Use antcallback from the ant-contrib jar

<target name="testCallback">
    <antcallback target="capitalize" return="allUpperCase">
        <param name="param1" value="This is a normal line that doesn't say much"/>
    </antcallback>
    <echo>a = ${allUpperCase}</echo>
</target>

and capitalise task uses the passed in param1 thus

 <target name="capitalize">

        <property name="foo" value="${param1}"/>

Final output

   [echo] a = THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH
like image 81
JoseK Avatar answered Sep 28 '22 05:09

JoseK