Ant string functions?

前端 未结 2 373
礼貌的吻别
礼貌的吻别 2020-12-15 08:46

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 poss

相关标签:
2条回答
  • 2020-12-15 09:41

    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
    
    0 讨论(0)
  • 2020-12-15 09:46

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

    0 讨论(0)
提交回复
热议问题