String Casing in Ant

One of the various build tools we use is trusty, reliable Apache Ant. In order to take some of the regex load off our Apache web server, we decided to deploy versions of our javascript files in various cases (uppercase, lowercase, etc.) in addition to our standard mixed case. Normally this type of thing isn’t a problem, since our JS is only called from within our code, but somehow, browsers are messing with String case. Annoying.

Unfortunately, amongst the numerous tasks provided within Ant and contrib functions, there isn’t one for String manipulation. After a bit of Googling, the best I found was in a blog post by Peter Thomas which suggested writing a small Java class, that was compiled during the Ant build, and then used to read in a value and write the output to a temp file, which could then be set into a property. It inspired me, but I wanted to avoid the temp file. The main thinking I liked was trying to avoid manipulating the Ant install environment for simple string manipulation.

Before starting down this path, I considered alternate approaches, that were mostly RegEx based, but I couldn’t find a way to move forward without adding in contrib tasks.

Finally, I worked up the following Java class that was a bit more flexible, able to do lowercase, uppercase, and true titlecase (Peter’s implementation only title cased one word). And most importantly, it avoids the temp file.

* Simple class that takes an input value, converts the letter case and writes
* the result to standard out. The intention is for this class to be used within
* ant.
* Takes two parameters, the first is a command:
* <ul>
* <li>uc - Converts the remaining arguments to all uppercase</li>
* <li>lc - Converts the remaining arguments to all lowercase</li>
* <li>tc - Converts the remaining arguments to title case</li>
* </ul>
* Example conversion to title case:
* Usage: java StringCase -tc here IS some String to case
* Output: Here Is Some String To Case
* If the usage is incorrect, the error will be written to standard error.
* To use this within Ant, invoke as:
* <pre>
* <java classpath="contrib" classname="StringCase" outputproperty="">
* <arg value="-uc" />
* <arg value="string to uppercae" />
* </java>
* </pre>
* Inspired from:
* @author <a href="">Lon F. Binder</a>
* $Id$
public class StringCase {

/* —– CONSTANTS —– */

private final static String UPPERCASE = “-uc”;
private final static String LOWERCASE = “-lc”;
private final static String TITLECASE = “-tc”;

/* —– METHODS —– */

public static void main(String[] args) throws Exception {
if (args==null || args.length==0) {
System.err.println(”Incorrect usage, no arguments provided.”);

String command = args[0].toLowerCase();

if (!(UPPERCASE.equals(command) || LOWERCASE.equals(command) ||
TITLECASE.equals(command))) {
“Incorrect usage, unrecognized command argument provided: ” +

if (args.length == 1) {
System.err.println(”Incorrect usage, no text provided.”);

String word = “”;
for (int i=1; i<args.length; ++i) {
if (UPPERCASE.equals(command)) {
word = args[i].toUpperCase();
} else if (LOWERCASE.equals(command)) {
word = args[i].toLowerCase();
} else if (TITLECASE.equals(command)) {
word = args[i].substring(0,1).toUpperCase();
if (args[i].length() > 1) {
word += args[i].substring(1).toLowerCase();

if (i>1) {
System.out.print(’ ‘);

} // Everyone likes the end of class

I dropped this Java file into a contrib directory within my Ant project. Then I modified my build.xml file, at the top, adding this one line:

<javac srcdir="contrib" includes="" />

Then wherever I needed a manipulated property, I use this command:

<java classpath="contrib" classname="StringCase" outputproperty="dist.file.initializer.uc">
<arg value="-uc" />
<arg value="${dist.file.initializer}" />

In this case the outputproperty will be a new Ant property (unfortunately, immutable as usual), that is cased as indicated by the first arg (”-uc” or uppercase). Works perfectly.

You can leave a response, or trackback from your own site.

  1. Peter Thomas says:

    Great you found the hack useful! I did not know about “outputproperty”, thanks - it certainly helps to avoid the temp file. Does it work if there is lots of multi-line output?

  2. Alexander says:

    GREAT! thank you! :)

  3. Sankar Gorthi says:

    Thanks a bunch for this tip! It works perfectly.

    I ran into an issue with running this ant target from within a maven build environment, but was able to fix it by forking the java execution task. Hope that helps.

  4. Chantay Dama says:

    You put together an excellent position with what you stated. Folks should read your post to allow them to have a better viewpoint about this concern. It was fantastic of you to offer good details and supporting arguments. After reading this, I know my thoughts are extremely certain on the matter. Carry on the great work!

  5. Charlie Reos says:

    Nice idea, just what I was searching for

  6. Deniz Acay says:

    I was looking for something similar, then I liked this solution:

    <project …

  7. Deniz Acay says:


  8. Deniz Acay says:

    Oops my XML tags are wiped out, but the main point is that I use standard Java’s String operations on properties for this purpose.

    i.e. <copy tofile=”toLowerCase:${dest}” …

  9. Deniz Acay says:

    Looks like the Script task is the best alternative for this purposes. Mozilla Rhino is packaged with the Java 6+, so it is usable out of the box. I may write a post about it in a few days, one can even write all of the ant script via javascript.

  10. Floretta Schaal says:

    the right way to get a lady to like you how to get a girl to like you the way to get a girl to love you

  11. Issac Maez says:

    Very interesting topic, thank you for posting.

  12. gosu ume chidori 4 says:

    Thanks for the marvelous posting! I genuinely enjoyed reading it, you are a great author. I will be sure to bookmark your blog and may come back in the future. I want to encourage yourself to continue your great work, have a nice day!

  13. nba 2k16 mt forum says:

    Keep up the remarkable job !! Lovin’ it!|

  14. EugenioDFrom says:

    I used to be able to find good advice from your blog posts. Also visit my web blog - EugenioDFrom

  15. mywebsite says:

    I used to be able to find good advice from your articles. Also visit my blog mywebsite

  16. suba suba says:

    You have brought up a very superb points , thankyou for the post.

Leave a Reply

Your Privacy is important to us. We will never give, lease or sell your personal information.