2009-04-09 69 views
3

任何人都知道我可以在Ant脚本中输入多行值吗?我正在使用input task提示用户使用Subversion提交评论,我希望能够支持多行文本。如何使用Ant'input'任务读取多行值?

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

我想我可以做一个搜索并替换\ n,但我看不到任何简单的方法来做从属性值到属性值的替换。它看起来像我必须写一个文件,replace in the file,然后将文件加载到另一个属性。我不想那么糟糕。

回答

5

我不是100%肯定态度,但我看了看蚂蚁的源代码,它只是做了的readLine():

从/组织/阿帕奇/工具/ ANT /输入/ 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); 
      } 
     } 
    } 
} 

这里是如果我是你,我会做什么:

  • 如果您使用Ant 1.7,然后尝试实现自己的InputHandler,如documentation描述。 Apache许可证允许您基本上复制并粘贴上述代码作为起点。
  • 如果您使用的是Ant 1.6或更早版本,那么只需创建您自己的MultiLineInput任务。您可以扩展现有的Input类并只读多行。

无论哪种情况,您都需要决定用户如何表示“我完成了”。您可以使用空白行或句点或其他内容。

祝你好运!

P.S.当我做了一个“蚂蚁多行输入”的谷歌搜索,这个页面是第一次击中:-)。对于不到一个小时前问过的问题来说,这是相当不错的。

+0

感谢您的源代码。 – 2009-04-09 21:01:34