tags:

views:

855

answers:

1

Anyone know how I can enter a multiline value in an Ant script? I'm prompting the user for a Subversion commit comment using the input task, and I'd like to be able to support multiple lines of text.

I'm running the standalone version of Ant at the Windows command prompt.

I thought I might be able to do a search and replace for \n, but I can't see any easy way to do a replace from property value to property value in Ant. It looks like I'd have to write a file, replace in the file, and then load the file into another property. I don't want it that badly.

+4  A: 

Hi Don. I'm not 100% positive about this, but I took a look at the Ant source code, and it just does a readLine():

From /org/apache/tools/ant/input/DefaultInputHandler.java:

/**
 * Prompts and requests input.  May loop until a valid input has
 * been entered.
 * @param request the request to handle
 * @throws BuildException if not possible to read from console
 */
public void handleInput(InputRequest request) throws BuildException {
    String prompt = getPrompt(request);
    BufferedReader r = null;
    try {
        r = new BufferedReader(new InputStreamReader(getInputStream()));
        do {
            System.err.println(prompt);
            System.err.flush();
            try {
                String input = r.readLine();
                request.setInput(input);
            } catch (IOException e) {
                throw new BuildException("Failed to read input from"
                                         + " Console.", e);
            }
        } while (!request.isInputValid());
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (IOException e) {
                throw new BuildException("Failed to close input.", e);
            }
        }
    }
}

Here is what I would do if I were you:

  • If you are using Ant 1.7, then try implementing your own InputHandler, as described in the documentation. The Apache License permits you to basically copy-and-paste the above code as a starting point.
  • If you are using Ant 1.6 or earlier, then just create your own MultiLineInput task. You can extend the existing Input class and just read multiple lines.

In either case, you would need to decide how the user indicates "I'm done." You could use a blank line or a period or something.

Good luck!

P.S. When I did a Google search for "ant multi-line input", this page was the first hit :-). Pretty impressive for a question that was asked less than an hour ago.

Matt Solnit
Thanks for the source code.
Don Kirkby