12

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?

Mike Q
  • 22,839
  • 20
  • 87
  • 129

2 Answers2

14

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
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
JoseK
  • 31,141
  • 14
  • 104
  • 131
  • Though it is easier with Java 6, it works with Java 5, but you have to provide an implementation of your scripting language, e.g., Rhino for Javascript. – Richard Steele May 10 '11 at 13:57
  • @JoseK: Do you know how I can separate the script in a target? When I do that, the property changed in the target, but afterwards the old value is restored. My problem is, that I want to make the main target as small as possible so that even non-programmer are able to change properties. Thanks – WarrenFaith Aug 10 '11 at 14:34
  • @Warren: I tried using `antcall` to call the separated script. but properties set by the called targets will not persist back to the calling project, as per the docs. How have you tried? – JoseK Aug 10 '11 at 17:11
  • @JoseK: Exactly the same way like you. Just `antcall` and the target, which was defined in the same xml. Thought that `project` means for the complete ant run, but it seems thats not the case. If you stumble upon a solution, please share :) Thanks! – WarrenFaith Aug 10 '11 at 17:25
  • @JoseK: Great! Works perfect! – WarrenFaith Aug 11 '11 at 11:19
  • You seem to have misunderstood what capitalize means - I'd expect that to only make the first character upper case. – Vala Dec 06 '11 at 16:58
  • javascript has been removed from jdk15+, any other ways? – martian Jan 18 '22 at 02:08
0

you could use the script task and use a jsr223-supported script language like javascript, jruby, jython,... to do your string handling

Nikolaus Gradwohl
  • 19,708
  • 3
  • 45
  • 61