ant - 如果在给定时间内未输入,如何使 Ant 提示使用默认值?

标签 ant input timeout

我写的很多 Ant 脚本都使用了默认值,而这些默认值几乎是排他性的。 IE。我只是偶尔想在没有默认值的情况下运行它。

很多时候,这些脚本需要足够的时间,所以在它们运行时去做其他事情是有意义的,比如喝咖啡或使用小开发人员的房间。当然,如果上面有提示,而您忘记了,那么您就是SOL。

有什么办法可以在提示上设置超时,所以如果没有输入,哦,假设 30 秒,它只接受默认值,以便当我回到我的工作站时,我有我的 war / jar /准备好了吗?就像是

<input addproperty="branch.tag" 
       defaultvalue="dev" 
       timeout="30000">
    Which branch would you like to build?
</input>

现在显然这个超时功能不存在,但你明白我想要完成什么。

最佳答案

选项 1:配置构建以运行自动化或交互式
您可以通过在属性文件中提供默认输入值,将构建配置为全自动运行,而不是让输入提示超时。
default.properties

Which\ branch\ would\ you\ like\ to\ build?=dev
要在交互式构建和自动构建之间切换,可以在调用 Ant 时指定要使用的输入处理程序的类型:
自动化构建
$ ant -Dhandler.type=propertyfile
交互式构建
$ ant -Dhandler.type=default
需要使用嵌套的 <handler> 指定输入处理程序。元素。
<input addproperty="branch.tag" defaultvalue="dev"
    message="Which branch would you like to build?">
  <handler type="${handler.type}" />
</input>
最后一步是为 PropertyFileInputHandler 指定属性文件通过定义 ant.input.properties系统属性。
Linux
export ANT_OPTS=-Dant.input.properties=default.properties

选项 2:使用 AntContrib Trycatch结合 Parallel在宏定义中
<taskdef name="trycatch" classname="net.sf.antcontrib.logic.TryCatchTask">
  <classpath>
    <pathelement location="/your/path/to/ant-contrib.jar"/>
  </classpath>
</taskdef>

<macrodef name="input-timeout">
  <attribute name="addproperty" />
  <attribute name="defaultvalue" default="" />
  <attribute name="handlertype" default="default" />
  <attribute name="message" default="" />
  <attribute name="timeout" default="30000" />
  <text name="text" default="" />

  <sequential>
    <trycatch>
      <try>
        <parallel threadcount="1" timeout="@{timeout}">
          <input addproperty="@{addproperty}"
              defaultvalue="@{defaultvalue}"
              message="@{message}">
            <handler type="@{handlertype}" />
            @{text}
          </input>
        </parallel>
      </try>

      <catch>
        <property name="@{addproperty}" value="@{defaultvalue}" />
      </catch>
    </trycatch>
  </sequential>
</macrodef>


<target name="test-timeout">
  <input-timeout addproperty="branch.tag" defaultvalue="dev"
      message="Which branch would you like to build?"
      timeout="5000" />
  <echo message="${branch.tag}" />
</target>

选项 3:编写自定义 Ant 任务
实现留给读者作为练习。

关于ant - 如果在给定时间内未输入,如何使 Ant 提示使用默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11269502/

相关文章:

java - ant 支持 java 9 addmods 选项

jquery - 使用 jQuery 设置复选框样式

java - 设置套接字接收超时

javascript - 在 React 中单击按钮时,如何在输入字段的任意位置直接插入文本?

java - GAE : Request needs more than 30s

wordpress - WordPress崩溃,nginx和fastcgi问题?

java - 如何将使用ant运行的java程序的调试器附加到Eclipse?

ant - 如何使用 Ant 脚本遍历文件内容?

java - Sonarqube 评估 - Ant Build 失败

c - 如何梳理输入(方程式)并获取变量,忽略空格? (scanf()), (数组)