如何使用Ant'输入'任务读取多行值?

时间:2021-12-08 18:36:41

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.

任何人都知道如何在Ant脚本中输入多行值?我正在使用输入任务提示用户进行Subversion提交注释,我希望能够支持多行文本。

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

我在Windows命令提示符下运行Ant的独立版本。

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.

我以为我可能能够进行搜索并替换\ n,但是我看不到任何简单的方法来在Ant中将属性值替换为属性值。看起来我必须写一个文件,替换文件,然后将文件加载到另一个属性。我不是那么想要它。

1 个解决方案

#1


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

我对此并不是100%肯定,但我看了一下Ant源代码,它只是执行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.
  • 如果您使用的是Ant 1.7,请尝试实现自己的InputHandler,如文档中所述。 Apache许可证允许您基本上复制并粘贴上述代码作为起点。

  • 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.
  • 如果您使用的是Ant 1.6或更早版本,那么只需创建自己的MultiLineInput任务即可。您可以扩展现有的Input类,只读取多行。

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.

附:当我在谷歌搜索“蚂蚁多行输入”时,这个页面是第一个打击:-)。对于一个不到一小时前被问到的问题,令人印象深刻。

#1


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

我对此并不是100%肯定,但我看了一下Ant源代码,它只是执行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.
  • 如果您使用的是Ant 1.7,请尝试实现自己的InputHandler,如文档中所述。 Apache许可证允许您基本上复制并粘贴上述代码作为起点。

  • 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.
  • 如果您使用的是Ant 1.6或更早版本,那么只需创建自己的MultiLineInput任务即可。您可以扩展现有的Input类,只读取多行。

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.

附:当我在谷歌搜索“蚂蚁多行输入”时,这个页面是第一个打击:-)。对于一个不到一小时前被问到的问题,令人印象深刻。