Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

java - Ant string functions?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.0RC1in>ant capitalize
Buildfile: D:ant-1.8.0RC1inuild.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...